Flutter drawer 侧滑实现二(通过点击现实显示侧滑栏)

1.首先在需要使用的页面加入下方代码

GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();

2.在Scaffold里面加入key

 key: scaffoldKey,

3.在需要展示侧滑栏的地方加入下方代码

scaffoldKey.currentState!.openDrawer();

4.整体页面代码奉上

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // ignore: prefer_typing_uninitialized_variables
  var appBar;
  GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();

  @override
  void initState() {
    super.initState();
    appBar = WidgetUtils.getAppBar('首页', false, context, false);
    ///首页点击侧滑
    eventBus.on<HomeBack>().listen((event) {
      if(event.isBack){
        scaffoldKey.currentState!.openDrawer();
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar,
      key: scaffoldKey,
      drawer: Drawer(
        child: Column(
          children: [
            SizedBox(
              height: ConfigScreenUtil.autoHeight400,
              width: double.infinity,
              child: const DrawerHeader( // drawer的头部控件
                decoration: BoxDecoration(
                  color: MyColors.blue,
                ),
                child: UnconstrainedBox( // 解除父级的大小限制
                    child:CircleAvatar(
                      radius: 40,
                      backgroundColor: Colors.transparent,
                      backgroundImage:  AssetImage('assets/images/ic_mine_photo_def.png'),
                    )
                ),
              ),
            ),
            WidgetUtils.whiteKuang('assets/images/login_password.png', '修改密码', false),
            WidgetUtils.commonSizedBox(1, 0),
            WidgetUtils.whiteKuang('assets/images/login_password.png', '检查版本', true),
          ],
        ),
      ),
      body: Text(''),
    );
  }
}

猜你喜欢

转载自blog.csdn.net/as425017946/article/details/128187723
今日推荐