Need help to close appbar drawer in flutter mobile web. Error: Navigator operation requested with a context that does not include a Navigator

Viewed 44

I'm working on a flutter web project. I've created a sticky app bar using the below code. Now when I'm trying to navigate using side drawer, it doesn't pop (closes) and I'm getting error : "Error: Navigator operation requested with a context that does not include a Navigator. The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget". I'm using NavigatorService for this.

void main() {
  setupLocator();

  WidgetsFlutterBinding.ensureInitialized();
  Firebase.initializeApp();
  runApp(
    RestartWidget(
      child: App(),
    ),
  );
}

class RestartWidget extends StatefulWidget {
  RestartWidget({this.child});

  final Widget child;

  static void restartApp(BuildContext context) {
    context.findAncestorStateOfType<_RestartWidgetState>().restartApp();
  }

  @override
  _RestartWidgetState createState() => _RestartWidgetState();
}

class _RestartWidgetState extends State<RestartWidget> {
  Key key = UniqueKey();

  void restartApp() {
    setState(() {
      key = UniqueKey();
    });
  }

  @override
  Widget build(BuildContext context) {
    return KeyedSubtree(
      key: key,
      child: widget.child,
    );
  }
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ThemeProvider(
      initTheme: lightTheme,
      child: MultiProvider(
        providers: [
          FutureProvider<SharedPreferences>.value(
            value: SharedPreferences.getInstance(),
            catchError: (_, e) {
              return null;
            },
          ),
          StreamProvider<UserData>.value(
            value: DatabaseService().documentStream,
            catchError: (_, e) {
              return null;
            },
          ),
          StreamProvider<AppContentDataModel>.value(
            value: DatabaseAppService().appContentData,
            catchError: (_, e) {
              return null;
            },
          ),
          ChangeNotifierProvider<WebFullScreenMode>(
            create: (_) => WebFullScreenMode(),
          ),
        ],
        child: Builder(
          builder: (context) {
            return Consumer<WebFullScreenMode>(
                    builder: (_, data, child) {
                      bool fullScreen = data.getScreenMode();
                      return MaterialApp(
                        debugShowCheckedModeBanner: false,
                        theme: ThemeProvider.of(context),
                        builder: (context, child) => fullScreen
                            ? child
                            : WebMainScaffold(
                                child: child,
                              ),
                        navigatorKey: locator<NavigationService>().navigatorKey,
                        onGenerateRoute: generateRoute,
                        initialRoute: '/',
                      );
                    },
                  );
          },
        ),
      ),
    );
  }
}



class WebMainScaffold extends StatelessWidget {
  const WebMainScaffold({
    Key key,
    @required this.child,
  }) : super(key: key);
  final Widget child;
  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;

    bool isLaptop = PlatformInfo().isLaptop(MediaQuery.of(context));
    return Scaffold(
      extendBodyBehindAppBar: false,
      appBar: isLaptop
          ? null
          : PreferredSize(
              preferredSize: Size.fromHeight(50),
              child: MobileAppBar(),
            ),
      drawer: isLaptop
          ? null
          : Drawer(
              child: SideDrawerApp(),
            ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          if (isLaptop) NavigationBarTabletDesktop(),
          Expanded(
            child: child
          ),
        ],
      ),
    );
  }
}



class SideDrawerApp extends StatelessWidget {
  const SideDrawerApp({
    this.header = true,
    Key key,
  }) : super(key: key);

  final bool header;

  @override
  Widget build(BuildContext context) {

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (header) SideBarHeader(),
        SizedBox(height: 10),
        Expanded(
          child: SingleChildScrollView(
            child:  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      SideLinkCard(
                        text: 'Home',
                        icon: Icon(
                          LineAwesomeIcons.home,
                          size: 25,
                        ),
                        tap: '',
                      ),
                      SideLinkCard(
                        text: 'Test Series',
                        icon: Icon(
                          LineAwesomeIcons.clipboard_list,
                          size: 25,
                        ),
                        tap: 'test-series',
                      ),
                      Divider(
                        indent: 60,
                        thickness: 0.2,
                        color: Theme.of(context).disabledColor,
                      ),
                    ],
                  ),
                 ),
        ),
      ],
    );
  }
}



class SideLinkCard extends StatelessWidget {
  final String text;
  final Icon icon;
  final String tap;
  final bool diff;

  const SideLinkCard({
    this.text,
    this.icon,
    this.tap,
    this.diff = false,
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    bool laptop = PlatformInfo().isLaptop(MediaQuery.of(context));

    return Container(
      width: MediaQuery.of(context).size.width * 0.65,
      alignment: Alignment.centerLeft,
      child: FlatButton(
        visualDensity: VisualDensity(
            vertical: PlatformInfo().isWeb()
                ? 0
                : icon != null
                    ? -1.5
                    : -4),
        child: Padding(
          padding: EdgeInsets.symmetric(vertical: laptop ? 8 : 0),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              if (icon != null) icon,
              SizedBox(width: 15),
              Expanded(
                child: Text(
                  text,
                  style: kTitleText2NStyle,
                ),
              ),
            ],
          ),
        ),
        onPressed: () async {
          if (!laptop) {
            // giving error
            Navigator.pop(context);
            // locator<NavigationService>().goBack();
          }
          locator<NavigationService>().navigateTo("/$tap");
        },
      ),
    );
  }
}

0 Answers
Related