Home > Mobile >  Flutter maintain the values of a form in a visibility widget
Flutter maintain the values of a form in a visibility widget

Time:10-04

Am trying to build a custom step form using visibility to hide and show widgets but i have noticed my form looses values once i hide a widget and try to show it again.

I have the following

int currentStep = 0;


void next(){
   setState(() => _currentStep  = 1)
 }

void back(){
   setState(() => _currentStep -= 1)
 }

for the widget i have

   var _nameCtl = TextEditingController();

  @override
  Widget build(BuildContext context) { 
   return(           
     Column(
      children: [

       Visibility(
        visible: _currentStep ==0,
         child: Column(children: [
           TextFormField(
            controller: _nameCtl,
            decoration:InputDecoration(
             labelText: 'Phone',
          ),
         )
       ],
      ) 
     //other visibility with inputs
    ],
  )

);

Whenever the value of currrent step changes and i try to come back to the for input the value in the textinput is lost. Is there a way to main the value on the input once visiblity is changed

CodePudding user response:

Enable maintainState in Visibility like this,

...
Visibility(
  maintainState: true,
...
  • Related