flutterdartflutter-go-router

I would like to ask you about PopScope and go_router. I'm afraid it should have any issue


I'm trying to catch a PopScope event. I've tried but without success. Here is my code. I'll simplify it a bit:

GoRoute(
  path: '/group/:groupId',
  builder: (context, state) {
    final groupId = state.pathParameters['groupId']!;
    final extras = state.extra as Map<String, dynamic>?;

    logger.t('NR.goRoute: Grupo: $groupId. Extras recibidos: ${(extras == null) ? null : extras['fr']}');

    return PopScope(
      onPopInvokedWithResult: (didPop,_) async {
        logger.w('This log is not printed');
        if (!didPop & context.mounted) {
          context.go('/groups'); // Navegar de vuelta a /groups
        }
      },
      child: Group__Screen(
        groupId: groupId,
        groupName: extras?['groupName'] ?? '',
        groupColor: extras?['groupColor'] ?? Colors.blue,
        fr: extras?['fr'],
      ),
    );
  },
),

I've develop a button to get back, and it works fine. But for some reason, I can't use the "back" method Android native, button or gesture.

I don't know why it didn't work. There is no more PopScope that can be truncate that PopScore. There is no Navigation.go() or whatelse. This is the develop of my button, that works fine.

@override
Widget build(BuildContext context) {
  final bloc = context.read<Group__Bloc>();

  return Scaffold(
    appBar: AppBar(
      title: Text(groupName),
      backgroundColor: groupColor,
      leading: IconButton(
        icon: const Icon(Icons.arrow_back),
        onPressed: () async => context.go('/groups'),
      ),
      actions: [
        IconButton(
          icon: const Icon(Icons.share),
          onPressed: () => _handleDeepLinkShare(context),
        ),
      ],
    ),
    body: Column(
      children: 

That icon works fine. But back gesture doesn't even print the log with Warning level.

I'm trying to use a physical Android Pixel 7 to debug, in a Mac Air. I don't know if it's related with it, but I don't know what I can do more.

Any idea to avoid PopScope?


Solution

  • Finally I've found my error. It seems that I need to use nested routes to make the navigation backwards possible without exiting the app. Now it's better than ever.

    GoRoute(
      path: '/groups',
      builder: (context, state) => const PopScope(
        canPop:false,
        child: GroupsScreen(),
      ),
      routes: [
        GoRoute(
          path: 'avatar-selector',
          builder: (context, state) => PopScope(
            onPopInvokedWithResult: (didPop, result) {
            if (didPop) {
                 logger.d('Popped from group route');
            } else {
                 logger.d('Popped from group route with result: $result');
            }
          },
          child: const AvatarSelectorScreen(),
          ),
        ),
        GoRoute(
          path: ':groupId',
          builder: (context, state) {
          final groupId = state.pathParameters['groupId']!;
          final extras = state.extra as Map<String, dynamic>?;
          logger.d('Building group route for $groupId');
          return PopScope(
            onPopInvokedWithResult: (didPop, result) {
              if (didPop) {
                logger.d('Popped from group route'); // I finally see this log!!!!
              } else {
                logger.d('Popped from group route with result: $result');
              }
            },
          child: Group__Screen(...),
          );
        },
      ),
    ],
    ),
    

    It works fine now 🤌💪💪💪