Flutter - 如何为 IconButton 提供颜色?

ada*_*ori 3 flutter 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()
             );
           }
         }
Run Code Online (Sandbox Code Playgroud)

Chr*_*ore 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: () {
              
            }
          ),
        ],
      ),
    );
  }
}

Run Code Online (Sandbox Code Playgroud)

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