Show/hide widgets in Flutter programmatically

前端 未结 13 1219
轮回少年
轮回少年 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:27

    UPDATE: Since this answer was written, Visibility was introduced and provides the best solution to this problem.


    You can use Opacity with an opacity: of 0.0 to draw make an element hidden but still occupy space.

    To make it not occupy space, replace it with an empty Container().

    EDIT: To wrap it in an Opacity object, do the following:

                new Opacity(opacity: 0.0, child: new Padding(
                  padding: const EdgeInsets.only(
                    left: 16.0,
                  ),
                  child: new Icon(pencil, color: CupertinoColors.activeBlue),
                ))
    

    Google Developers quick tutorial on Opacity: https://youtu.be/9hltevOHQBw

    0 讨论(0)
  • 2020-11-28 04:29

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

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


    Invisible example

    Visibility(
      child: Text("Invisible"),
      maintainSize: true, 
      maintainAnimation: true,
      maintainState: true,
      visible: false, 
    ),
    

    Gone example

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

    Alternatively, you can use if condition for both invisible and gone.

    Column(
      children: <Widget>[
        if (show) Text("This can be visible/not depending on condition"),
        Text("This is always visible"),
      ],
    ) 
    
    0 讨论(0)
  • 2020-11-28 04:31

    You can encapsulate any widget in your code with a new widget called (Visibility), this is from the yellow lamp at the very left side of the widget that you want it to be in-visible

    example: say you want to make a row invisible:

    1. Click in the lamp and choose (Wrap with widget)
    2. Rename the widget to Visibility
    3. Add the visible property and set it to false
    4. The Child of the newly created widget (Visibility Widget) is the Widget that you want it to be invisible

                Visibility(
                    visible: false,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        SizedBox(
                          width: 10,
                        ),
                        Text("Search",
                          style: TextStyle(fontSize: 20
                          ),),
                      ],
                    ),
                  ),
      

    I hope it will help someone in the future

    0 讨论(0)
  • 2020-11-28 04:32

    Flutter now contains a Visibility Widget that you should use to show/hide widgets. The widget can also be used to switch between 2 widgets by changing the replacement.

    This widget can achieve any of the states visible, invisible, gone and a lot more.

        Visibility(
          visible: true //Default is true,
          child: Text('Ndini uya uya'),
          //maintainSize: bool. When true this is equivalent to invisible;
          //replacement: Widget. Defaults to Sizedbox.shrink, 0x0
        ),
    
    0 讨论(0)
  • 2020-11-28 04:37

    One solution is to set tis widget color property to Colors.transparent. For instance:

    IconButton(
        icon: Image.asset("myImage.png",
            color: Colors.transparent,
        ),
        onPressed: () {},
    ),
    
    0 讨论(0)
  • 2020-11-28 04:39

    Try the Offstage widget

    if attribute offstage:true the not occupy the physical space and invisible,

    if attribute offstage:false it will occupy the physical space and visible

    Offstage(
       offstage: true,
       child: Text("Visible"),
    ),
    
    0 讨论(0)
提交回复
热议问题