简体   繁体   English

如何在 flutter 中添加小部件列表?

[英]how to add list of widgets in flutter?

I have used the package flutter_form_builder to create list of widget form.我已使用 package flutter_form_builder创建小部件表单列表。

I created a page that enables the user to add as many widget as he wants with the function named _addwidget()我创建了一个页面,使用户可以使用名为 _addwidget() 的 function 添加任意数量的小部件



 var _myWidgets = <Widget>[];
 int _index = 0;
 var evaluationOptions = ['1', '2', '3', '4', '5', '6', '7', '8'];
 
 bool autoValidate = true;
 final _formKey = GlobalKey<FormBuilderState>();


 final Map<int, String> nomModulevalues = Map();
 final Map<int, String> evaluationvalues = Map();

 final List<TextEditingController> _nomModuleControllers = [];
 final List<TextEditingController> _evaluationnumber = [];

  void _addwidget() {
   int keyValue = _index;
   _myWidgets = List.from(_myWidgets)
     ..add(Column(
       key: Key("$keyValue"),
       children: <Widget>[
         SizedBox(height: 10),
         Container(
           
           child: Column(
             children: <Widget>[
                  .......
                   Column(
                     children: <Widget>[
                       SizedBox(
                         height: 20,
                       ),
                       _buildnommodule(keyValue),
                       _buildevaluation,
                      ],
                   ),
                 ],
               )
             ],
           ),
         )
       ],
     ));

   setState(() => ++_index);
 }
     
 @override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
  .....
     floatingActionButton: FloatingActionButton(
       onPressed: () {
         _addwidget();  
   ....
     body: SizedBox(
       height: double.infinity,
       width: double.infinity,
       child: Form(
           autovalidateMode: AutovalidateMode.disabled,
           key: _formKey,
           child: Stack(
        ....
                   Expanded(
                     child: SizedBox(
                       child: ListView(
                         padding: const EdgeInsets.all(10.0),
                         children: _myWidgets,
                     ),
                   ),
                   )],
               )




 Widget _buildnommodule(int keyValue) {
   TextEditingController controller = TextEditingController();
   _nomModuleControllers.add(controller);
   return Container(
     child: FormBuilderTextField(
       name: 'nom',
       controller: controller,
     ),
   );
 }


 Widget _buildevaluation(int keyValue) {
   TextEditingController controller = TextEditingController();
   _evaluationnumber.add(controller);
   return FormBuilder(
     autovalidateMode: AutovalidateMode.disabled,
     child: DropdownButtonHideUnderline(
         child: FormBuilderDropdown(
       name: 'evaluation',
       validator: FormBuilderValidators.compose(
           [FormBuilderValidators.required(context)]),
       items: evaluationOptions
           .map((evaluation) => DropdownMenuItem(
                 value: evaluation,
                 child: Text(
                   evaluation,
                   style: const TextStyle(color: Colors.black),
                   textAlign: TextAlign.center,
                 ),
               ))
           .toList(),
     )),
   );
 }
}

The code works well and is generated as the user wants, but when you start filling out this questionnaire, you notice that the information disappears when moving from one widget to another For example, we create four widgets by pressing four times in the FloatingActionButton,after when filling the first three parts and moving to the fourth, we notice that the information entered in the first and second barriers has disappeared (empty widget)代码运行良好,根据用户的需要生成,但是当您开始填写此问卷时,您会注意到从一个小部件移动到另一个时信息消失了例如,我们通过在 FloatingActionButton 中按四次创建四个小部件,之后当填充前三个部分并移动到第四部分时,我们注意到在第一和第二个障碍中输入的信息已经消失(空小部件)

As i understood the content of your FormFields is disappearing as soon you scroll right?据我了解,一旦您向右滚动,您的 FormFields 的内容就会消失?

That's probably a behaviour of the ListView.这可能是 ListView 的一种行为。 The ListView automatically disposes the state of the Widgets out of View. ListView 会自动将 Widget 的 state 丢弃到 View 之外。

You can try to add this to your ListView:您可以尝试将其添加到您的 ListView:

ListView(
  addAutomaticKeepAlives: true,
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM