0
点赞
收藏
分享

微信扫一扫

flutter 控件隐藏(Offstage、Visibility)

乌龙茶3297 2022-03-16 阅读 96

使用Offstage隐藏控件

 Offstage({ Key? key,
  this.offstage = true,//是否显示
   Widget? child //控件
})
 Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Offstage(
                offstage: isShow,
                child: Container(
                  color: Colors.tealAccent,
                  height: 100,
                  width: 100,
                ),
              ),
              TextButton(
                  onPressed: () {
                    setState(() {
                      isShow = !isShow;
                    });
                  },
                  child: Text('点我 $isShow')),
            ],
          ),

使用Visibility

const Visibility({
    Key? key,
    required this.child,
    this.replacement = const SizedBox.shrink(),
    this.visible = true,//是否显示
    this.maintainState = false,//不可见时显示的组件(当maintainState = false)
    this.maintainAnimation = false,//不可见时,是否维持子组件中的动画
    this.maintainSize = false,//不可见时是否留有空间
    this.maintainSemantics = false,//不可见时是否维持它的语义
    this.maintainInteractivity = false,//不可见时是否具有交互性
  }) 

这三个参数必须统一值

 maintainSize: true,
 maintainState: true,
 maintainAnimation: true,
Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Visibility(
                maintainSize: true,
                maintainState: true,
                maintainAnimation: true,
                visible: isVisible,
                child: Container(
                  color: Colors.tealAccent,
                  height: 100,
                  width: 100,
                ),
              ),
              TextButton(
                  onPressed: () {
                    setState(() {
                      isVisible = !isVisible;
                    });
                  },
                  child: Text('点我 $isShow')),
            ],
          ),
举报

相关推荐

0 条评论