Home > database >  How to set Visibility function in flutter to show or hide Text Form Field?
How to set Visibility function in flutter to show or hide Text Form Field?

Time:03-30

I want to set an option in my Trivia game to the user set a Text Field in specific question ID. for now I trying to use Visibility widget but its only take the bool _visibleText value, And does not get the value from void validateText. any idea what I missing?

this is my code:

class _QuestionCardState extends State<QuestionCard> {
  bool _visibleText = false;

  void validateText() => setState(() {
        if (widget.question.id == 1) {
          _visibleText = false;
        } else {
          if (widget.question.id == 2) {
            _visibleText = true;
          }
        }
      });

and the widget:

 Visibility(
        visible: (_visibleText),
        child: Container(
          width: 320.0,
          alignment: Alignment.center,
          child: TextFormField(
            textAlign: TextAlign.center,
            decoration: InputDecoration(
              enabledBorder: const OutlineInputBorder(
                borderSide:
                    const BorderSide(color: Color(0xFFCBA583), width: 2.0),
                borderRadius: BorderRadius.all(
                  Radius.circular(12),
                ),
              ),
              filled: true,
              fillColor: Colors.white,
              hintText: widget.question.text,
              hintStyle: TextStyle(
                fontSize: 16.0,
                color: Color(0xFF067751),
                fontFamily: 'Calibri',
                letterSpacing: 2,
              ),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(12)),
              ),
            ),
          ),
        ),
      ),

CodePudding user response:

validateText is never called.

Change the function to this :

  bool validateText() {
    if (widget.question.id == 1) return false;
    if (widget.question.id == 2) return true;
  }

And in your build method :

visible: validateText(),

CodePudding user response:

The validateText function is never called. If you have a button that toggles this then call it in it's onTap event.

CodePudding user response:

If you need to continuously toggle visibility during the game, please use streambuilder and leave your validatetext function inside streambuilder's sink method. Then streamcontroller will continuously change your UI dynamically. If you don't understand at all, then please mention that , I will try to add the full codebase. Thank You.

  • Related