简体   繁体   English

如何在颤动后立即改变图标颜色?

[英]how to change icon color immediately after pressed in flutter?

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

You can simply put a condition for each color : 您可以简单地为每种颜色设置一个条件:

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

and in the onPressed function : 并在onPressed函数中:

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

You need to use setState() function. 您需要使用setState()函数。 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,这应该更新到视图然后而不是写

_newVar = newValue;

it should be: 它应该是:

setState(() {
 _newVar = newValue;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM