user8741906
user8741906

Reputation:

Make a Column with an Expanded Widget scrollable

I want to make a infinit height column (because of expanded widget) scrollable, but a SingleChildScrollView does not do the job.

new Scaffold(
    backgroundColor: Color.fromRGBO(245, 245, 245, 1.0),
    body: new Container(
      child: SingleChildScrollView(
        child: new Column(
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            header,
            new Expanded(
              child: pageView,
            )
          ],
        )
      )
    ),
  )

Error Message:

The following assertion was thrown during performLayout(): RenderFlex children have non-zero flex but incoming height constraints are unbounded. When a column is in a parent that does not provide a finite height constraint, for example if it is in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining space in the vertical direction. These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child cannot simultaneously expand to fit its parent. Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible children (using Flexible rather than Expanded). This will allow the flexible children to size themselves to less than the infinite remaining space they would otherwise be forced to take, and then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum constraints provided by the parent.

Upvotes: 4

Views: 11159

Answers (3)

ejabu
ejabu

Reputation: 3147

1. Problems

  • I want to make a infinit height column (because of expanded widget) scrollable, but a SingleChildScrollView does not do the job.

  • it does not crash, but if the height is set to the screen height I cannot scroll and I dont want to set a specific height (dont know the page hight + dont want emtpy space)

  • If I add a pageview inside the column console returns: RenderViewport does not support returning intrinsic dimensions

2. Demo

Google Search Result

3. Replace SingleChildScrollView

rather than :

child: SingleChildScrollView(  // need to be replaced
  child: new Column(
    mainAxisSize: MainAxisSize.max,
    children: <Widget>[
      header,
      new Expanded(
        child: pageView,
      )
    ],
  )
)

write this :

child: CustomScrollView( // replace by this
  slivers: <Widget>[
    header(context),
    pageView(context),
  ],
),

4. Rather than Wrap Expanded, Wrap with SliverList and SizedBox

I dont want to set a specific height

Then having SizedBox is mandatory. As the screen must be scrollable, each of its component must have fixed height.

This enables app to render multiple another PageView or Header.

Note : wrapping contents by SliverList produces scrollable Screen both horizontally and vertically

Widget pageView(BuildContext context) {
  final section = SliverList(
    delegate: SliverChildListDelegate(
      [
        SizedBox(
          height: 100,
          child: PageView(
            children: <Widget>[
              AddTextInput(),
              AddNoteSettings(),
            ],
          ),
        ),
      ],
    ),
  );
  return section;
}

Widget headerContent(BuildContext context) {
  final section = SliverList(
    delegate: SliverChildListDelegate(
      [
        NewsFeed(),
      ],
    ),
  );
  return section;
}

5. Full working Repo

You may look into this repository and build it locally. Github

Upvotes: 2

aswin darma
aswin darma

Reputation: 438

Column widget is already an infinite height widget, you can add any widget as you like inside the Column, and the height will always follow the maximum height of the children. So you don't need to add Expanded widget anymore. An expanded widget can only be assigned to a fixed size widget like Container with fixed height/width.

Upvotes: 0

Emil Djupvik
Emil Djupvik

Reputation: 111

try this:

new Scaffold(
backgroundColor: Color.fromRGBO(245, 245, 245, 1.0),
body: new Container(
  child: SingleChildScrollView(
    child: Sizedbox(
      height: MediaQuery.of(context).size.height // or something simular :)
      child: new Column(
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          header,
          new Expanded(
            child: pageView,
          ),
        ],
      ),
    ),
  ),
),

),

Upvotes: 0

Related Questions