Abdallah El-Rashedy
Abdallah El-Rashedy

Reputation: 871

Flutter Stepper - BoxConstraints forces an infinite width

I trying to create Stepper, This is my code and I took it copy paste from 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"),
            ),
          ),
        ],
      ),
    );
  }
}

But I got this Error:

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

The parent have size, so why It's shows MISSING? I tried to add max and min width and height and I tried to wrap It in Flexible but nothing works!

I created a new project for test the Stepper, I created one normal flutter and one with GetX and they works fine.

I don't know why It's not works in my project, I took the code copy paste, I don't know why it's can't read the size of the parent, I added max width, min width, max height, min height, width and height and not working.

So It's not working only in my project like the image below:

enter image description here enter image description here

Upvotes: 0

Views: 558

Answers (2)

bucgebaha
bucgebaha

Reputation: 3

I know this is an old post, but I just encountered this problem, and it took me some time to solve it.

Here’s the deal: I’m using a custom theme, and in that theme, text buttons have infinite width. As a result, the Stepper widget tries to use text buttons, but it throws an exception due to the infinite width.

To fix this, either modify your theme or use controlsBuilder to create custom buttons.

Upvotes: 0

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63769

There is no such error on MyStatefulWidget you can rebuild the project using flutter clean then run. this problem can be cause by parent level.

Upvotes: 0

Related Questions