Flutter - 如何为IconButton指定颜色?

3

通过阅读文档,我确信这是已经声明好的,但添加图标仍然是灰色的。

 class _TaskState extends State<Task> {    
       @override
       Widget build(BuildContext context) {
    



     return Scaffold(
           

appBar: AppBar(
             backgroundColor: Colors.red,
             title: Text('Tasks'),
             centerTitle: true,
             actions: <Widget>[
               IconButton(
                 icon: Icon(Icons.add),
                 color: Colors.white,
                 iconSize: 32.0,
                   ),
                 ],
               ),
               drawer: TheDrawer()
             );
           }
         }
1个回答

6

注意linter警告。你没有传递所需的onPressed参数以供IconButton构造函数使用。

添加该参数应该可以解决你的问题。

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(Task());
}

class Task extends StatefulWidget {
    @override
    _TaskState createState() => _TaskState();
}
    
class _TaskState extends State {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.red,
        title: Text('Tasks'),
        centerTitle: true,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.add),
            color: Colors.white,
            iconSize: 32.0,
            onPressed: () {
              
            }
          ),
        ],
      ),
    );
  }
}

onPressed 回调为 null 时,IconButton 会自动变灰以指示按钮已禁用。更多信息请参见 文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接