Flutter: Prevent Margin Clipping
ListView
possess a padding
property for that purpose.
The following code will offset the first element by 8. But that padding won't clip the list content, as opposed to wrapping ListView
inside a Padding
.
ListView.builder(
padding: EdgeInsets.only(top: 8.0),
itemBuilder: (context, index) {
return Container(
height: 42.0,
color: index % 2 == 0 ? Colors.red : Colors.blue,
);
},
),
Josh Kahane
Updated on December 05, 2022Comments
-
Josh Kahane over 1 year
I have a horizontal
ListView.builder
. The image attached demonstrates my UI.The
listView
(red).itemBuilder
button widgets (blue); The margin (green).When the list is scrolled to the edge, without a margin, my button will be visibly against the edge of the screen.
If I add a
margin
orpadding
via aContainer
or theListView
, it moves my UI in as expected.However, when I scroll my list, items in the list are now clipped by this margin and do not scroll to the edge of the screen, but the margin boundary.
How can I have an inset that doesn't clip my list when scrolling?
-
Josh Kahane almost 6 yearsThis is not the case with horizontal scrolling and left/right padding.
-
Rémi Rousselet almost 6 yearsI tested it on both axis, with and without reverse, and it does work.
-
Rémi Rousselet almost 6 yearsIf it doesn't work for you either submit a bug or add your code here. Because that shouldn't be.
padding
oflistview
exists only to not clip it's children.