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 97 forks source link

Breadcrumbs and Cupertino Back Context Menu #260

Closed jamesblasco closed 2 years ago

jamesblasco commented 2 years ago

It would be great if we could make publicly accessible the current list of routes in the stack so it is possible to create Breadcrumbs and Cupertino Back Context Menu. The design of those components don't need to be part of this library, but it would be great if it makes it possible

https://user-images.githubusercontent.com/19904063/147133237-2352ee2f-2656-4d3f-bc91-0049f033ee2c.MP4

https://user-images.githubusercontent.com/19904063/147133293-10ef1f4e-7b8c-4db3-81e5-de5faba4b6c8.mp4

Current approach


class BackButton extends StatelessWidget {
  const BackButton({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    assert(debugCheckHasMaterialLocalizations(context));

    return GestureDetector(
      onLongPress: () async {
        final router = GoRouter.of(context);
        final navigator = Navigator.of(context);
        if (router.routerDelegate.matches.isEmpty) return;
        final location = await showMenu(
            context: context,
            position: const RelativeRect.fromLTRB(0, 0, 20, 20),
            items: <PopupMenuEntry<String>>[
              for (final match
                  in router.routerDelegate.matches.reversed.skip(1))
                PopupMenuItem<String>(
                  value: match.route.name,
                  child: Text(match.route.name ?? 'Hey'),
                ),
            ]);
        if (location != null) {
          navigator.popUntil((route) => route.settings.name == location);
        }
      },
      child: IconButton(
        icon: const BackButtonIcon(),
        onPressed: () {
          Navigator.maybePop(context);
        },
      ),
    );
  }
}
csells commented 2 years ago

Hey! I definitely want to enable breadcrumbs! Would you be willing to produce the PR to create a breadcrumbs example for GR?

Check out GoRouter.of(context).routerDelegate.matches for the current stack of matches.

jamesblasco commented 2 years ago

Here you have it! https://github.com/csells/go_router/pull/261 Consider that GoRouter.of(context).routerDelegate.matches is marked as visibleForTesting only.

And I am using navigator.popUntil because go(..) does not transition back the routes being popped