Search code examples
fluttericonbutton

Flutter - How to give a color to an IconButton?


By reading documentation, i'm sure this is well-declared, but the add icon is still gray.

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

Solution

  • Pay attention to linter warnings. You are not passing an onPressed parameter which is required for the IconButton constructor.

    Adding it should solve your issue.

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

    When the onPressed callback is null, the IconButton automatically greys itself out to indicate that the button is disabled. See the documentation for more information.