0

I'm creating a widget that presents a Stack inside a Container. Inside the Stack there is a dynamic Column inside a Positioned widget, which sticks the Column to the Bottom.

I am trying to wrap the Stack in a SingleChildScrollView, that if the dynamic Column will have lots of children it will be able to scroll.

But after I add the scroll view I get this error message:

RenderStack object was given an infinite size during layout.

2 Answers 2

1

A scrollView doesn't have height, it will use infinite height for it's children. So the stack should be wrapped in a sizedBox widh a known height in case that you want that. If you want the stack to fill the screen then remove the singleChildScrollview completely. And if you want the stack to be scrollable from the inside. Just add SingleChildScrollView inside the stack

return Scaffold(
      body: Container(
        color: Colors.blue[100],
        child: Stack(
          children: [
            Positioned.fill(child: SingleChildScrollView(child: MyColumn()))
          ],
        ),
      ),
    );

EDIT 1: Ok the problem is the layout itself. Since you only use the stack to position the image. And you know the widht and height of the image, and the background shape fills all the items. We could

Isolate the stack only to the image, and use padding and some calculation to have the layout build in a similar way.

Then render everything else inside a normal column

My implementation https://dartpad.dev/230b14d8f0b22b45929a75c208786257

9
  • I have a few additional Positioned elements (images etc) that I want them to be scrolled when the Column is scrolled, so I need the scrollView to wrap the Stack itself. How can I make the Stack itself scrollable if I don't know the height (because the Column is dynamic)?
    – F.SO7
    Sep 25, 2020 at 15:05
  • Welp, that's quite difficult to figure out when the layout has multiple potential "infinite heighters". I would start by comennting everything else and using only the column. Then set the property mainAxisSize of the column to MainAxisSize.min .That should be the starting point. From there figure out if other widgets are giving infinite height
    – RegularGuy
    Sep 25, 2020 at 15:11
  • I still get the error when commenting everything except the Column and setting the mainAxisSize (only when using Positioned). Do you know why?
    – F.SO7
    Sep 25, 2020 at 15:15
  • Oh positioned might be stretching the column even with that. I really don't know where to look since the code is not enough. maybe add a top:0 to the positioned ?, the problem is not in stack, it is in your content
    – RegularGuy
    Sep 25, 2020 at 15:24
  • I've added the top: 0 but nothing changed... It happens even when I try to add just a simple Text widget inside a Positioned instead of the Column
    – F.SO7
    Sep 25, 2020 at 15:26
0

From the RenderStack documentation:

First, the non-positioned children (those with null values for top, right, bottom, and left) are laid out and initially placed in the upper-left corner of the stack. The stack is then sized to enclose all of the non-positioned children. If there are no non-positioned children, the stack becomes as large as possible.

Since all of your Stack's children are positioned, Stack will match the maximum size constraint passed down by the SingleChildScrollView. This size has infinite height, so the Stack is forced to have infinite height as well.

To fix this, you will need to find another way to accomplish what you want. One option that comes to mind involves a ConstrainedBox providing a minimum height (obtained from a LayoutBuilder), a non-positioned Column, and a bottomCenter alignment on the Stack itself. This is similar to the first code sample in the SingleChildScrollView documentation:

LayoutBuilder(
  builder: (context, constraints) {
    return SingleChildScrollView(
      child: ConstrainedBox(
        // This ensures that the Stack fills the entire viewport,
        // even if the Column is small.
        constraints: BoxConstraints(
          minHeight: constraints.maxHeight,
        ),
        child: Stack(
          // Place the only non-positioned child (the column) at the bottom
          alignment: Alignment.bottomCenter,
          children: [
            Column(
              mainAxisSize: MainAxisSize.min,
              children: [ /* ... */ ]
            )
          ]
        )
      )
    );
  }
)
6
  • Thank you, your explanation of the cause of the problem is very good. Can you please explain a bit more about the proposed solution? Because I did not quite understand it
    – F.SO7
    Sep 25, 2020 at 15:39
  • I've tried but now I get this error about the constrainedBox: RenderConstrainedBox object was given an infinite size during layout.
    – F.SO7
    Sep 25, 2020 at 15:52
  • Did you set the Column's mainAxisSize?
    – Nitrodon
    Sep 25, 2020 at 15:55
  • Yes, I did that, to mainAxisSize.min
    – F.SO7
    Sep 25, 2020 at 15:56
  • Do any of the Column's children have infinite height if unconstrained?
    – Nitrodon
    Sep 25, 2020 at 16:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.