Flutter 将表单传递给多个小部件

Flutter 将表单传递给多个小部件,flutter,Flutter,我正在构建一个包含长表单的应用程序。所以我决定把它分成几个步骤。 每个步骤都是一个包含表单字段的小部件 所以我会有这样的东西: int _currentStep = 0; final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); String _firstname; String _lastname; String _city; List<Widget> formGroup

我正在构建一个包含长表单的应用程序。所以我决定把它分成几个步骤。 每个步骤都是一个包含表单字段的小部件

所以我会有这样的东西:

int _currentStep = 0;
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  String _firstname;
  String _lastname;
  String _city;

  List<Widget> formGroups = [
    FormGroup1(),
    FormGroup2(),
    FormGroup3(),
  ];
我的想法是能够导航到像这样的下一个小部件

Form(
  key: _formKey,
  child: formGroups[_currentStep],
),
  void goToNext() {
    setState(() {
      if (_currentStep < formGroups.length - 1) {
        _currentStep += 1;
      }
    });
  }
void goToNext(){
设置状态(){
if(_currentStep
首先,这是一个好的做法吗? 如何获取主窗口小部件以从子窗口小部件获取输入? 例如,如果FormGroup2包含用于设置_lastname的inputfield,如何使其在表单级别可用


感谢您的帮助。

每当您希望父窗口小部件从子窗口小部件获取输入时,您总是在父窗口使用
通知侦听器
,并传递包含子窗口小部件数据的
通知

当用户滑动到最后一页并仍在尝试滑动时,许多颤振小部件(如
DefaultTabController
会使用此技术接收
overscroll通知

在您的用例中,您可以将值通知从子小部件传递到表单小部件

这里有一个演示供您参考

以下是演示此小部件使用的工作代码:

import 'package:flutter/material.dart';

main() => runApp(MaterialApp(home: MyApp()));

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _string = 'You haven\'t pressed a button yet' ;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Notification demo'),),
      body: NotificationListener<IntegerNotification>(
        onNotification: (IntegerNotification notification){
          print(notification.value);
          setState(() {
            _string = 'You have pressed button ${notification.value} times.';
          });
          return true;
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(_string),
            ChildWidget(),
          ],
        ),
      ),
    );
  }
}

class ChildWidget extends StatefulWidget {
  @override
  _ChildWidgetState createState() => _ChildWidgetState();
}

class _ChildWidgetState extends State<ChildWidget> {
  int _counter = 0;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: RaisedButton(
        child: Text('Increment'),
        onPressed: () {
          IntegerNotification(++_counter).dispatch(context);
        },
      ),
    );
  }
}

class IntegerNotification extends Notification {
  final value;

  IntegerNotification(this.value);
}
导入“包装:颤振/材料.省道”;
main()=>runApp(MaterialApp(home:MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
String _String='您还没有按下按钮';
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本(“通知演示”),
正文:NotificationListener(
onNotification:(集成通知){
打印(通知值);
设置状态(){
_字符串='您已按按钮${notification.value}次';
});
返回true;
},
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
文本(_字符串),
ChildWidget(),
],
),
),
);
}
}
类ChildWidget扩展StatefulWidget{
@凌驾
_ChildWidgetState createState()=>\u ChildWidgetState();
}
类_ChildWidgetState扩展状态{
int _计数器=0;
@凌驾
小部件构建(构建上下文){
返回中心(
孩子:升起按钮(
子项:文本('Increment'),
已按下:(){
IntegerNotification(+++u计数器).dispatch(上下文);
},
),
);
}
}
类IntegerNotification扩展了通知{
最终价值;
整数通知(this.value);
}

我希望这有帮助,如果有任何疑问,请发表评论。

“因此我决定将其分为几个步骤。”
-尝试了
步进器
类?不。一个多步骤表单对不起,我不知道“多步骤表单”是什么意思,而不是一个巨大的表单,我将把它分成一组字段,并有条件地显示这就是
Stepper
类的用途:“一个material stepper小部件,显示一系列步骤的进度。如果表单中的一个步骤需要完成另一个步骤,或者需要完成多个步骤才能提交整个表单,那么步进器尤其有用。“谢谢@Kalpesh,我会尝试一下,让你知道它是如何进行的。干杯如果它对你有用,请不要忘记接受并投票决定答案。”。。