1

I wanna change color of button when user type at least one character on textfield also wanna change color when textfield is empty

so I tryed to flag(boolean) to make it

onChanged: (value) {
    if (value.length == 0) {
      setState(() {
        _isTimeFilled = false;
      });
    } else {
      setState(() {
        _isTimeFilled = true;
      });
    }
},

...

_isFilled ? Colors.grey : Colors.green

but it only change color when textfield unfocused

i tryed provider too but it's same. how can i fix this?

here's my code

    void _changeButtonColor(int value) {✅✅✅✅
    if (value == 0) {
      setState(() => _isTimeFilled = false);
    } else {
      setState(() => _isTimeFilled = true);
    }
  }
  
    TextFormField TimeTextFormField() {
    return TextFormField(
      focusNode: timeFocusNode,
      autofocus: true,
      keyboardType: TextInputType.number,
      textInputAction: TextInputAction.next,
      onChanged: (value) {
        _changeButtonColor(value.length);✅✅✅✅
      },
      
      
      RaisedButton(
            focusColor: Colors.white,
            splashColor: Colors.white,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(6.0),
            ),
            onPressed: () {},
            color: _isTimeFilled ? Colors.greenAccent : Colors.grey, ✅✅✅✅
            textColor: Colors.white,
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: SizedBox(
                  width: double.infinity, child: Icon(Icons.check)),
            ),
          ),
        ),
2
  • Check your variable _isTimeFilled must be above @override build. Commented Oct 11, 2020 at 3:13
  • right below class name 🙂 Commented Oct 11, 2020 at 4:33

3 Answers 3

6

Do this:

onChanged: (value) {
    _isTimeFilled = (value != 0);
    setState(() { });
},
Sign up to request clarification or add additional context in comments.

Comments

1

The onChanged of a TextField is a string as an argument. And, _isFilled and _isTimeFilled need to be unified.

So,

onChanged: (value) {
  if (value.length <= 0) {
    setState(() {
      _isFilled = false;
    });
  } else {
    setState(() {
      _isFilled = true;
    });
  }
},

Comments

0

Found solution but don't know exactly the cause. Just managed to make it.

solution: make a stateful widget and call it.

floatingActionButton: FloatingActionButton(
        onPressed: () {
          showCupertinoModalBottomSheet(
            context: context,
            builder: (context, scrollController) => inputForms(),
          );
        },

inputForms is method which return form fields.

solution: make form fields stateful widget and import it

floatingActionButton: FloatingActionButton(
    onPressed: () {
        showCupertinoModalBottomSheet(
        context: context,
        builder: (context, scrollController) => StatefulFormFields(),
    );
},

Comments

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.