Forms 基于多页的颤振创建表单

Forms 基于多页的颤振创建表单,forms,flutter,dart,bloc,flutter-provider,Forms,Flutter,Dart,Bloc,Flutter Provider,我需要创建基于多个页面的表单,验证并在完成时提交。如何使用provider和Flatter bloc包实现类似于此表单的内容?我试着用存储来创建这样的表单,但我认为这是一种糟糕的做法。有人知道吗?谢谢你 我建议您使用flow\u builder()插件,该插件将根据状态为您处理流。存储库中的示例正是您想要的。首先,您将构建一个用作状态的模型 class Profile { const Profile({this.name, this.age, this.weight}); final

我需要创建基于多个页面的表单,验证并在完成时提交。如何使用provider和Flatter bloc包实现类似于此表单的内容?我试着用存储来创建这样的表单,但我认为这是一种糟糕的做法。有人知道吗?谢谢你

我建议您使用
flow\u builder
()插件,该插件将根据状态为您处理流。存储库中的示例正是您想要的。首先,您将构建一个用作状态的模型

class Profile {
  const Profile({this.name, this.age, this.weight});

  final String name;
  final int age;
  final int weight;

  Profile copyWith({String name, int age, int weight}) {
    return Profile(
      name: name ?? this.name,
      age: age ?? this.age,
      weight: weight ?? this.weight,
    );
  }
}
然后您可以像这样使用
FlowBuilder
小部件:

FlowBuilder<Profile>(
  state: const Profile(),
  onGeneratePages: (profile, pages) {
    return [
      MaterialPage(child: NameForm()),
      if (profile.name != null) MaterialPage(child: AgeForm()),
    ];
  },
);
FlowBuilder(
状态:const Profile(),
onGeneratePages:(个人资料,页数){
返回[
MaterialPage(子项:NameForm()),
如果(profile.name!=null)MaterialPage(子项:AgeForm()),
];
},
);
然后,您可以使用以下方法更新表单页面中的小部件:

context.flow<Profile>().update((profile) => profile.copyWith(name: _name));
context.flow().update((profile)=>profile.copyWith(name:_name));

我建议您使用
flow\u builder
()插件,该插件将根据状态为您处理流。存储库中的示例正是您想要的。首先,您将构建一个用作状态的模型

class Profile {
  const Profile({this.name, this.age, this.weight});

  final String name;
  final int age;
  final int weight;

  Profile copyWith({String name, int age, int weight}) {
    return Profile(
      name: name ?? this.name,
      age: age ?? this.age,
      weight: weight ?? this.weight,
    );
  }
}
然后您可以像这样使用
FlowBuilder
小部件:

FlowBuilder<Profile>(
  state: const Profile(),
  onGeneratePages: (profile, pages) {
    return [
      MaterialPage(child: NameForm()),
      if (profile.name != null) MaterialPage(child: AgeForm()),
    ];
  },
);
FlowBuilder(
状态:const Profile(),
onGeneratePages:(个人资料,页数){
返回[
MaterialPage(子项:NameForm()),
如果(profile.name!=null)MaterialPage(子项:AgeForm()),
];
},
);
然后,您可以使用以下方法更新表单页面中的小部件:

context.flow<Profile>().update((profile) => profile.copyWith(name: _name));
context.flow().update((profile)=>profile.copyWith(name:_name));