【问题标题】:Why Can't I Define the Height of an Inner Container?为什么不能定义内容器的高度?
【发布时间】:2021-10-08 23:19:43
【问题描述】:

无论我做什么,我似乎都无法在我的颤振应用程序中调整嵌套容器的高度。它似乎总是继承父 Container 的高度。我在网上阅读的所有内容都说用Center() 包裹内部容器。我试过了,但还是遇到了同样的问题。

  final _recommendations = <String>["Embroidery", "Jewelry Making", "Tennis", "Hiking", "Disc Golf"];

    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Container(
          padding: EdgeInsets.symmetric(horizontal: 5.0, vertical: 16.0),
          height: 200,
        child: ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: _recommendations.length,
          itemBuilder: (context, index) {
            return Container(
              width: 150,
              height: 50,
              child: Card(
                color: Colors.blue,
                child: Center(
                  child: Text(_recommendations[index])
                )
              )
            );
          }
        )

      )
      );

在我看来,ListView 的高度是由其父级 Container 定义的。所以我希望ListView 的高度为200。然后Card 的高度应由其父容器定义,其高度为50。但是Card 的高度为200...

【问题讨论】:

  • 尝试用ExpandedFlexible 小部件包装您的父容器,如果这不起作用,那么对 ListView 执行相同操作即可
  • ExpandedFlexible 包裹不起作用...

标签: flutter dart flutter-layout


【解决方案1】:

通常,ListView 小部件会尝试填充父元素提供的所有可用空间,即使列表项需要更少的空间。您可以使用ListViewshrinkWrap 参数禁用此功能,如下所示:\

ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: _recommendations.length,
          shrinkWrap:true,
          itemBuilder: (context, index) {
            return Container(
              width: 150,
              height: 100,
              child: Card(
                color: Colors.blue,
                child: Center(
                  child: Text(_recommendations[index])
                )
              )
            );
          }
        )

【讨论】:

  • shrinkWrap: true 没有解决问题:\
猜你喜欢
  • 2016-07-15
  • 2013-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
相关资源
最近更新 更多