Flutter Stepper - BoxConstraints 强制无限宽度

Posted

技术标签:

【中文标题】Flutter Stepper - BoxConstraints 强制无限宽度【英文标题】:Flutter Stepper - BoxConstraints forces an infinite width 【发布时间】:2021-05-03 10:27:15 【问题描述】:

我尝试创建 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? 我尝试添加最大和最小宽度和高度,并尝试将其包裹在 Flexible 中,但没有任何效果!

我创建了一个新项目来测试步进器, 我用 GetX 创建了一个正常的颤振和一个 他们工作正常。

我不知道为什么它在我的项目中不起作用,我把代码复制粘贴,我不知道为什么它无法读取父级的大小,我添加了最大宽度,最小宽度,最大高度,最小高度,宽度和高度,不工作。

所以它不仅仅在我的项目中工作,如下图所示:

【问题讨论】:

如果它在一个全新的 getx 项目中工作,那么你的项目中存在问题。我假设您已经清理了缓存,完成了flutter clean,包获取,重建等。如果是,但仍然失败,请将所有文件(以及对清单文件、ios plist 文件等的更改)复制到新项目并查看如果它可以工作。还有,你没有提到flutter版本,getx版本,如果是ios/web/macos等。 【参考方案1】:

MyStatefulWidget 上没有此类错误,您可以使用flutter clean 重建项目然后运行。这个问题可能是由父级引起的。

【讨论】:

以上是关于Flutter Stepper - BoxConstraints 强制无限宽度的主要内容,如果未能解决你的问题,请参考以下文章

Flutter Stepper - BoxConstraints 强制无限宽度

如何在 FLUTTER 的 stepper 中显示一个列表

组件之间传递数据。 flutter中在Stepper中传递数据

Flutter 自定义实现时间轴侧边进度条

Flutter 自定义单选按钮

微信小程序 自定义组件(stepper)