Flutter drawer side sliding implementation 2 (by clicking on the real display side sliding bar)

1. First add the following code to the page that needs to be used

GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();

2. Add key in Scaffold

 key: scaffoldKey,

3. Add the following code where the side slide bar needs to be displayed

scaffoldKey.currentState!.openDrawer();

4. The overall page code is provided

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(''),
    );
  }
}

 

Guess you like

Origin blog.csdn.net/as425017946/article/details/128187723