Flutterando / modular

A smart project structure
https://pub.dev/packages/flutter_modular
Other
1.3k stars 252 forks source link

onPoppedInvoked not working when swiping back #964

Open GeorgeZoiade opened 1 month ago

GeorgeZoiade commented 1 month ago

When using the modular package and pushing to a new route, when swiping back to previous screen, the screen is wrapped in a PopScope widget, but the onPopInvoked method is not working.

Expected behavior When swiping back, onPopInvoked should handle the callback.

This is my code:

void main() {
  runApp(
    ModularApp(module: AppModule(), child: const MyApp()),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      routerConfig: Modular.routerConfig,
    );
  }
}

class AppModule extends Module {
  @override
  void binds(i) {}

  @override
  void routes(r) {
    r.child('/', child: (context) => const HomeScreen());
    r.child('/swipe', child: (context) => const SwipeScreen());
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home'),
      ),
      body: GestureDetector(
        onTap: () => Modular.to.pushNamed('/swipe'),
        child: const Center(
          child: Text('Home'),
        ),
      ),
    );
  }
}

class SwipeScreen extends StatelessWidget {
  const SwipeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return PopScope(
      onPopInvoked: (didPop) {
        if (didPop) {
          print('should handle some callback after popping the route');
        }
      },
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Swipe'),
        ),
        body: const Center(
          child: Text('Swipe'),
        ),
      ),
    );
  }
}
edugemini commented 1 month ago

Flutter Web???

ramon-bernardo commented 6 days ago

I attempted to reproduce the issue on the latest versions of Flutter and f_modular but couldn't replicate it. It's worth noting that onPopInvoked has been replaced by onPopInvokedWithResult.

I navigated to the home screen and after click, moved to the swipe screen. When I click on back button, I received the following message in the console: 'should handle some callback after popping the route'.

image

Flutter 3.24.2 (stable) Dart SDK 3.5.2 Flutter modular 6.3.4 (and current master)

// deprecated
onPopInvoked: (didPop) {
  if (didPop) {
    print('should handle some callback after popping the route');
  }
}

// new
onPopInvokedWithResult: (didPop, result) {
  if (didPop) {
    print('should handle some callback after popping the route');
  }
}