【问题标题】:Get an error `Failed assertion: line 1687 pos 12: 'hasSize'` when render ListTile inside `Row`在“Row”中渲染 ListTile 时出现错误“断言失败:第 1687 行 pos 12:'hasSize'”
【发布时间】:2020-09-02 23:08:16
【问题描述】:

我在颤振中有以下代码。

Widget build(BuildContext context) {
    return Center(
      child: Card(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Row(
              children: <Widget>[
                SizedBox(
                    height: 100,
                    child: ListTile(
                      leading: IconButton(
                        iconSize: 30,
                        icon: roundImage(post.userPicture, Icon(Icons.person)),
                        onPressed: () {},
                      ),
                      subtitle: Text(this.post.message),
                    )),
              ],
            ),
          ],
        ),
      ),
    );
  }

但我收到此错误:

════════ Exception caught by rendering library ═════════════════════════════════
RenderBox was not laid out: RenderPhysicalShape#7713c relayoutBoundary=up3
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1687 pos 12: 'hasSize'
The relevant error-causing widget was
    Card 

我在Row 中添加了SizedBox,但它仍然抱怨hasSize 错误。我该如何解决这个问题?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    目前尚不清楚您要实现什么,但您可以尝试将ExpandedFlexible 包裹在小部件中,而不是使用SizedBox

    【讨论】:

      【解决方案2】:

      我尝试将 200 的宽度添加到您的 SizedBox 并且它有效。

      尽管按照@dhanasekar 的建议使用Expanded 也很有效。

      【讨论】:

        【解决方案3】:

        添加灵活

        Row(
            children: [
              new Flexible(
                child: new TextField(
                  decoration: const InputDecoration(helperText: "Enter App ID"),
                  style: Theme.of(context).textTheme.body1,
                ),
              ),
            ],
          ),
        

        【讨论】:

          【解决方案4】:

          我在尝试拥有一个 Column 并且它的一个孩子是 ScopedModelDescendant 时遇到了这个问题。

          同样的错误让我发疯了。

          阅读此主题并在layout widgets 上进行自我教育的组合给了我解决方案:

          我已将 ScopedModelDescendant 子组件包装在 Flexible 小部件中,如下所示:

          Column(
           children: [
            Flexible(child: ScopedModelDescendant<MyAPIProvider>(...)
           ...
          

          现在它可以工作了。

          【讨论】:

            猜你喜欢
            • 2021-03-04
            • 2023-01-24
            • 2021-08-19
            • 2020-04-22
            • 2020-12-29
            • 2021-11-02
            • 2021-05-30
            • 2021-03-07
            • 1970-01-01
            相关资源
            最近更新 更多