Flutter: Prevent Margin Clipping

8,498

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,
        );
      },
    ),
Share:
8,498
Josh Kahane
Author by

Josh Kahane

Updated on December 05, 2022

Comments

  • Josh Kahane
    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).

    enter image description here

    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 or padding via a Container or the ListView, 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
    Josh Kahane almost 6 years
    This is not the case with horizontal scrolling and left/right padding.
  • Rémi Rousselet
    Rémi Rousselet almost 6 years
    I tested it on both axis, with and without reverse, and it does work.
  • Rémi Rousselet
    Rémi Rousselet almost 6 years
    If it doesn't work for you either submit a bug or add your code here. Because that shouldn't be. padding of listview exists only to not clip it's children.