使用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,
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')),
],
),