I have a form and I want it to redraw when I press the submit button and go from saying "Enter" to "Please wait".
For this I use a StatefullWidget
with the property isLoading
that changes when the button is pressed with a setState
from false to true. The button's text is rendered based on the value of the boolean isLoading
, however pressing the button does not re-draw it correctly and still shows the text "Enter".
class _LoginForm extends StatefulWidget {
@override
State<_LoginForm> createState() => _LoginFormState();
}
class _LoginFormState extends State<_LoginForm> {
@override
Widget build(BuildContext context) {
bool isLoading = false;
return Container(
child: Form(
child: Column(
children: [
TextFormField(
),
//Botón en cuestion
MaterialButton(
onPressed:(){
setState(() {
isLoading=true; //setState donde debería redibuujarse el botón
});
} ,
child:Container(
child:Text(isLoading ? 'Espere' : 'Ingresar', style:TextStyle(color:Colors.white)),//Texto que se renderiza según isLoading, pero que no se redibuja como debería
),
)
],
),
),
);
}
}
You are making a mistake, the variable
isLoading
must go outside the methodbuild
, put it outside the methodbuild
and it will work correctly.