即使 parent 小部件的高度在 Flutter 中为零,图标仍然显示
Icons still showing even if it's parent widget's height is zero in Flutter
我有一个 AnimatedContainer,因为它是 child,所以我有一个包含文本和图标按钮的行。单击某个按钮后,我在 0 到 100 之间切换容器的高度。当容器高度为零时,IconButton 仍然可见(不是文本)并且不可单击。
Widget _myAnimatedContainer() {
return AnimatedContainer(
curve: Curves.easeOut,
alignment: Alignment.center,
duration: Duration(milliseconds: 300),
height: height,
color: Colors.green,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text(
'Animated Container',
style: TextStyle(fontSize: 20),
overflow: TextOverflow.ellipsis,
),
IconButton(icon: Icon(Icons.favorite_border), onPressed: () {},)
],
),
);
}
class _AnimatedContainerExampleState extends State<AnimatedContainerExample> {
double height = 100;
bool isExpanded = false;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
_myAnimatedContainer(),
SizedBox(height: 20,),
RaisedButton(
child: Text('Expand'),
onPressed: () {
setState(() {
isExpanded = !isExpanded;
height = isExpanded ? 100 : 0;
});
},
),
],
),
);
}
请建议如何解决此问题。
添加if条件:
if (height > 0)
IconButton(icon: Icon(Icons.favorite_border), onPressed: () {},)
您可能希望使用不同于 0 的值(例如 10)来删除图标
我有一个 AnimatedContainer,因为它是 child,所以我有一个包含文本和图标按钮的行。单击某个按钮后,我在 0 到 100 之间切换容器的高度。当容器高度为零时,IconButton 仍然可见(不是文本)并且不可单击。
Widget _myAnimatedContainer() {
return AnimatedContainer(
curve: Curves.easeOut,
alignment: Alignment.center,
duration: Duration(milliseconds: 300),
height: height,
color: Colors.green,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text(
'Animated Container',
style: TextStyle(fontSize: 20),
overflow: TextOverflow.ellipsis,
),
IconButton(icon: Icon(Icons.favorite_border), onPressed: () {},)
],
),
);
}
class _AnimatedContainerExampleState extends State<AnimatedContainerExample> {
double height = 100;
bool isExpanded = false;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
_myAnimatedContainer(),
SizedBox(height: 20,),
RaisedButton(
child: Text('Expand'),
onPressed: () {
setState(() {
isExpanded = !isExpanded;
height = isExpanded ? 100 : 0;
});
},
),
],
),
);
}
请建议如何解决此问题。
添加if条件:
if (height > 0)
IconButton(icon: Icon(Icons.favorite_border), onPressed: () {},)
您可能希望使用不同于 0 的值(例如 10)来删除图标