Flutterando / modular

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

How to use WillPopScope under an architecture made with modular #496

Closed abdiaschafy closed 3 years ago

abdiaschafy commented 3 years ago

How to use WillPopScope under an architecture made with modular? An example of my main and my app_page

MAIN

void main() async { // flavor configuration FlavorConfig( name: FlavorEnvironment.DEV, location: BannerLocation.topEnd, variables: { FlavorEnvironment.FLAVOR_ENABLE_DEBUG_CONSOLE: false, FlavorEnvironment.FLAVOR_OPEN_DEBUG_CONSOLE_ON_START_UP: false, FlavorEnvironment.FLAVOR_ENABLE_FIREBASE_ANALITICS: true, FlavorEnvironment.FLAVOR_ENABLE_FIREBASE_CRASHLITICS: true, FlavorEnvironment.FLAVOR_USE_HTTPS: false, FlavorEnvironment.FLAVOR_SERVER_CENTRAL_SERVER_ENDPOINT: "192.168.1.105:6080/api", }, );

WidgetsFlutterBinding.ensureInitialized();

// Wait for Firebase to initialize await Firebase.initializeApp();

runZonedGuarded(() { FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError; runApp(ModularApp(module: AppModule())); }, FirebaseCrashlytics.instance.recordError); }

app_page

@override Widget build(BuildContext context) { return FutureBuilder( future: _initializeFlutterFireFuture, builder: (context, snapshot) { switch (snapshot.connectionState) { case ConnectionState.done: if (snapshot.hasError) { return Center( child: Text('Error: ${snapshot.error}'), ); } return _getMaterialApp(); default: return Container(); } }, ); }

Widget _getMaterialApp() { final AnalyticsService analyticsService = Modular.get();

return MaterialApp(
  title: 'P-Cash',
  //debugShowCheckedModeBanner: false,
  localizationsDelegates: [
    S.delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
  ],
  supportedLocales: S.delegate.supportedLocales,
  locale: new Locale("en", "fr"),
  // TODO move local management to BLOC
  //localeResolutionCallback: S.delegate.resolution(fallback: new Locale("en")),
  // TODO move local management to BLOC
  theme: ThemeData(
    // This is the theme of your application.
    //
    // Try running your application with "flutter run". You'll see the
    // application has a blue toolbar. Then, without quitting the app, try
    // changing the primarySwatch below to Colors.green and then invoke
    // "hot reload" (press "r" in the console where you ran "flutter run",
    // or simply save your changes to "hot reload" in a Flutter IDE).
    // Notice that the counter didn't reset back to zero; the application
    // is not restarted.
    primaryColor: Colors.lightGreen,
    primaryColorLight: Colors.lightGreen[100],
    primaryTextTheme: TextTheme(headline6: TextStyle(color: Colors.white)),

    // Define the default font family.
    //fontFamily: 'Georgia',

    // This makes the visual density adapt to the platform that you run
    // the app on. For desktop platforms, the controls will be smaller and
    // closer together (more dense) than on mobile platforms.
    visualDensity: VisualDensity.adaptivePlatformDensity,
  ),
  navigatorObservers: <NavigatorObserver>[analyticsService.getAnalyticsObserver()],
  // set your initial route
  initialRoute: "/",
  navigatorKey: Modular.navigatorKey,
  // add Modular to manage the routing system
  onGenerateRoute: Modular.generateRoute,
);

}

Ascenio commented 3 years ago

Well, Modular doesn't make much difference in this case, it depends more on your use case. If you want to prevent a single page from popping, put above its Scaffold and you should be good to go.