0

I find myself working with TextFormField hand in hand with DropdownButton. There is a strange process going on inside the view. I put the steps below:

  1. To the TexFormField, I'm assigning an initial value: controller: _nameController..text = datumAdministrative.name.

initial value

  1. I enter a new value to the TextFormField.

new value

  1. When the DropdownButton is deployed, the keyboard is closed the whole Widget is redrawn, which causes the TextFormField to recover its initial value and does not keep the new value entered.

 keyboard is closed

Is there any way to avoid, that the TextFormField returns to its initial value when selecting DropdownButton? I would appreciate if you could help me with a post or feedback.

Code Scaffold:

return Scaffold(
  resizeToAvoidBottomPadding: false,
  resizeToAvoidBottomInset: true,
  appBar: AppBar(
      backgroundColor: ColorsTheme.primary,
      leading: GestureDetector(
        onTap: () {
          Navigator.pushReplacementNamed(context, 'administrativeListData');
        },
        child: Container(
            padding: EdgeInsets.all(16),
            child: FaIcon(FontAwesomeIcons.arrowLeft)),
      ),
      centerTitle: true,
      title: Text(datumAdministrative.name +
          ' ' +
          datumAdministrative.lastNameFather),
      automaticallyImplyLeading: false),
  body: Stack(
    children: <Widget>[
      ImageBackground(),
      _sizeBoxHeight(),
      Container(
        child: Form(
          key: formValidator.formKey,
          child: ListView(
            children: <Widget>[
              _containerImageUser(context, datumAdministrative),
              _sizeBoxHeight(),

             
              CustomCardExpansionTile(
                title: Constants.administrativeData,
                icon: FontAwesomeIcons.addressBook,
                widget: Container(
                    padding: EdgeInsets.all(15),
                    child: responsiveDataAdministrative(context)),
              ),

             
              CustomCardExpansionTile(
                  title: Constants.addressInformationAdministrative,
                  icon: FontAwesomeIcons.houseUser,
                  widget: Container(
                      padding: EdgeInsets.all(15),
                      child: responsiveAddressInformationAdministrative(
                          context))),

              
              CustomCardExpansionTile(
                  title: Constants.userDataSystem,
                  icon: FontAwesomeIcons.idCard,
                  widget: Container(
                      padding: EdgeInsets.all(15),
                      child: responsiveInformationSystemAdministrative(
                          context))),

            ],
          ),
        ),
      ),
    ],
  ),
);

Code TextFormField:

TextFormField(
  controller: _nameController..text = datumAdministrative.name,
  keyboardType: TextInputType.text,
  textInputAction: null,
  textCapitalization: TextCapitalization.sentences,
  onChanged: (value) => formValidator.name = value,
  validator: (value) {
    if (formValidator.fieldValidText(value)) {
      return null;
    } else {
      return Constants.nameAdministrativeMessage;
    }
  });
4
  • can you this full widget? Commented Aug 24, 2021 at 22:02
  • Sure, I edit my question and put the complete Widget Commented Aug 24, 2021 at 22:15
  • Where CustomTextField is coming from? Also, can you provide unique key:ValueKey() on TextFiled, and can you provide simple widget with producing the same issue that you are facing? Commented Aug 24, 2021 at 22:49
  • CustomTextField It is a Widget of type TextFormField. key:ValueKey() , It is located within the Form. Commented Aug 24, 2021 at 23:01

1 Answer 1

1

I see that you set the value for the text property of TextEditingController inside the build method. So, it will be invoked whenever the widget rebuilds, causing the value for the field back to the initial one.

The docs actually tells about it:

text property

Setting this will notify all the listeners of this TextEditingController that they need to update (it calls notifyListeners). For this reason, this value should only be set between frames, e.g. in response to user actions, not during the build, layout, or paint phases.

To fix it, you should remove the cascade notation here:

controller: _nameController..text = datumAdministrative.name,

Do it inside initState() instead:

@override
void initState() {
   _nameController.text = datumAdministrative.name;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.