我刚刚开始使用 Flutter,我还不是专家。目前,当用户点击 NavigationDrawer 中的注销选项时,我试图显示一个对话框。
这个想法是显示一个带有一些文本的模态以及关闭模态或接受并继续注销过程的选项。我添加了一些代码来显示对话框,但不幸的是似乎缺少一些东西,我已经搜索了信息,但没有找到对我的案例有帮助的内容。
如代码片段所示,我收到了“使用不包含导航器的上下文请求的导航器操作”。错误,我不知道如何管理。
class NavigationDrawer extends StatefulWidget {
final GlobalKey<NavigatorState> navigator;
NavigationDrawer({this.navigator});
@override
_NavigationDrawerState createState() => _NavigationDrawerState();
}
class _NavigationDrawerState extends State<NavigationDrawer> {
String email = "";
@override
void initState() {
super.initState();
loadEmail();
}
void loadEmail() async {
HiveHelper.tokenBox = await Hive.openBox(tokenBox);
email = HiveHelper.email;
this.setState(() {});
}
@override
Widget build(BuildContext context) {
final state = RootDrawer.of(context);
return Drawer(
child: ListView(
children: [
DrawerHeader(
decoration: BoxDecoration(
image: DecorationImage(
image:
AssetImage('assets/images/navigation_bar_image.jpeg'),
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(
Colors.black.withOpacity(0.30), BlendMode.dstATop))),
child: Column(children: [
Padding(
padding: EdgeInsets.only(top: 25),
child: Icon(Icons.account_circle,
size: 70, color: Color.fromRGBO(166, 9, 61, 1)),
),
Padding(
padding: EdgeInsets.only(top: 15),
child: Text("$email",
style:
TextStyle(fontSize: 17, fontWeight: FontWeight.w500)))
])),
NavItem(
onTap: () {
widget.navigator.currentState
.push(MaterialPageRoute(builder: (context) => HomePage()));
state.close();
},
title: '${trans(context, "Home").toUpperCase()}',
icon: Icons.home_outlined,
),
NavItem(
title: '${trans(context, "Settings").toUpperCase()}',
icon: Icons.settings,
onTap: () {
widget.navigator.currentState.push(
MaterialPageRoute(builder: (context) => SettingsPage()));
state.close();
}),
NavItem(
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Confirm Sign out'),
content: Text('Are sure to sign out from app now?'),
actions: [
TextButton(
onPressed: () {
state.close();
},
child: Text('Cancel'),
),
TextButton(
onPressed: () async {
HiveHelper.tokenBox = await Hive.openBox(tokenBox);
HiveHelper.deleteToken();
widget.navigator.currentState.pushAndRemoveUntil(
MaterialPageRoute(
builder: (BuildContext context) =>
LoginScreen(),
),
(route) => false);
state.close();
},
child: Text('Yes and Confirm'),
)
],
);
});
},
title: '${trans(context, "Logout").toUpperCase()}',
icon: Icons.logout,
),
],
));
}
}
你能帮我吗?