A RenderBox object must have an explicit size before it can be hit-tested

又在开发 Flutter 中碰到一个有意思的问题。

E/flutter ( 2033): Although this node is not marked as needing layout, its size is not set.
E/flutter ( 2033): A RenderBox object must have an explicit size before it can be hit-tested. Make sure that the RenderBox in question sets its size during layout.

它的原因是 ListView 放到 Column/Row 中导致。可能你觉得自己没使用 ListView,你仔细检查一下代码,可能某个 Widget 子控件使用了 ListView。根据提示,我们需要为 ListView 设置一个 Size

所以,解决办法也很简单,将 ListView 使用 Expanded 包裹,里面用 SizedBoxContainer 设置 height 属性就行。

    return Expanded(
        child: Container(
            margin: EdgeInsets.only(left: 12.w),
            height: double.infinity,
            child: ListView()));

本文由老郭种树原创,转载请注明:https://guozh.net/a-renderbox-object-must-have-an-explicit-size-before-it-can-be-hit-tested/

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注