单击 [Flutter] 时不会打开 DropdownMenu

问题描述

我的导航栏有问题。每当我单击导航栏中的管理下拉菜单时,都会出错

使用不包含导航器的上下文请求导航器操作。

enter image description here

main.dart

MyStyledToast(
    child: BlocBuilder<AuthBloc,AuthState>(
      builder: (context,state) {
        return MaterialApp(
          title: 'CRM',debugShowCheckedModeBanner: false,builder: (context,child) => LayoutTemplate(child: child),navigatorKey: locator<NavigationService>().navigatorKey,onGenerateRoute: generateRoute,initialRoute:
              (state is AuthenticatedState) ? UsersRoute : LoginRoute,theme: themeData,);
      },),

nav_bar.dart

Container(
  height: 60,decoration: Boxdecoration(
    color: Colors.white,BoxShadow: [
      BoxShadow(
        color: Colors.grey.withOpacity(0.5),spreadRadius: 2,blurRadius: 8,offset: Offset(0,-4),// changes position of shadow
      ),],padding: EdgeInsets.symmetric(horizontal: 20),child: Row(
    crossAxisAlignment: CrossAxisAlignment.center,mainAxisAlignment: MainAxisAlignment.spaceBetween,children: <Widget>[
      Row(
        mainAxisSize: MainAxisSize.min,children: <Widget>[
          NavBarlogo(),SizedBox(
            width: 80,NavBarItem('Home',HomeRoute),SizedBox(
            width: 40,NavBarItem('Customers',CustomersRoute),NavBarItem('Tickets',TicketsRoute),NavBarItem('Reports',ReportsRoute),NavBarItem('Dashboard',DashboardRoute),DropdownButtonHideUnderline(
            child: DropdownButton<String>(
              iconSize: 24,elevation: 16,hint: Text(
                'Admin',style: Theme.of(context).textTheme.headline1,onChanged: (String newValue) {
                if (newValue == 'Users') {
                  context.read<UserBloc>().add(LoadDataEvent());
                  locator<NavigationService>()
                      .navigateReplaceto(UsersRoute);
                } else if (newValue == 'Roles') {
                  locator<NavigationService>()
                      .navigateReplaceto(RolesRoute);
                }
              },items: <String>[
                'Users','Roles',].map<DropdownMenuItem<String>>((String value) {
                return DropdownMenuItem<String>(
                  value: value,child: Text(value),);
              }).toList(),Row(
        children: [
          NavBarIcon(Icons.notifications_none,SizedBox(
            width: 5,NavBarIcon(Icons.person_outline,ProfileRoute),);

navigation_service.dart

class NavigationService {
 final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

 Future<dynamic> navigateto(String routeName) {
  return navigatorKey.currentState.pushNamed(routeName);
 }

 Future<dynamic> navigateReplaceto(String routeName) {
  return navigatorKey.currentState.pushReplacementNamed(routeName);
 }

 Future<dynamic> navigateReplaceAllTo(String routeName) {
   return navigatorKey.currentState
    .pushNamedAndRemoveUntil(routeName,(route) => false);
 }

 void goBack() {
   return navigatorKey.currentState.pop();
 }

 void popDialog(BuildContext context) {
   return Navigator.pop(context);
 }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)