9

I would like to change the color of an icon after pressing it, but it seems the following code doesn't work.

  void actionClickRow(String key) {
    Navigator.of(context).push(
      new MaterialPageRoute(builder: (context) {
        return new Scaffold(
            appBar: new AppBar(
              title: new Text(key),
              actions: <Widget>[
                new IconButton(
                  icon: new Icon(
                    Icons.favorite, 
                    color: isSaved(key)  ? Colors.red : null,  //<--- if key is already saved, then set it to red
                    ), 
                  onPressed: ()
                  {
                    setState(() //<--whenever icon is pressed, force redraw the widget
                    {
                      pressFavorite(key);
                    });                    
                  }
                ),
              ],
            ),
            backgroundColor: Colors.teal,
            body: _showContent(key));
      }),
    );
  }


 void pressFavorite(String key)
  {
    if (isSaved(key))
      saved_.remove(key);
    else
      saved_.add(key);
  }

 bool isSaved(String key) {
    return saved_.contains(key);
 } 

Currently, if I press the icon, its color will not change, I have to go back to its parent, then re-enter. I am wondering how to change its color immediately, Thanks.

update:

class MainPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new MainPageState();
  }
}


class MainPageState extends State<MainPage> {
      bool _alreadySaved;

      void actionRowLevel2(String key) {
        _alreadySaved = isSaved(key);
        Navigator.of(context).push(
          new MaterialPageRoute(builder: (context) {
            return new Scaffold(
                appBar: new AppBar(
                  title: new Text(key),
                  actions: <Widget>[
                    new IconButton(
                      icon: new Icon(
                        _alreadySaved ? Icons.favorite : Icons.favorite_border,
                        color:  _alreadySaved ? Colors.red : null,
                        ), 
                      onPressed: ()
                      {
                        setState(()
                        {
                          pressFavorite(key);
                          _alreadySaved = isSaved(key); //<--update alreadSaved
                        });                    
                      }
                    ),
                  ],
                ),
                backgroundColor: Colors.teal,
                body: _showScript(key));
          }),
        );
      }
2
  • Are you sure your have extended statefulwidget? May 9, 2018 at 4:07
  • @ArnoldParge Yes, MainPage extended StatefulWidget
    – camino
    May 11, 2018 at 0:14

2 Answers 2

9

You can simply put a condition for each color :

color:(isPressed) ? Color(0xff007397)
                        : Color(0xff9A9A9A))

and in the onPressed function :

 onPressed: ()
                      {
                        setState(()
                        {
                          isPressed= true;
                        });                    
                      }
7

You need to use setState() function. Wherever you are updating your variable values.

For example, I want to update my _newVar value to newValue and this should be updated into the view then instead of writing

_newVar = newValue;

it should be:

setState(() {
 _newVar = newValue;
});
6
  • I do called setState() inside onPressed. Do you know why it doesn't work?
    – camino
    May 6, 2018 at 22:58
  • It will be helpful if you provide your latest code here. In the above-mentioned code, you are not assigning the new value for color. May 8, 2018 at 20:15
  • I update the code and you can see that I update _alreadySaved in setState function. But it doesn't work either
    – camino
    May 9, 2018 at 0:54
  • Hi @camino. For your reference I have created a sample app on github. You just have to pull it and run it. This has sample code for changing the color on Press. github.com/manishkumar-dsi/awesome_flutter May 11, 2018 at 18:52
  • So, if I have 4 icons ? Sep 18, 2019 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.