【问题标题】:Flutter - How to give a color to an IconButton?Flutter - 如何为 IconButton 赋予颜色?
【发布时间】:2020-10-19 23:28:49
【问题描述】:

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

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

【问题讨论】:

    标签: flutter iconbutton


    【解决方案1】:

    注意 linter 警告。您没有传递 onPressed 构造函数所需的 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 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      相关资源
      最近更新 更多