Ashtav
Ashtav

Reputation: 2966

How to hide or show widget by key in flutter?

I have Material widget like this in my StatefulWidget

final _key = GlobalKey();

new Material(
   key: _key,
   color: Colors.white,
   child: Text('lorem ipsum')
)

then I need to hide or show Material widget like this

new RaisedButton(
    child: Text('Click')
    color: Colors.green,
    onPressed: (){ _key.currentContext.hide; }
)

It's not work, how to solve it? thanks

Upvotes: 3

Views: 716

Answers (1)

griffins
griffins

Reputation: 8246

you can make your button a child of any of this widgets which manages visibility read more on this blog post here is a summary:

**Visibility widget ** visibility widget

Visibility(
  visible: false,
  child: Container(),
  )
);

the child widget is not rendered in the subtree, and Flutter uses instead a shrinked sized box to replace it.

**Offstage widget ** offstage widget

Offstage(
  offstage: true,
  child: //your child
  )
);

Offstage renders the child widget off set the screen. This means that the widget is not rendered in the subtree and so doesn't take any space.

**Opacity widget ** opacity widget

 Opacity(
  opacity: 0.0,
  child: Container(
      child: //child here
  )
);

This widget simply sets the child's opacity to zero but still renders it. So the child is hidden but takes space and you can interact with it.

Upvotes: 1

Related Questions