Search code examples
flutterlistviewdartflutter-layout

Scrollable Container inside a Column


I tried a few different approaches but I can't get this to work. The layout I want to achieve is really simple and it's a breeze to implement in native Android:

  • Fixed Container on top (blue)
  • Scrollable Container below (red). A ListView won't work in my case.

I tried to use SingleChildScrollView, but it doesn't seem to work inside a Column. Maybe I'm doing something wrong or I'm not using the right widgets...

My result:

enter image description here

Scaffold(
  body: Column(
    children: <Widget>[
      Container(
        height: 100.0,
        color: Colors.blue,
      ),
      SingleChildScrollView(
        child: Container(
          color: Colors.red,
          padding: EdgeInsets.all(20.0),
          child: Column(
            children: <Widget>[
              Text('Red container should be scrollable'),
              Container(
                width: double.infinity,
                height: 700.0,
                padding: EdgeInsets.all(10.0),
                color: Colors.white.withOpacity(0.7),
                child: Text('I will have a column here'),
              )
            ],
          ),
        ),
      ),
    ],
  ),
)

Solution

  • The problem is that Column widget does not support scrolling. In order to make it work you may switch to ListView, but current implementation lack of some sort of header for sections. In order to get them you may use sticky_headers package like that:

    Widget build(BuildContext context) => Scaffold(
          body: new ListView.builder(
              itemCount: 1,
              padding: EdgeInsets.zero,
              itemBuilder: (context, index) {
                return new StickyHeader(
                    header: Container(
                      height: 100.0,
                      color: Colors.blue,
                    ),
                    content: Container(
                      color: Colors.red,
                      padding: EdgeInsets.all(20.0),
                      child: Column(
                        children: <Widget>[
                          Text('Red container should be scrollable'),
                          Container(
                            width: double.infinity,
                            height: 700.0,
                            padding: EdgeInsets.all(10.0),
                            color: Colors.white.withOpacity(0.7),
                            child: Text('I will have a column here'),
                          )
                        ],
                      ),
                    ));
              }));