用户在 Flutter 中进行更改后如何在没有路由器导航的情况下自动刷新页面?
How to refresh a page automatically after user make changes in Flutter, without router's navigate?
我写了一个小部件来显示带有一些列表数据的主屏幕,这些数据应该从数据库中加载,如下所示:
class _HomeWidgetState extends State<HomeWidget> {
//create a new data and inser into database
Future<void> showInformationDialog(BuildContext context) async {
return await showDialog(context: context,
builder: (context){
return StatefulBuilder(builder: (context,setState){
return AlertDialog(
//form to get information
content: Form(),
actions: <Widget>[
TextButton(
onPressed: (){
//..insert to database
toast("create finished!!");
Navigator.of(context).pop();
//I used to use navigate to refresh this page to load data,but it's
too boring
//router.navigateTo(context,'index',transition:TransitionType.fadeIn );
}
},
),
],
);
});
});
}
@override
Widget build(BuildContext context) {
...
return Scaffold(
body: makeBody,
floatingActionButton:FloatingActionButton(
child: Icon(Icons.add),
onPressed: (){
showInformationDialog(context).then((value) => null);
},
),
);
}
}
我想实现的是:在用户创建新数据并点击post后,它可以将此数据插入数据库,并且无需刷新页面,直接使用导航,但更温和的方式。不知道是不是进入页面的问题。这可能吗?
点击post
后直接使用SetState
TextButton(
onPressed: (){
setState(() {});
}),
但是为什么呢?
Calling setState notifies the framework that the internal state of this object has changed in a way that might impact the user interface in this subtree, which causes the framework to schedule a build for this State object.
因此,如果小部件的状态发生变化,您可以调用 setState 来触发视图的重建,并立即看到新状态所暗示的变化。
我写了一个小部件来显示带有一些列表数据的主屏幕,这些数据应该从数据库中加载,如下所示:
class _HomeWidgetState extends State<HomeWidget> {
//create a new data and inser into database
Future<void> showInformationDialog(BuildContext context) async {
return await showDialog(context: context,
builder: (context){
return StatefulBuilder(builder: (context,setState){
return AlertDialog(
//form to get information
content: Form(),
actions: <Widget>[
TextButton(
onPressed: (){
//..insert to database
toast("create finished!!");
Navigator.of(context).pop();
//I used to use navigate to refresh this page to load data,but it's
too boring
//router.navigateTo(context,'index',transition:TransitionType.fadeIn );
}
},
),
],
);
});
});
}
@override
Widget build(BuildContext context) {
...
return Scaffold(
body: makeBody,
floatingActionButton:FloatingActionButton(
child: Icon(Icons.add),
onPressed: (){
showInformationDialog(context).then((value) => null);
},
),
);
}
}
我想实现的是:在用户创建新数据并点击post后,它可以将此数据插入数据库,并且无需刷新页面,直接使用导航,但更温和的方式。不知道是不是进入页面的问题。这可能吗?
点击post
后直接使用SetState
TextButton(
onPressed: (){
setState(() {});
}),
但是为什么呢?
Calling setState notifies the framework that the internal state of this object has changed in a way that might impact the user interface in this subtree, which causes the framework to schedule a build for this State object.
因此,如果小部件的状态发生变化,您可以调用 setState 来触发视图的重建,并立即看到新状态所暗示的变化。