【问题标题】:dynamically adding widgets (FormBuilderTextField) to formBuilder将小部件 (FormBuilderTextField) 动态添加到 formBuilder
【发布时间】:2021-01-19 08:51:05
【问题描述】:

我正在使用 flutter_form_builder 创建表单。用户将使用表单创建自己的投票,并且他/她将根据需要向他/她的问题添加选项。我解决了选择部分的动态添加,但现在的问题是;当我用一个选项创建一个问题并点击提交按钮时,选择、问题和日期会打印在控制台中,但是如果我点击 fab 按钮来创建另一个选择,只有新创建的字段会打印它的内容日期和问题,旧的字段被忽略。

如何获得动态创建的每个字段的结果..

提前谢谢..

这是我的代码:-

import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:intl/intl.dart';


class AddPoll extends StatefulWidget {
  @override
  _AddPollState createState() => _AddPollState();
}

class _AddPollState extends State<AddPoll> {

  final GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();
  List<Widget> choiceFieldList;

  void initState() {
    super.initState();
    choiceFieldList = <Widget>[];
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(title: Text("Create a poll")),
        floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: () {
            setState(() {
              choiceFieldList.add(ChoiceField());
            });
          }
        ),
        body: Padding(
          padding: EdgeInsets.all(10.0),
          child: SingleChildScrollView(
            child: Column(
              children: [
                FormBuilder(
                  key: _fbKey,
                  initialValue: {
                    'date': DateTime.now()
                  },
                  autovalidateMode: AutovalidateMode.always,
                  child: Column(
                    children: <Widget>[
                      FormBuilderTextField(
                        attribute: 'question',
                        validators: [FormBuilderValidators.required()],
                        maxLines: null,
                        keyboardType: TextInputType.multiline,
                        decoration: InputDecoration(
                          labelText: "Your Question",
                          border: OutlineInputBorder()
                        )
                      ),
                      Container(
                        padding: EdgeInsets.fromLTRB(0, 30, 0, 20),
                        alignment: Alignment.centerLeft,
                        child: Text(
                          "Choices:-",
                          style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.bold
                          )
                        )
                      ),
                      ListView.builder(
                        shrinkWrap: true,
                        itemCount: choiceFieldList.length,
                        itemBuilder: (context, index) => choiceFieldList[index],
                      ),
                      Visibility(
                        visible: false,
                        child: FormBuilderDateTimePicker(
                          attribute: "date",
                          inputType: InputType.both,
                          validators: [FormBuilderValidators.required()],
                          format: DateFormat("dd-MM-yyyy 'at' h:mma"),
                          decoration: InputDecoration(labelText: "Date of Poll")
                        )
                      )
                    ]
                  )
                ),
                SizedBox(height: 30.0),
                ButtonBar(
                  alignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    RaisedButton(
                      child: Text("Reset"),
                      onPressed: () {
                        _fbKey.currentState.reset();
                      }
                    ),
                    RaisedButton(
                      child: Text("Submit"),
                      onPressed: () async {
                        _fbKey.currentState.save();
                        print(_fbKey.currentState.value);
                        if (_fbKey.currentState.validate()) {
                          // Loading().show(context);
                          // var date = _fbKey.currentState.value['date'];
                          // var resp = await registerUser(date);
                        }
                      }
                    )
                  ]
                )
              ]
            )
          )
        )
      )
    );
  }
}

class ChoiceField extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return Center(
      child: FormBuilderTextField(
        attribute: 'choice',
        validators: [FormBuilderValidators.required()],
        decoration: InputDecoration(
          labelText: "Enter Choice",
          border: OutlineInputBorder()
        )
      )
    );
  }
}

【问题讨论】:

    标签: flutter flutter-form-builder


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    您需要提供不同的attribute 名称
    你可以使用attributeName: "choice ${choiceFieldList.length}"
    代码sn-p

    onPressed: () {
              setState(() {
                choiceFieldList.add(ChoiceField(
                  attributeName: "choice ${choiceFieldList.length}",
                ));
              });
            }
    ...     
    class ChoiceField extends StatelessWidget {
      final String attributeName;
      ChoiceField({this.attributeName});
    
      @override
      Widget build(BuildContext context) {
        return Center(
            child: FormBuilderTextField(
                attribute: attributeName,   
    

    输出

    I/flutter ( 7497): {date: 2020-10-05 08:54:56.785373, question: q, choice 0: a, choice 1: b}
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    import 'package:flutter_form_builder/flutter_form_builder.dart';
    import 'package:intl/intl.dart';
    
    class AddPoll extends StatefulWidget {
      @override
      _AddPollState createState() => _AddPollState();
    }
    
    class _AddPollState extends State<AddPoll> {
      final GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();
      List<Widget> choiceFieldList;
    
      void initState() {
        super.initState();
        choiceFieldList = <Widget>[];
      }
    
      @override
      Widget build(BuildContext context) {
        return SafeArea(
            child: Scaffold(
                appBar: AppBar(title: Text("Create a poll")),
                floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
                floatingActionButton: FloatingActionButton(
                    child: Icon(Icons.add),
                    onPressed: () {
                      setState(() {
                        choiceFieldList.add(ChoiceField(
                          attributeName: "choice ${choiceFieldList.length}",
                        ));
                      });
                    }),
                body: Padding(
                    padding: EdgeInsets.all(10.0),
                    child: SingleChildScrollView(
                        child: Column(children: [
                      FormBuilder(
                          key: _fbKey,
                          initialValue: {'date': DateTime.now()},
                          autovalidateMode: AutovalidateMode.always,
                          child: Column(children: <Widget>[
                            FormBuilderTextField(
                                attribute: 'question',
                                validators: [FormBuilderValidators.required()],
                                maxLines: null,
                                keyboardType: TextInputType.multiline,
                                decoration: InputDecoration(
                                    labelText: "Your Question",
                                    border: OutlineInputBorder())),
                            Container(
                                padding: EdgeInsets.fromLTRB(0, 30, 0, 20),
                                alignment: Alignment.centerLeft,
                                child: Text("Choices:-",
                                    style: TextStyle(
                                        fontSize: 18,
                                        fontWeight: FontWeight.bold))),
                            ListView.builder(
                              shrinkWrap: true,
                              itemCount: choiceFieldList.length,
                              itemBuilder: (context, index) =>
                                  choiceFieldList[index],
                            ),
                            Visibility(
                                visible: false,
                                child: FormBuilderDateTimePicker(
                                    attribute: "date",
                                    inputType: InputType.both,
                                    validators: [FormBuilderValidators.required()],
                                    format: DateFormat("dd-MM-yyyy 'at' h:mma"),
                                    decoration:
                                        InputDecoration(labelText: "Date of Poll")))
                          ])),
                      SizedBox(height: 30.0),
                      ButtonBar(
                          alignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            RaisedButton(
                                child: Text("Reset"),
                                onPressed: () {
                                  _fbKey.currentState.reset();
                                }),
                            RaisedButton(
                                child: Text("Submit"),
                                onPressed: () async {
                                  _fbKey.currentState.save();
                                  print(_fbKey.currentState.value);
                                  if (_fbKey.currentState.validate()) {
                                    // Loading().show(context);
                                    // var date = _fbKey.currentState.value['date'];
                                    // var resp = await registerUser(date);
                                  }
                                })
                          ])
                    ])))));
      }
    }
    
    class ChoiceField extends StatelessWidget {
      final String attributeName;
      ChoiceField({this.attributeName});
    
      @override
      Widget build(BuildContext context) {
        return Center(
            child: FormBuilderTextField(
                attribute: attributeName,
                validators: [FormBuilderValidators.required()],
                decoration: InputDecoration(
                    labelText: "Enter Choice", border: OutlineInputBorder())));
      }
    }
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: AddPoll(),
        );
      }
    }
    

    【讨论】:

    • 我正在查看我的问题并发现我没有感谢您的回答......谢谢......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2021-06-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多