简体   繁体   English

即使 Flutter 中父小部件的高度为零,图标仍然显示

[英]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.我有一个 AnimatedContainer,作为它的孩子,我有一个包含文本和图标按钮的行。 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.单击某个按钮后,我在 0 和 100 之间切换 Container 的高度。当 Container 高度为零时,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;
                });
              },
            ),
          ],
        ),
    );
  }

Please suggest how to solve this issue.请建议如何解决这个问题。

Add the if condition:添加if条件:

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您可能希望使用与 0 不同的值(如 10)来删除图标

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter,如何让子部件的高度与 SingleChildScrollView 中的父部件匹配? - Flutter,How to let a child widget's height match parent in SingleChildScrollView? 为 Flutter 的 Card 小部件设置特定高度 - Setting a specific height to Flutter's Card widget 颤振的 clipRRect 小部件现在显示在屏幕上 - flutter's clipRRect widget is now showing on the screen 子容器的高度取决于父容器的 Widget - Child Container's Height is depending on parent Container's Widget 在父窗口小部件中访问子窗口小部件的变量(Flutter with Dart) - Access child widget's variable in parent widget (Flutter with Dart) 获取 flutter 中的父小部件高度 - Get parent widget height in flutter Flutter:如何在 Flutter 中使 ListView 的高度 match_parent - Flutter: How to make a ListView's height match_parent in Flutter 即使在 Flutter 中弹出父窗口小部件屏幕,如何渲染仍将运行的 webview? - How to render a webview that will still running even though the parent widget screen is popped out in Flutter? 如何将小部件放置在堆栈中,但不匹配父级的宽度/高度? - How to position a Widget in a Stack, but without matching parent's width/height? 即使在 flutter 中使用硬编码宽度,小部件的宽度也不均匀 - Uneven widget's width even with hard coded width in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM