Dart / Flutter 页面在 SetState 后不重新加载?
Dart / Flutter Page Not Reloading After SetState?
我有一个关注/取消关注按钮,它根据 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",
)
],
),
),
我试图根据 "friends" 的值显示关注或取消关注 RaisedButton,但它不起作用。我不确定这是我设置状态的方式还是我的三元运算符写错了。请帮忙!
要设置状态,请在 setState()
方法中添加您的逻辑。无论如何,当我查看您的代码时,我认为您不需要使用 async
,但您可以将其改回。
void unfollow(){
setState(() =>friends = "Not Friends");
}
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",
)
],
),
))));
}
}
我有一个关注/取消关注按钮,它根据 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",
)
],
),
),
我试图根据 "friends" 的值显示关注或取消关注 RaisedButton,但它不起作用。我不确定这是我设置状态的方式还是我的三元运算符写错了。请帮忙!
要设置状态,请在 setState()
方法中添加您的逻辑。无论如何,当我查看您的代码时,我认为您不需要使用 async
,但您可以将其改回。
void unfollow(){
setState(() =>friends = "Not Friends");
}
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",
)
],
),
))));
}
}