我正在尝试创建 Stepper,这是我的代码,我从 flutter doc 复制粘贴:
class AdFormView extends GetView<AdFormController> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stepper'),
centerTitle: true,
),
body: Center(
child: MyStatefulWidget(),
),
);
}
}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
MyStatefulWidget({Key key}) : super(key: key);
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _index = 0;
Widget build(BuildContext context) {
return Container(
height: 300,
width: 300,
child: Stepper(
currentStep: _index,
onStepCancel: () {
if (_index <= 0) {
return;
}
setState(() {
_index--;
});
},
onStepContinue: () {
if (_index >= 1) {
return;
}
setState(() {
_index++;
});
},
onStepTapped: (index) {
setState(() {
_index = index;
});
},
steps: [
Step(
title: Text("Step 1 title"),
content: Container(
height: 400,
width: 400,
alignment: Alignment.centerLeft,
child: Text("Content for Step 1")),
),
Step(
title: Text("Step 2 title"),
content: Container(
height: 400,
width: 400,
child: Text("Content for Step 2"),
),
),
],
),
);
}
}
但是我遇到了这个错误:
The following assertion was thrown during performLayout():
BoxConstraints forces an infinite width.
Stepper
The following RenderObject was being processed when the exception was fired: RenderConstrainedBox#79f3a relayoutBoundary=up19 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
RenderObject: RenderConstrainedBox#79f3a relayoutBoundary=up19 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
parentData: offset=Offset(0.0, 0.0) (can use size)
constraints: BoxConstraints(0.0<=w<=Infinity, 0.0<=h<=48.0)
size: MISSING
additionalConstraints: BoxConstraints(w=Infinity, 0.0<=h<=Infinity)
child: RenderPhysicalShape#0b08e NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
needs compositing
parentData: <none>
constraints: MISSING
size: MISSING
elevation: 0.0
color: Color(0xff002c7f)
shadowColor: Color(0xff002c7f)
clipper: ShapeBorderClipper
child: RenderCustomPaint#df4fd NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
parentData: <none>
constraints: MISSING
size: MISSING
child: _RenderInkFeatures#570e6 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
parentData: <none>
constraints: MISSING
size: MISSING
child: RenderSemanticsAnnotations#f05ee NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
parentData: <none>
constraints: MISSING
size: MISSING
父级有大小,为什么它显示 MISSING? 我尝试添加最大和最小宽度和高度,并尝试将其包裹在灵活中,但没有任何效果!
我创建了一个新项目来测试 Stepper, 我创建了一个正常的颤振和一个使用 GetX 它们运行良好。
我不知道为什么它在我的项目中不起作用,我将代码复制粘贴,我不知道为什么它无法读取父级的大小,我添加了最大宽度,最小宽度,最大高度, 最小高度、宽度和高度并且不起作用。
所以它不仅仅适用于我的项目,如下图所示:
答案 0 :(得分:0)
MyStatefulWidget
上没有此类错误,您可以使用 flutter clean
重建项目,然后运行。这个问题可能是由父级引起的。