Show/hide widgets in Flutter programmatically

前端 未结 13 1297
轮回少年
轮回少年 2020-11-28 04:13

In Android, every single View subclass has a setVisibility() method that allows you modify the visibility of a View object

The

13条回答
  •  难免孤独
    2020-11-28 04:43

    Update

    Flutter now has a Visibility widget. To implement your own solution start with the below code.


    Make a widget yourself.

    show/hide

    class ShowWhen extends StatelessWidget {
      final Widget child;
      final bool condition;
      ShowWhen({this.child, this.condition});
    
      @override
      Widget build(BuildContext context) {
        return Opacity(opacity: this.condition ? 1.0 : 0.0, child: this.child);
      }
    }
    

    show/remove

    class RenderWhen extends StatelessWidget {
      final Widget child;
      final bool condition;
      RenderWhen({this.child, this.show});
    
      @override
      Widget build(BuildContext context) {
        return this.condition ? this.child : Container();
      }
    }
    

    By the way, does any one have a better name for the widgets above?

    More Reads

    1. Article on how to make a visibility widget.

提交回复
热议问题