I'm looking to create a simple layout using a scrollable ListView, with an immovable header tile. I've placed the header tile and ListView into a Column, so the Header can be above the ListView without scrolling off the screen. However, when scrolling the ListView, my header tile seems to take on the color of whatever ListView tiles are scrolling "under" it.
On startup, the app looks like this:
However if we scroll half a tile down, the green color of the list tiles appears to push out the red from the header. The text from the green tiles does not have the same problem, and is properly occluded by the header tile
Minimal code for reconstruction
void main() => runApp(MyTestApp2());
class MyTestApp2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("AppBar Header"),
),
body: Column(children: <Widget>[
ListTile(
title: Center(child: Text("This Header Should be Red")),
tileColor: Colors.redAccent,
),
Expanded(child: ListView(
children: List.generate(20, (int index) => "List Item $index")
.map((n) => ListTile(
title: Text(n.toString()),
tileColor: Colors.lightGreen))
.toList(),
)),
])),
);
}
}
What is happening here? I could probably achieve this layout using different Widgets, but I would like to know Why is this color bleed effect occurring? How does it fit with the box layout model Flutter uses?
EDIT: The immediate problem can be solved by wrapping the red ListTile in a Container widget, and setting the color property to be red on that container, like this:
Container(
color: Colors.redAccent,
child: ListTile(....
However I would still like to know what is going on in terms of the layout algorithm in the original code, if anybody knows. Shouldn't the existence of the header tile prevent our listview from pushing its elements into the area owned by the red ListTile?