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:
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:
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'),
)
],
),
),
),
],
),
)
The other method to make a column scrollable is the use of ListView instead of the Column widget. ListView is a scrollable list of widgets arranged linearly and displays all its children one after another in the vertical or horizontal direction.
We can make the text scrollable in flutter using these 2 widgets: Expanded Class: A widget that expands a child of a Row, Column, or Flex so that the child fills the available space. SingleChildScrollView Class: A box in which a single widget can be scrolled.
I think that maybe after a year you have managed to do that.
But for others searching for the same problem, the easiest way is to wrap the SingleChildScrollView
inside an Expanded
widget.
Widget build(BuildContext context) =>
Scaffold(
body: Column(
children: <Widget>[
Container(
height: 100.0,
color: Colors.blue,
),
Expanded(
child: 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'),
)
],
),
),
),
),
],
),
);
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'),
)
],
),
));
}));
I managed to implement a working layout using a Stack
, the only down-side being that if I have a TextField
and I scroll down, the cursor 'bubble' shows up above my top container... which is kind of ugly. The order of my widgets in the Stack
doesn't affect this.
See screenshot
Widget build(BuildContext context) =>
Scaffold(
body: Stack(
children: <Widget>[
Container(
height: 100.0,
color: Colors.blue,
),
Container(
margin: EdgeInsets.only(top: 100.0),
child: SingleChildScrollView(
child: Container(
color: Colors.red,
padding: EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
Container(
width: double.infinity,
height: 700.0,
padding: EdgeInsets.all(10.0),
color: Colors.white.withOpacity(0.7),
child: TextField(),
)
],
),
),
),
),
],
),
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With