7

I want to hide a text widget visibility on the click of a button using Visibility widget. How can I achieve this in Flutter? I have created a function which accepts a bool variable which defines the visibility of the Text Widget. On the click of the button, I am calling that function. For the first time, the text is shown to the user. But on the click of the button, the text is not invisible.

//created a method to show and hide the text using visibility widget
hideTextVisibility(bool visibilityStatus){
return visibilityStatus? Visibility(
visible: visibilityStatus,
child: Text("flutter"),

):Container();}



//button click code and for the first time the text will be visible 
RaisedButton(onPressed: (){
setState(() {
hideTextVisibility(false);
});

2 Answers 2

18

Step 1:

bool _visible = false;

Step 2:

void _toggle() {
    setState(() {
      _visible = !_visible;
    });
  }

Step 3: Add on your RaisedButton or any other button

onPressed: _toggle,

Step 4: Code your widget like this.

Gone: The widget doesn't take any physical space and is completely gone.

Visibility(
  child: Text("Gone"),
  visible: _visible,
),

Invisible: The widget takes physical space on the screen but not visible to user.

Visibility(
  child: Text("Invisible"),
  maintainSize: true, 
  maintainAnimation: true,
  maintainState: true,
  visible: _visible, 
),
Sign up to request clarification or add additional context in comments.

Comments

2

You are passing hardcode false to the button's method hideTextVisibility(false); just change here hideTextVisibility(visibilityStatus);

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.