2

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.

1
  • What does "I tried using onChanged but to no avail" mean? Can you show what you have tried and why it does not work? Commented Nov 23, 2022 at 11:42

1 Answer 1

0
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

Sign up to request clarification or add additional context in comments.

1 Comment

What if I got multiple textfield and want only a certain texfield to change its enabled state?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.