I have a FormBuilderTextField
that I want to toggle it's enabled
property based on another FormBuilderTextField
value length.
I know there are ways with TextEditingController
, but I want to try it another way because I have a lot of FormBuilderTextField
.
I tried using onChanged
but to no avail.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
TextEditingController username = TextEditingController();
TextEditingController password = TextEditingController();
bool enabledPassword = false;
bool enabledSubmitButton = false;
@override
Widget build(BuildContext context) {
return Column(
children: [
const SizedBox(height: 12),
TextField(
controller: username,
onChanged: (value) {
setState(() {
enabledPassword = value.length >= 8 ? true : false;//this
});
},
decoration: const InputDecoration(
hintText: 'Username',
contentPadding:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.blue, width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
),
),
const SizedBox(height: 12),
TextField(
controller: password,
onChanged: (value) {
setState(() {
enabledSubmitButton = value.length >= 8 ? true : false;//this
});
},
enabled: enabledPassword,//this
decoration: const InputDecoration(
hintText: 'Password',
contentPadding:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
border: OutlineInputBorder(),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.blue, width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
disabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey, width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: enabledSubmitButton//this
? () {
print('Username:${username.text}');
print('Password:${password.text}');
}
: null,
child: const Text('Submit'),
)
],
);
}
}
See result here