按下颤动后如何立即更改图标颜色?

cam*_*ino 9 flutter

我想在按下图标后更改它的颜色,但似乎以下代码不起作用。

  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);
 } 
Run Code Online (Sandbox Code Playgroud)

目前,如果我按下图标,它的颜色不会改变,我必须回到它的父级,然后重新输入。我想知道如何立即改变它的颜色,谢谢。

更新:

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));
          }),
        );
      }
Run Code Online (Sandbox Code Playgroud)

Man*_*mar 5

您需要使用 setState() 函数。无论您在哪里更新变量值。

例如,我想将我的 _newVar 值更新为 newValue 并且应该将其更新到视图中,而不是写入

_newVar = newValue;
Run Code Online (Sandbox Code Playgroud)

它应该是:

setState(() {
 _newVar = newValue;
});
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以简单地为每种颜色设置一个条件:

color:(isPressed) ? Color(0xff007397)
                        : Color(0xff9A9A9A))
Run Code Online (Sandbox Code Playgroud)

并在 onPressed 函数中:

 onPressed: ()
                      {
                        setState(()
                        {
                          isPressed= true;
                        });                    
                      }
Run Code Online (Sandbox Code Playgroud)