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