Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected top padding in ListView put inside scaffold with no appBar #14842

Closed
radzish opened this issue Feb 23, 2018 · 22 comments · Fixed by #60726
Closed

Unexpected top padding in ListView put inside scaffold with no appBar #14842

radzish opened this issue Feb 23, 2018 · 22 comments · Fixed by #60726
Assignees
Labels
customer: crowd Affects or could affect many people, though not necessarily a specific customer. d: api docs Issues with https://api.flutter.dev/ f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. P2 Important issues not at the top of the work list waiting for PR to land (fixed) A fix is in flight

Comments

@radzish
Copy link

radzish commented Feb 23, 2018

If you put ListView somewhere inside Scaffold having no appBar, ListView has top padding. There is no padding if appBar is present. There is no padding if you specify EdgeInsets.zero padding for ListView explicitly. See following code snipped for example

...
    return new MaterialApp(
      home: new Scaffold(
//        appBar: new AppBar(
//          title: new Text("test"),
//        ),
        body: new Column(
          children: <Widget>[
            new Container(
              child: new Center(
                  child: new Text("some top widget")
              ),
              decoration: new BoxDecoration(
                color: Colors.red,
              ),
              height: 100.0,
            ),
            new Expanded(
              child: new ListView(
                children: items,
//                padding: EdgeInsets.zero, // explicit padding as a workaround
              ),
            ),
          ],
        ),
      ),
    );
...

See screenshot:

bug

Flutter Doctor:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (on Linux, locale en_US.UTF-8, channel master)
[✓] Android toolchain - develop for Android devices (Android SDK 27.0.3)
[✓] Android Studio (version 3.0)
[!] IntelliJ IDEA Ultimate Edition (version 2017.3)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.20.1)
[✓] Connected devices
@cbracken cbracken added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Mar 1, 2018
@cbracken
Copy link
Member

cbracken commented Mar 1, 2018

/cc @HansMuller @Hixie

@itsJoKr
Copy link

itsJoKr commented Mar 1, 2018

Just had this problem, top SliverPadding is set to 20.0 by default. Looking at docs I see:

/// By default, [ListView] will automatically pad the list's scrollable
/// extremities to avoid partial obstructions indicated by [MediaQuery]'s
/// padding. To avoid this behavior, override with a zero [padding] property.

@Hixie
Copy link
Contributor

Hixie commented Mar 8, 2018

Yeah this is intentional. If you put a widget before the ListView, you should wrap the ListView with a MediaQuery.removePadding widget (with removeTop: true).

@radzish
Copy link
Author

radzish commented Mar 8, 2018

Well, as long as this is documented I will close the issue

@radzish radzish closed this as completed Mar 8, 2018
@Hixie
Copy link
Contributor

Hixie commented Mar 8, 2018

I'm not sure it's well-documented yet. If you can't find it in the documentation, please don't hesitate to reopen.

@Hixie Hixie added the d: api docs Issues with https://api.flutter.dev/ label Mar 8, 2018
@sergiandreplace
Copy link

I had this same issue and after some search this is the only exact answer I've found. Maybe is worth to create a short entry on documentation about window measurement and specific cases

@Jasonthefirst
Copy link

Found this topic because I thought this is maybe a bug or I did oversee something. So maybe some more info is needed. (I would prefer a method to turn this off inside the listview, too. This would be easier.)

@tiagosito
Copy link

I did not find it in the documentation, is it there?

@zoechi
Copy link
Contributor

zoechi commented Aug 31, 2018

I assume this should not be closed because @Hixie added the d: api docs label after the issue was closed.

@dimrsilva
Copy link

https://api.flutter.dev/flutter/widgets/ListView-class.html

The docs now have a comment about this behavior:

By default, ListView will automatically pad the list's scrollable extremities to avoid partial obstructions indicated by MediaQuery's padding. To avoid this behavior, override with a zero padding property.

I think it can be closed.

@jigarpatel17
Copy link

Yeah this is intentional. If you put a widget before the ListView, you should wrap the ListView with a MediaQuery.removePadding widget (with removeTop: true).

This solution is amazing. Thank you so much.

@blokberg
Copy link

blokberg commented Aug 11, 2019

Yeah this is intentional. If you put a widget before the ListView, you should wrap the ListView with a MediaQuery.removePadding widget (with removeTop: true).

Thank you very much!
This code solved my problem:

Widget _myListView(){
  return MediaQuery.removePadding(
    context: context,
    removeTop: true,
    child: ListView.builder(
     .......
    )
  );
}

@j3g
Copy link

j3g commented Aug 20, 2019

I do not feel the documentation is helpful. I would be lost without an example line of code.
I did this to remove all padding. Then pad each of your rows using container widget. I'm not an expert at all so please advise.
padding: const EdgeInsets.all(0.0)

@UttamPanchasara
Copy link

We can also fix this by wrapping whole Scaffold body part in SafeArea if there is no Appbar in your screen.

return Scaffold(
      body: SafeArea(
        child: Container(
          child: ListView.builder(
          .......
          );
        ),
      ),
    );

@blunderous
Copy link

blunderous commented Mar 12, 2020

The solution that worked for me was to set the ListView padding to zero as here

child: ListView.builder(
    padding: EdgeInsets.zero,
    itemBuilder: (context, index) {
        return Card(...);
    },
),

Most likely this is not the most recommended way, but since I can define the size of the ListView through its parent that is a Container, and the Card() widget has its own defined size. One of most likely many solutions for this problem. Felt like sharing it as none of the options above suited my situation.

@TahaTesser TahaTesser added customer: crowd Affects or could affect many people, though not necessarily a specific customer. documentation labels Mar 25, 2020
tuarrep pushed a commit to OwnWeb/dotted_line that referenced this issue Apr 30, 2020
@kf6gpe kf6gpe added the P2 Important issues not at the top of the work list label May 29, 2020
@Piinks Piinks self-assigned this Jul 2, 2020
@Piinks Piinks modified the milestones: [DEPRECATED] Goals, 1.21 - July 2020 Jul 2, 2020
@Piinks Piinks added the waiting for PR to land (fixed) A fix is in flight label Jul 2, 2020
@douglasrosa0110
Copy link

douglasrosa0110 commented Aug 25, 2020

Yeah this is intentional. If you put a widget before the ListView, you should wrap the ListView with a MediaQuery.removePadding widget (with removeTop: true).

Tks a lot! This applies too when we need to show one scaffold into another scaffold

@hovanhung98hust
Copy link

Just had this problem, top SliverPadding is set to 20.0 by default. Looking at docs I see:

/// By default, [ListView] will automatically pad the list's scrollable
/// extremities to avoid partial obstructions indicated by [MediaQuery]'s
/// padding. To avoid this behavior, override with a zero [padding] property.

thanks, this work for me

@inoas
Copy link

inoas commented Oct 29, 2020

This was really an unexpected implicit and hidden that made you doubt your own sanity (until adding padding: EdgeInsets.zero).

@Hixie (the guy who thought me CSS box model on Freenode IRC about 18-20yrs ago <3) :
Is this setting padding: okay or ist a wrapping in MediaQuery.removePadding(... recommended/must-have. Latter is much more code and convoluted to the uninitiated eyes.

@Hixie
Copy link
Contributor

Hixie commented Oct 29, 2020

Once you know about SafeArea it makes more sense. The problem is that in trying to make SafeArea work automatically without having to think about it, we also require that in cases where you do things it can't handle you have to handle it. I don't think we have a good solution to this other than improving the docs. If this is still confusing then we should add another section to the "troubleshooting" part of the Scaffold docs. Please do file a new bug if you'd like us to do that.

@nox-machina
Copy link

Yeah this is intentional. If you put a widget before the ListView, you should wrap the ListView with a MediaQuery.removePadding widget (with removeTop: true).

Well that was a life saver, thanks.

@kamleshwebtech
Copy link

Yeah this is intentional. If you put a widget before the ListView, you should wrap the ListView with a MediaQuery.removePadding widget (with removeTop: true).

This solution helped me and removed my top space of GridView.builder widget:

Container(
            child: MediaQuery.removePadding(
              removeTop: true,
              context: context,
              child: GridView.builder(

Happy to get your help. Thanks dear.

@github-actions
Copy link

github-actions bot commented Aug 1, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
customer: crowd Affects or could affect many people, though not necessarily a specific customer. d: api docs Issues with https://api.flutter.dev/ f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. P2 Important issues not at the top of the work list waiting for PR to land (fixed) A fix is in flight
Projects
None yet
Development

Successfully merging a pull request may close this issue.