如何为SliverAppBar添加抽屉

时间:2018-12-31 11:50:49

标签: dart flutter flutter-layout flutter-sliver

这是我的代码,复制自here

SliverAppBar(
  expandedHeight: 150.0,
  flexibleSpace: const FlexibleSpaceBar(
      title: Text('Available seats'),
    ),
    actions: < Widget > [
      IconButton(
        icon: const Icon(Icons.add_circle),
          tooltip: 'Add new entry',
          onPressed: () { /* ... */ },
      ),
    ]
)

但是我需要添加一个Drawer。我该怎么办?
我正在尝试在Flutter中重建我的应用。
Converting java android app to flutter

我替换了图标,但是如何创建Drawer

leading: IconButton(icon: Icon( Icons.menu ),onPressed: ()=>{},)

我的完整代码

@override
  Widget build(BuildContext context) {

  return NestedScrollView(
      headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return <Widget>[
          SliverAppBar(
            leading: IconButton(
              icon: Icon(Icons.menu),
              onPressed: () => {},
            ),
            actions: <Widget>[
              IconButton(
                onPressed: () => {},
                icon: Icon(Icons.shopping_cart),
              )
            ],
            expandedHeight: 200.0,
            floating: false,
            pinned: true,
            flexibleSpace: FlexibleSpaceBar(
                centerTitle: true,
                title: Text("My Pet Shop",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16.0,
                    )),
                background: Image.network(
                  "https://firebasestorage.googleapis.com/v0/b/my-pet-world.appspot.com/o/images%2Fbannerads%2FxTmAblJI7fMF1l0nUa1gM32Kh9z1%2F734rm6w7bxznp%2FPETWORLD-HOME-SLIDER-KITTENS.webp?alt=media&token=cf7f48bb-6621-47b3-b3f8-d8b36fa89715",
                  fit: BoxFit.cover,
                )),
          ),
        ];
      },
      body: Container(
        margin: EdgeInsets.only(top: 0),
        child: Column(
          children: <Widget>[
            Expanded(
              //getHomePageWidget()
              child: ListView(
                padding: EdgeInsets.all(0.0),
                children: <Widget>[getHomePageWidget()],
              ),
            )
          ],
        ),
      ));
 }

1 个答案:

答案 0 :(得分:4)

抽屉是脚手架的属性。设置抽屉属性后,菜单图标将自动出现在SliverAppBar中。

将其返回到您的构建方法中,您将获得所需的内容。

  return Scaffold(
    body: CustomScrollView(
      slivers: <Widget>[
        SliverAppBar(
          expandedHeight: 200.0,
          flexibleSpace: const FlexibleSpaceBar(
            title: Text('Available seats'),
          ),
          actions: <Widget>[
            IconButton(
              icon: const Icon(Icons.add_circle),
              tooltip: 'Add new entry',
              onPressed: () {},
            ),
          ],
        ),
        SliverList(
          delegate: SliverChildListDelegate([
            getHomePageWidget(),
          ]),
        ),
      ],
    ),
    drawer: Drawer(),
  );