Flutter - 如何给 IconButton 上色?

Flutter - How to give a color to an IconButton?

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

 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()
             );
           }
         }

注意 linter 警告。您没有传递 IconButton 构造函数所需的 onPressed 参数。

添加它应该可以解决您的问题。

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 自动变灰以指示该按钮已禁用。有关详细信息,请参阅 the documentation