【发布时间】:2022-01-01 17:32:58
【问题描述】:
我正在尝试为带有一些文本和文本框的简单屏幕构建 UI,但我遇到了类似这样的奇怪布局错误。
Error: Cannot hit test a render box that has never been laid out.
The hitTest() method was called on this RenderBox: RenderStack#487f4 NEEDS-LAYOUT NEEDS-PAINT:
creator: Stack ← _FloatingActionButtonTransition ← MediaQuery ← LayoutId-[<_ScaffoldSlot.floatingActionButton>] ← CustomMultiChildLayout ← AnimatedBuilder ← DefaultTextStyle ← AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#0cc7b ink renderer] ← NotificationListener<LayoutChangedNotification> ← PhysicalModel ← AnimatedPhysicalModel ← ⋯
parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.floatingActionButton
constraints: MISSING
size: MISSING
alignment: Alignment.centerRight
textDirection: ltr
fit: loose
Unfortunately, this object's geometry is not known at this time, probably because it has never been laid out. This means it cannot be accurately hit-tested.
If you are trying to perform a hit test during the layout phase itself, make sure you only hit test nodes that have completed layout (e.g. the node's children, after their layout() method has been called).
at Object.throw_ [as throw] (http://localhost:61969/dart_sdk.js:5061:11)
at http://localhost:61969/packages/flutter/src/rendering/layer.dart.lib.js:4204:23
at stack.RenderStack.new.hitTest (http://localhost:61969/packages/flutter/src/rendering/layer.dart.lib.js:4209:26)
at http://localhost:61969/packages/flutter/src/rendering/layer.dart.lib.js:7665:44
at box.BoxHitTestResult.wrap.addWithPaintOffset (http://localhost:61969/packages/flutter/src/rendering/layer.dart.lib.js:7418:19)
...
所以,我删除了所有代码,只保留了两个文本框和两个文本字段,以了解导致问题的原因。但我仍然无法弄清楚原因。这是我的无状态小部件的构建方法的简化代码:
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text("Create"),
),
body: Container(
child:Column(
children: [
//First row - Amount and Ccy labels/text boxes
Expanded(
child: Row(
children: [
Expanded(
child:Column(
mainAxisSize: MainAxisSize.max,
children: [
Text('Amount'),
TextField(),
],
)
),
Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Currency'),
TextField(),
],
)
],
)
),
//Second Row - Comment label/text box
]
)
)
);
}
我想知道我在这里是否缺少一些基本概念。我正在 Chrome 上测试它。
【问题讨论】:
标签: flutter flutter-layout flutter-web