Sprowk
Sprowk

Reputation: 397

How to update variable from TextFormField in flutter?

I have TextFormField which has its own controller. I also have a disabled button that I would like to enable after any input inside TextFormField. The problem is that I managed to enable the button however only when I close the keyboard. For example, if I'm typing a word in a session it won't enable the button unless I close the keyboard. I would like to enable the button after the first character.

This is the code:

final TextEditingController passwordController = new TextEditingController();

TextFormField(
  keyboardType: TextInputType.visiblePassword,
  controller: passwordController,
),

RaisedButton(
  onPressed: (passwordController.text== "") ? null : () {
    setState(() {
      _isLoading = true;
    });
    signIn(email, passwordController.text);
  },
),

Upvotes: 1

Views: 2629

Answers (3)

mjhansen3
mjhansen3

Reputation: 993

Based on what you're looking for, I created a boolean called isEnabled and set the initial value to false. This should work for you!

bool isEnabled;
final TextEditingController passwordController = new TextEditingController();

@override
void initState() {
  super.initState();
  passwordController.addListener(() {
    setState(() {
      if (passwordController.text.length > 0) {
        isEnabled = true;
      } else {
        isEnabled = false;
      }
    });
  });
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Column(
      children: <Widget>[
        TextFormField(
          keyboardType: TextInputType.visiblePassword,
          controller: passwordController,
        ),
        RaisedButton(
          child: Text('Click Me'),
          onPressed: isEnabled ? () {
            //print('Button has been pressed');
            setState(() {
              _isLoading = true;
            });
            signIn(email, passwordController.text);
          } : null,
        ),
      ],
    ),
  );
}

Upvotes: 1

CopsOnRoad
CopsOnRoad

Reputation: 267404

Is this what you're looking for?

final TextEditingController _controller = TextEditingController();
bool _isLoading = true;

// it tracks if TextField is enabled or disabled
bool _enabled = false;

@override
void initState() {
  super.initState();

  _controller.addListener(() { // you need to add listener like this
    setState(() {
      if (_controller.text.length > 0)
        _enabled = true;
      else
        _enabled = false;
    });
  });
}

Widget build(context) {
  return Scaffold(
    body: Column(
      children: <Widget>[
        SizedBox(height: 50),
        TextFormField(controller: _controller),
        RaisedButton(
          onPressed: !_enabled ? null : () => setState(() => _isLoading = true),
        ),
      ],
    ),
  );
}

Upvotes: 1

Sprowk
Sprowk

Reputation: 397

This worked:

var password = '';

TextFormField(
  keyboardType: TextInputType.visiblePassword,
  controller: passwordController,
  onChanged: (value){
    setState(() {
      password = value;
    });
  },
),

RaisedButton(
  onPressed: (password == "") ? null : () {
    setState(() {
      _isLoading = true;
    });
    signIn(email, password);
  },
),

Upvotes: 1

Related Questions