简体   繁体   中英

Icons still showing even if it's parent widget's height is zero in Flutter

I have an AnimatedContainer and as it's child, I have a Row containing a Text and an IconButton. After clicking some button, I am toggling the height of the Container between 0 and 100. When the Container height is zero, the IconButton is still visible (not the Text) and not clickable. 在此处输入图像描述

在此处输入图像描述

 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;
                });
              },
            ),
          ],
        ),
    );
  }

Please suggest how to solve this issue.

Add the if condition:

if (height > 0)
     IconButton(icon: Icon(Icons.favorite_border), onPressed: () {},)

You might want to use a different value from 0 (like 10) to remove the icon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM