user1209216
user1209216

Reputation: 7914

Flutter NestedScrollView/SliverAppBar - unwanted bottom margin

To build appbar with Navigation Drawer:

@override
  Widget build(BuildContext context) {
    var drawerOptions = <Widget>[];
    for (var i = 0; i < widget.drawerItems.length; i++) {
      var d = widget.drawerItems[i];
      drawerOptions.add(new ListTile(
        leading: new Icon(d.icon),
        title: new Text(d.title),
        selected: i == _selectedDrawerIndex,
        onTap: () => _onSelectItem(i),
      ));
    }
    drawerOptions.add(
        new RaisedButton(onPressed: () => {}, child: new Text("Ustawienia")));

    return new Scaffold(
        body: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverAppBar(
                expandedHeight: 130.0,
                floating: false,
                pinned: true,
                flexibleSpace: FlexibleSpaceBar(
                  title:
                      new Text(widget.drawerItems[_selectedDrawerIndex].title),
                ),
              ),
            ];
          },
          body: _getDrawerItemWidget(_selectedDrawerIndex),
        ),
        drawer: new Drawer(
          child: new Column(
            children: <Widget>[
              new UserAccountsDrawerHeader(
                  accountName: new Text("Gość"), accountEmail: null),
              new Column(children: drawerOptions)
            ],
          ),
        ));

Result:

enter image description here

So there is unwanted white margin just above body. If I try with regular appbar, this way:

return new Scaffold(
      appBar: new AppBar(
        // here we display the title corresponding to the fragment
        // you can instead choose to have a static title
        title: new Text(widget.drawerItems[_selectedDrawerIndex].title),
      ),
      drawer: new Drawer(
        child: new Column(
          children: <Widget>[
            new UserAccountsDrawerHeader(
                accountName: new Text("Gość"), accountEmail: null),
            new Column(children: drawerOptions)
          ],
        ),
      ),
      body: _getDrawerItemWidget(_selectedDrawerIndex),
    );

Result is fine, no any extra margin:

enter image description here

How to remove unwanted extra margin and align title to left in expanded mode (it should be placed under hamburger menu indicator).

DrawerItem class definition:

class DrawerItem {
  String title;
  IconData icon;

  DrawerItem(this.title, this.icon);
}

drawerItems is defined as follows:

final drawerItems = [
    new DrawerItem("Option1", Icons.list),
    new DrawerItem("Option2", Icons.event_note),
    new DrawerItem("Option3", Icons.account_balance),
   //... etc
  ];

Upvotes: 4

Views: 8868

Answers (2)

blokberg
blokberg

Reputation: 994

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

more information : https://github.com/flutter/flutter/issues/14842

Upvotes: 9

Gaurav Pingale
Gaurav Pingale

Reputation: 454

I was facing the same issue with a nested scroll view. I was using a grid view as body and setting its padding to 0 got rid of the space

GridView.builder(
   padding: EdgeInsets.only(top: 0),

so whatever widgets you are returnign from this method _getDrawerItemWidget(_selectedDrawerIndex), try adding padding to it.

Upvotes: 19

Related Questions