final TextEditingController _weight = TextEditingController();
if (_weight.text.contains(RegExp(r'[0-9]')))
Padding(
padding: const EdgeInsets.only(bottom: 20),
child: BMIButton(
onpressed: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.rightToLeft,
child: BMIHeight(),
inheritTheme: true,
ctx: context),
);
},
))
I’m trying to show an OutlinedButton when the user enters some data into the textFormField. When I enter a value in the TextFormField and confirm, it doesn’t show a button, but when I hot reload it, it sees that value and shows the button.
4
Answers
This means you need just to update the state of your widget, first make sure this inside a
StatefulWidget
, then add aSetState(() {})
on the end of that method:Try to add listener and call setState to update ui
And override the dispose method and depose this controller.
TextEditingController
is implementingListenable
. You can exploit that to build part of your UI conditionally.Moreover,
TextEditingController
must be correctly initialized and disposed of. You can do that smoothly with flutter_hooks.You’d obtain this concise result:
Your code that checks if the TextFormField is populated needs to be inside the build function of a stateful widget.
To trigger the state update, listen to changes on the TextFormField, inside the function set the state of some variable that you can check.
Add this to the initState method:
And change your if statement as follows:
Method 2
You could also Wrap your Widget with a Visibility Widget:
This still needs the listener with the setState call.