Dart / Flutter页面在SetState之后没有重新加载?

时间:2018-02-27 01:10:10

标签: dart state flutter ternary

我有一个follow / unfollow按钮,可以根据公共字符串的值更改UI。问题是在String的值发生变化并且调用了setState后,UI保持不变。如果我重新加载页面,那么它可能会改变。这是我的代码......

var friends;

Future<Null> follow() async{
friends = "Friends"
setState((){});
}

Future<Null> unfollow() async{
friends = "Not Friends"
setState((){});
}

@override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          backgroundColor: Colors.amber,
          title: new Text(
            'Friends',
            ),
          ),
        ),
        body: new Container(


         // Unfollow and Message

                friends == "Friends" ? new RaisedButton(
                      onPressed: unfollow,
                      child: new Row(
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          new Icon(
                            Icons.not_interested,
                          ),
                          new Text(
                            "UNFOLLOW",
                          )
                        ],
                      ),
                    ) :

                  new RaisedButton(
                  onPressed: follow,
                  child: new Row(
                    children: <Widget>[
                      new Icon(
                        Icons.thumb_up,
                      ),
                      new Text(
                        "FOLLOW",
                      )
                    ],
                  ),
                ),

我正在尝试显示跟随或取消关注的RaisedButton,具体取决于“朋友”的价值而且无法正常工作。我不确定这是我设置状态的方式,还是我的三元运算符写错了。请帮忙!

1 个答案:

答案 0 :(得分:1)

setState仅适用于有状态小部件,但不适用于无状态小部件 它是一个有状态的小部件..?

我已经添加了答案,请查看

 void main() {
   runApp(new samplea());
  }

  class samplea extends StatefulWidget {
 @override
_sampleaState createState() => new _sampleaState();
 }

class _sampleaState extends State<samplea> {
 var friends;

   follow() {
  friends = "Friends";
   setState(() {});
   }

    unfollow(){
  friends = "Not Friends";
  setState(() {});
  }

  @override
  Widget build(BuildContext context) {
   return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Plugin example app'),
        ),
        body: new Container(
            child: friends == "Friends"
                ? new RaisedButton(
                    onPressed: unfollow,
                    child: new Row(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        new Icon(
                          Icons.not_interested,
                        ),
                        new Text(
                          "UNFOLLOW",
                        )
                      ],
                    ),
                  )
                : new RaisedButton(
                    onPressed: follow,
                    child: new Row(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        new Icon(
                          Icons.not_interested,
                        ),
                        new Text(
                          "FOLLOW",
                        )
                      ],
                    ),
                  ))));
  }
  }