1

我有一个需要管理状态的多页表单。我目前正在尝试使用范围模型,但它不起作用。我转到另一个页面的所有内容都被清除了。我通常是 PageView 小部件来创建多页表单。我真正知道的是为什么状态没有持续存在。谢谢

4

1 回答 1

0

您要确保您的 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
            ]
          )
      );
    });
  }
于 2020-04-25T06:45:14.277 回答