【问题标题】:How to force a ListView not to expand more than other widgets in a row (Flutter)如何强制 ListView 不连续扩展超过其他小部件(Flutter)
【发布时间】:2021-07-19 00:45:03
【问题描述】:

我正在为此苦苦挣扎,无法像在搜索引擎上那样描述它,所以这是我的问题:我有一个包含两个扩展的行,flex: 5 旨在拥有两个宽度相等的单元格;在第一个单元格内,我有一个列(包含一个表格和一个按钮);在第二个单元格内我有一个列(包含一个 ListView 和一个按钮);一旦其内容高于第一个单元格的固有高度,我想强制 ListView 变得可滚动,因此第二个单元格的高度永远不会高于第一个单元格的高度。

Row(
   children: [
      Expanded(
         flex: 5,
         child: Column(
            children: [
               DataTable(),
               ElevatedButton()
            ]
         ), // Column
      ), // Expanded
      Expanded(
         flex: 5,
         child: Column(
            children: [
               ListView(),
               ElevatedButton()
            ]
         ), // Column
      ) // Expanded
   ]
) // Row

我在 ListView 上尝试使用 shrinkWrap: true、在 Row 周围使用 IntrinsicHeight() 等时迷失了自我。

感谢您的帮助!

编辑:

我发现的解决方案是通过监听第一个单元格的大小变化并将其高度设置为第二个单元格的高度(感谢this Medium link)。即使我宁愿采用更清洁的方式,它似乎也能正常工作。

这是我的代码目前的样子:

Row(
   children: [
      Expanded(
         flex: 5,
         child: WidgetSize(
            onChange: (size) {
               if (size != null)
                  setState(() => heightOfTable = (size as Size).height);
            },
            child: Column(
               children: [
                  DataTable(),
                  ElevatedButton()
               ]
            ), // Column
         ), // WidgetSize
      ), // Expanded
      Expanded(
         flex: 5,
         child: Container(
            height: heightOfTable,
            child: Column(
               children: [
                  ListView(),
                  ElevatedButton()
               ]
            ), // Column
         ), // Container
      ) // Expanded
   ]
) // Row

如果你有更好的主意,请告诉我! :)

【问题讨论】:

    标签: flutter listview row height


    【解决方案1】:

    我认为您可以尝试将ListView 包裹在Container 中,并给出合适的height 和`width。

    Row(
       children: [
          Expanded(
             flex: 5,
             child: Column(
                children: [
                   DataTable(),
                   ElevatedButton()
                ]
             ), // Column
          ), // Expanded
          Expanded(
             flex: 5,
             child: Column(
                children: [
                   Container(
               height:,
               width:,
              child: ListView()),
                   ElevatedButton()
                ]
             ), // Column
          ) // Expanded
       ]
    ) // Row
    

    【讨论】:

    • 我不想使用硬编码值,而是让 Table 的高度(由其内容定义)来定义 ListView 的高度。
    猜你喜欢
    • 1970-01-01
    • 2020-01-16
    • 2021-10-15
    • 2021-08-18
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多