【问题标题】:Flutter - ListView not showingFlutter - ListView 未显示
【发布时间】:2020-12-17 17:38:19
【问题描述】:

由于某种原因,ListView 没有出现。我认为其中一个小部件的高度从其父级获得非高度......也许?似乎有些小部件不会随其内容自动调整大小。我不想强制任何容器的大小,因为它们可能包含不同数量的信息。 https://codepen.io/dhust/pen/vYGZLPZ

更新: 错误:无法点击测试没有大小的渲染框。在这个 RenderBox 上调用了 hitTest() 方法:

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Container(
          child: Column(
            children: [
              Row(
                children: [
                  Container(
                    child: Column(
                      children: [
                        Expanded(
                          child: ListView.builder(
                            itemCount: 4,
                            itemBuilder: (BuildContext context, int index) {
                              return Text("Hi");
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter-web


    【解决方案1】:

    对于ListView 添加shrinkWrap: true, 以避免“垂直视口被赋予了无限的高度”。错误并将Expanded 用于其父Column 以避免“constraints.hasBoundedWidth is not true”。

        Column(
          children: [
            Row(
              children: [
                Expanded( // Use Expanded here
                  child: Container(
                    child: Column(
                      children: [
                        ListView.builder(
                          shrinkWrap: true, // and use shrinkWrap
                          itemCount: 4,
                          itemBuilder: (BuildContext context, int index) {
                            return Text("Hi");
                          },
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
    

    结果:

    【讨论】:

    • 感谢您的信息。对我上面的更新有什么想法吗?
    • 我看不出更新代码和原始代码之间的差异。我提到的解决方案对您不起作用?
    • 我收到错误消息“错误:无法对没有大小的渲染框进行测试。在此渲染框上调用了 hitTest() 方法:”。这发生在我的原始代码中,并且在尝试将其修改为看起来像您的代码之后。
    • 无法重现该问题。用另一个 Expanded 包装 Row 也会返回相同的错误,对吧?
    猜你喜欢
    • 2021-06-03
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2020-04-30
    • 2021-08-18
    相关资源
    最近更新 更多