首页 > 解决方案 > 抽屉没有出现在 SliverAppBar

问题描述

我想Drawer在我的代码里面添加一个,里面SliverAppBar没有任何错误,但是当我运行代码时,抽屉没有出现。

这是我的代码

body: CustomScrollView(
    slivers: <Widget>[
      SliverLayoutBuilder(
        builder: (BuildContext context, constraints) {
          final scrolled = constraints.scrollOffset > 50;
          final scrolledicon = constraints.scrollOffset > 0;

          return SliverAppBar(
            expandedHeight: 20,
            toolbarHeight: 65,
            leading: Padding(
              padding: const EdgeInsets.only(top: 2),
              child: Icon(DBIcons.logo, color: Colors.red, size: 50),
            ),
            actions: [
              Padding(
                padding: const EdgeInsets.all(10),
                child: IconButton(
                  icon: Icon(Icons.menu),
                  color: scrolledicon ? Colors.white : Colors.black,
                  onPressed: (){
                    Drawer(
                      child: Center(
                        child: Text('This is Drawer'),
                      ),
                    );
                  },
                ),
              ),
            ],
            backgroundColor: scrolled ? Colors.black : Colors.transparent,
            elevation: 0,
            pinned: true,
          );
        },
      ),

标签: flutterflutter-layoutdrawerlayout

解决方案


请看这里

在您的脚手架中,您必须注册一个抽屉

Scaffold(
    ...
    drawer: Drawer(child: Center(
                    child: Text('This is Drawer'),
    ),
    ...
  );

使用onPressed您将打开抽屉的请求发送到脚手架:

onPressed: () => Scaffold.of(context).openDrawer(),

推荐阅读