Flutter NestedScrollView / SliverAppBar-不需要的底边距

时间:2019-02-06 12:06:14

标签: dart flutter android-collapsingtoolbarlayout margins

要使用导航抽屉构建应用栏,

@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)
            ],
          ),
        ));

结果:

enter image description here

因此,刚好在身体上方有多余的白边。如果我尝试使用常规应用栏,则可以这样:

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),
    );

结果很好,没有任何多余的利润:

enter image description here

如何删除多余的多余边距并在扩展模式下将标题向左对齐(应将其放置在汉堡菜单指示器下方)。

DrawerItem类定义:

class DrawerItem {
  String title;
  IconData icon;

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

drawerItems的定义如下:

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

2 个答案:

答案 0 :(得分:4)

我在使用嵌套滚动视图时遇到了同样的问题。我使用网格视图作为正文并将其填充设置为0摆脱了空间

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

因此,从该方法_getDrawerItemWidget(_selectedDrawerIndex)返回的所有小部件,都应尝试向其添加填充。

答案 1 :(得分:1)

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

更多信息:https://github.com/flutter/flutter/issues/14842