2

I have the following code

Column(
  crossAxisAlignment: CrossAxisAlignment.stretch,
  children: [
    TableHeadView(tablename: name, key: ValueKey(name + 'head')),
    Expanded(
      child: SingleChildScrollView(
        child: TableBodyView(
          tablename: name,
          key: ValueKey(name + 'body'),
        ),
      ),
    ),
  ],
),

which now works to scroll when the TableBodyView's height is larger than the available height, but when I removed the Expanded widget, I couldn't scroll and the running the app showed the scary '~pixels overflowd' warning.

I understand that constraints go down and size go up in flutter, but I am having a hard time trying to figure out how the Expanded widget does any effect, since even without the Expanded widget, the constraints that SingleChildScrollView gets is the same, which is the Column widget sayig 'hey SingleChildScrollView, you can have as much height as you want whatever is left from the TableHeadView used up'

5
  • 1
    use devtool's inspector to inspect the render tree and see the BoxConstraints that were applied to a particular RenderBox with and without Expanded widget, more: docs.flutter.dev/tools/devtools/inspector Commented Sep 19 at 11:30
  • 1
    but if you dont like such tools you can also use ConstraintsTransformBox or LayoutBuilder to see the incoming BoxConstraints Commented Sep 19 at 11:42
  • @pskink thank you that is not an answer to the question but I guess I can solve problems myself now Commented Sep 22 at 1:23
  • it is the answer to the question "... but I am having a hard time trying to figure out how the Expanded widget does any effect": check how Expanded changes BoxConstraints passed to its child widget Commented Sep 22 at 4:37
  • @pskink yes you taught me how to catch the fish thx Commented Sep 22 at 8:27

1 Answer 1

2

When dealing with a scroll view inside a Column, it’s important to separate two ideas: the height of the scroll view itself (meaning how tall its visible container is) and the scrollable size (the total height of the content inside it).

The scroll view’s container height is limited by how much space it’s allowed to take—in your case, that’s the height left over after the header (TableHeadView). This height is fixed and bounded when you use Expanded.

However, the scrollable size can be much bigger because the content inside the scroll view might be very tall—like a long list or a table.

The scroll view only starts scrolling when its container height (the visible area) is reached, but the scrollable content inside is still bigger than that. Without something like Expanded to limit the container’s height, the scroll view thinks it can keep growing infinitely, which leads to the overflow error.

So, by wrapping your SingleChildScrollView in Expanded, you’re telling it, “Here’s your actual container height limit,” which lets it scroll properly when the internal content is taller than that limit without causing layout errors.

So, in short
- With expanded -> The scrollView has a maxHeight constraint set to the remaining space
- Without expanded -> The scrollView has no maxHeight and try to grow indefinitely as a big container with no scroll

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.