我有一个关注/取消关注按钮,它根据公共(public)字符串的值更改 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",
)
],
),
),
我试图根据“ friend ”的值(value)显示关注或取消关注 RaisedButton,但它不起作用。我不确定这是我设置状态的方式还是我的三元运算符写错了。请帮忙!
最佳答案
要设置状态,请在 setState()
方法中添加您的逻辑。无论如何,当我查看您的代码时,我认为您不需要使用 async
,但您可以将其改回。
void unfollow(){
setState(() =>friends = "Not Friends");
}
关于Dart/Flutter 页面在 SetState 后不重新加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48999623/