首页 > 解决方案 > TabBarView 中的 Flutter ListView.builder 引发 hitTest 错误

问题描述

我有一个带有三个选项卡的屏幕。我的第三个选项卡应该根据收到的数据构建小部件。因此,我有以下类,其中包含该特定选项卡的 ListView.builder。

class ThirdFormEdit extends StatelessWidget {
  ThirdFormEdit({Key key, this.title}) : super(key: key);
  final String title;
  Map<dynamic,dynamic> _values = new Map();

  @override
  Widget build(BuildContext context) {
    final FormsInheritedWidgetState state = FormsInheritedWidget.of(context,true);
    _values = state.editableAllValues["ThirdForm"];

    return ListView.builder(
      itemCount: _values.length,
      itemBuilder: (context, index) {
        return Text("Test",style: TextStyle(color: Colors.black));
      }
    );
  }
}

这个类在这里被称为 TabBarView 的子类:

DefaultTabController(
    length: 3,
    child: Scaffold(
    body: TabBarView(
        children: <Widget>[
        SingleChildScrollView(child: FirstFormEdit(title: "Edit First Form",),),
        Text("Second Form"),
        SingleChildScrollView(child: ThirdFormEdit(title: "Edit First Form",),),
        ],
    ),
    ),
);

当我打开选项卡时,我收到以下错误:

I/flutter (22554): Another exception was thrown: Vertical viewport was given unbounded height.
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderViewport#8e25e NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderViewport#8e25e NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderIgnorePointer#b8d17 relayoutBoundary=up9 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#fcf71 relayoutBoundary=up8 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderPointerListener#14047 relayoutBoundary=up7 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderSemanticsGestureHandler#3ea95 relayoutBoundary=up6 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderPointerListener#47980 relayoutBoundary=up5 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: _RenderScrollSemantics#d5f2c relayoutBoundary=up4 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#bff38 relayoutBoundary=up3 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderCustomPaint#cb5c4 relayoutBoundary=up2 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#bfb82 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: _RenderSingleChildViewport#19173 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderIgnorePointer#2c868 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#7623a NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderPointerListener#c443d NEEDS-PAINT
NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderSemanticsGestureHandler#12cd3 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderPointerListener#8701f NEEDS-PAINT
NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: _RenderScrollSemantics#1935e NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#684e0 NEEDS-PAINT
NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderCustomPaint#0fa83 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#7440e NEEDS-PAINT
NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#04e1a NEEDS-PAINT
NEEDS-COMPOSITING-BITS-UPDATE
I/flutter (22554): Another exception was thrown: 'package:flutter/src/rendering/sliver_multi_box_adaptor.dart': Failed assertion: line 549 pos 12: 'child.hasSize': is not true.
I/flutter (22554): Another exception was thrown: RenderBox was not laid out: RenderIndexedSemantics#c8dd5 NEEDS-PAINTI/flutter (22554): Another exception was thrown: 'package:flutter/src/rendering/sliver_multi_box_adaptor.dart': Failed assertion: line 549 pos 12: 'child.hasSize': is not true.
E/flutter (22554): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Cannot hit test a render box with no size.
E/flutter (22554): The hitTest() method was called on this RenderBox: RenderIndexedSemantics#c8dd5 NEEDS-PAINT:
E/flutter (22554):   needs compositing
E/flutter (22554):   creator: IndexedSemantics ← NotificationListener<KeepAliveNotification> ← KeepAlive ← AutomaticKeepAlive ← KeyedSubtree-[Key <[<2>]>] ← SliverFillViewport ← Viewport ← IgnorePointer-[GlobalKey#2927f] ← Semantics ← Listener ← _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#8660e] ← ⋯
E/flutter (22554):   parentData: index=2; layoutOffset=720.0
E/flutter (22554):   constraints: BoxConstraints(w=360.0, h=552.0)
E/flutter (22554):   semantic boundary
E/flutter (22554):   size: MISSING
E/flutter (22554):   index: 2
E/flutter (22554): Although this node is not marked as needing layout, its size is not set.
E/flutter (22554): 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.
E/flutter (22554): #0      RenderBox.hitTest.<anonymous closure> (package:flutter/src/rendering/box.dart:2119:9)
E/flutter (22554): #1      RenderBox.hitTest (package:flutter/src/rendering/box.dart:2134:6)
E/flutter (22554): #2      _RenderSliverMultiBoxAdaptor&RenderSliver&ContainerRenderObjectMixin&RenderSliverHelpers.hitTestBoxChild.<anonymous closure> (package:flutter/src/rendering/sliver.dart:1636:22)
E/flutter (22554): #3      BoxHitTestResult.addWithRawTransform (package:flutter/src/rendering/box.dart:797:31)
E/flutter (22554): #4      BoxHitTestResult.addWithPaintOffset (package:flutter/src/rendering/box.dart:754:12)
E/flutter (22554): #5      _RenderSliverMultiBoxAdaptor&RenderSliver&ContainerRenderObjectMixin&RenderSliverHelpers.hitTestBoxChild (package:flutter/src/rendering/sliver.dart:1632:19)
E/flutter (22554): #6      RenderSliverMultiBoxAdaptor.hitTestChildren (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:564:11)
E/flutter (22554): #7      RenderSliver.hitTest (package:flutter/src/rendering/sliver.dart:1279:11)
E/flutter (22554): #8      RenderViewportBase.hitTestChildren.<anonymous closure> (package:flutter/src/rendering/viewport.dart:585:24)
E/flutter (22554): #9      BoxHitTestResult.addWithRawTransform (package:flutter/src/rendering/box.dart:797:31)
E/flutter (22554): #10     BoxHitTestResult.addWithPaintTransform (package:flutter/src/rendering/box.dart:725:12)
E/flutter (22554): #11     RenderViewportBase.hitTestChildren (package:flutter/src/rendering/viewport.dart:581:33)
E/flutter (22554): #12     RenderBox.hitTest (package:flutter/src/rendering/box.dart:2136:11)
E/flutter (22554): #13     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:114:19)
E/flutter (22554): #14     RenderBox.hitTest (package:flutter/src/rendering/box.dart:2136:11)
E/flutter (22554): #15     RenderIgnorePointer.hitTest (package:flutter/src/rendering/proxy_box.dart:2972:37)
E/flutter (22554): #16     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:114:19)
E/flutter (22554): #17     RenderBox.hitTest (package:flutter/src/rendering/box.dart:2136:11)
E/flutter (22554): #18     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:114:19)
E/flutter (22554): #19     RenderProxyBoxWithHitTestBehavior.hitTest (package:flutter/src/rendering/proxy_box.dart:161:19)
E/flutter (22554): #20     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:114:19)
E/flutter (22554): #21     RenderBox.hitTest (package:flutter/src/rendering/box.dart:2136:11)
E/flutter (22554): #22     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:114:19)
E/flutter (22554): #23     RenderProxyBoxWithHitTestBehavior.hitTest (package:flutter/src/rendering/proxy_box.dart:161:19)
E/flutter (22554): #24     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:114:19)
E/flutter (22554): #25     RenderBox.hitTest (package:flutter/src/rendering/box.dart:2136:11)
E/flutter (22554): #26     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.hitTestChildren (package:flutter/src/rendering/proxy_box.dart:114:19)
E/flutter (22554): #27     RenderBox.hitTest (package:flutter/src/rendering/box.dart:2136:11)
E/flutter (22554): #28     _RenderProxyBox&

我尝试将 ListView.builder() 放在 SizedBox 中,但没有奏效。我了解该错误与小部件的大小有关,但我不知道如何准确解决它。

标签: flutter

解决方案


经过几个小时的搜索,我找到了解决方案。为了使 ListView 工作,我必须按照Olaf Schlüter所说的(删除SingleChildScrollView我的小部件的 parnet),并将shrinkWrap属性添加到我的 ListView 中,值为 true。

我遇到的另一个问题是 ListView 不再可滚动。再次解决方案是将physics属性添加到具有 value 的 ListView 中BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics())


推荐阅读