Flutter - Create dynamic number of texteditingcontrollers

前端 未结 3 1271
我在风中等你
我在风中等你 2020-12-16 23:55

I am recreating an app I have previously made in Swift, and on one of my pages we call an API and based on the results, we present the user a dynamic number of textfields to

3条回答
  •  孤街浪徒
    2020-12-17 00:21

    May be this can help you or other's as well.

    createFieldsList(context) {
        thirdStepUserRegistration.value.forEach((key, value) { //// This line will loop all your data [ValueNotifier> thirdStepUserRegistration]
          if (!fieldsController.containsKey(key)) {
            fieldsController[key] = TextEditingController(); //// This one will create you a list of controllers that u need for your fiels [Map fieldsController]
            fieldsList.add( ////You will be creating a list of widget which is textfields [List fieldsList]
              Container(
                height: 40.0,
                margin: EdgeInsets.only(bottom: 10),
                width: MediaQuery.of(context).size.width - 100,
                child: PrimaryTextFormField( //This is my customize textfield you can create yours
                  controller: fieldsController[key],
                  keyboardType: TextInputType.text,
                  textCapitalization: TextCapitalization.words,
                  inputFormatters: textFormatter(),
                  hintText: value,
                ),
              )
            );
          }
        });
      }
    
      Column additionalDetails(BuildContext context) {
        createFieldsList(context);
        return Column( children: fieldsList );
      }
    

提交回复
热议问题