DropdownMenu не открывается при нажатии [Flutter]

У меня проблема с панелью навигации. Всякий раз, когда я нажимаю раскрывающийся список администратора на панели навигации, появляется ошибка

Операция навигатора запрошена с контекстом, который не включает навигатор.

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),
          SizedBox(
            width: 40,
          ),
          NavBarItem('Tickets', TicketsRoute),
          SizedBox(
            width: 40,
          ),
          NavBarItem('Reports', ReportsRoute),
          SizedBox(
            width: 40,
          ),
          NavBarItem('Dashboard', DashboardRoute),
          SizedBox(
            width: 40,
          ),
          DropdownButtonHideUnderline(
            child: DropdownButton<String>(
              iconSize: 24,
              elevation: 16,
              hint: Text(
                'Admin',
                style: Theme.of(context).textTheme.headline1,
              ),
              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, HomeRoute),
          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);
 }
}

0 ответов

Другие вопросы по тегам