csells / go_router

The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
https://gorouter.dev
441 stars 96 forks source link

Can not insert a widget below MaterialApp and above go_router's Navigator #320

Closed whollacsek closed 2 years ago

whollacsek commented 2 years ago

Hi @csells, according to the docs https://gorouter.dev/navigator-builder

Sometimes it is necessary to insert a widget above the Navigator, but below MaterialApp/CupertinoApp

For these purposes, you need to use the navigatorBuilder parameter in the GoRouter constructor. This is similar to the builder parameter in the MaterialApp constructor, but gives access to infrastructure provided by MaterialApp.

However I ran into an issue where I provided a wrapper widget to navigatorBuilder while defining my go_router instance, but this wrapper widget does not have access to the MaterialApp context. In this widget I'm calling showModalBottomSheet which throws:

The following assertion was thrown while handling a gesture:
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.
When the exception was thrown, this was the stack:
#0      Navigator.of.<anonymous closure> (package:flutter/src/widgets/navigator.dart:2553:9)
#1      Navigator.of (package:flutter/src/widgets/navigator.dart:2560:6)
#2      showModalBottomSheet (package:flutter/src/material/bottom_sheet.dart:694:46)
...

My go_router instance is defined as:

final _router = GoRouter(
  routes: [...],
  navigatorBuilder: (context, state, child) {
    return MyWrapperWidget(child: child);
  },
);

And the MyWrapperWidget displays on all routes a button that when clicked should show a bottom sheet modal.

How can I have a wrapper around all pages that can access the Navigator from MaterialApp?

csells commented 2 years ago

This is the expected behavior, since you've explicitly chosen to add content about the go_router-provided Navigator instance. If you'd like to provide you own Navigator for this kind of thing, you can do so as shown in the shared scaffold sample.