如何在 flutter 中使用范围模型管理多页面表单状态
How to manage multi page form state with scoped model in flutter
我有一个多页表单需要用来管理状态。我目前正在尝试使用范围模型,但它不起作用。我转到另一个页面的所有内容都已清除。我通常使用 PageView 小部件来创建多页表单。我真正知道的是为什么状态不持久。谢谢
您想确保您的 ScopedModel 正确包装了多页表单中的所有页面。通常,您希望使用 ScopedModel 包装整个应用程序。像这样:
Future startUp() async {
UserModel userModel = await loadUser();
runApp(ScopedModel<UserModel>(model: userModel, child: MyApp()));
}
void main() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
startUp();
}
在某些情况下,您想在模型更改(用户登录?)时重建
示例:
@override
Widget build(BuildContext context) {
return ScopedModelDescendant<UserModel>(
builder: (BuildContext context, Widget child, UserModel model) {
return Scaffold(
body: ListView(
children: <Widget>[
// your page here
]
)
);
});
}
我有一个多页表单需要用来管理状态。我目前正在尝试使用范围模型,但它不起作用。我转到另一个页面的所有内容都已清除。我通常使用 PageView 小部件来创建多页表单。我真正知道的是为什么状态不持久。谢谢
您想确保您的 ScopedModel 正确包装了多页表单中的所有页面。通常,您希望使用 ScopedModel 包装整个应用程序。像这样:
Future startUp() async {
UserModel userModel = await loadUser();
runApp(ScopedModel<UserModel>(model: userModel, child: MyApp()));
}
void main() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
startUp();
}
在某些情况下,您想在模型更改(用户登录?)时重建
示例:
@override
Widget build(BuildContext context) {
return ScopedModelDescendant<UserModel>(
builder: (BuildContext context, Widget child, UserModel model) {
return Scaffold(
body: ListView(
children: <Widget>[
// your page here
]
)
);
});
}