larryaasen / upgrader

A Flutter package for prompting users to upgrade when there is a newer version of the app in the store.
MIT License
547 stars 268 forks source link

Setup with GoRouter #259

Closed deepak-kumar-swain closed 1 year ago

deepak-kumar-swain commented 1 year ago

With GoRouter, where to initiate Upgrader

krishnatejakanchi commented 1 year ago

Yes, currently we are facing issues.

larryaasen commented 1 year ago

Can you wrap your existing widget from the MaterialApp.home like this?

    return MaterialApp(
      title: 'Upgrader Example',
      home: UpgradeAlert(
          child: Scaffold(
        appBar: AppBar(title: Text('Upgrader Example')),
        body: Center(child: Text('Checking...')),
      )),
    );
krishnatejakanchi commented 1 year ago

Thanks for the response @larryaasen, but I have 6 initial routes which will be shown based on different conditions. We want to show UpgradeAlert soon after app is opened irrespective of the initial page. Can you help me with any solution.

kyeshmz commented 1 year ago

yeah same thing here, I am using MaterialApp.router, which doesn't disclose a child

    return ScreenUtilInit(
        designSize: const Size(1080, 2340),
        minTextAdapt: true,
        splitScreenMode: true,
        builder: (context, child) {
          return MaterialApp.router(
            title: 'xxxxx',
            debugShowCheckedModeBanner: false,
            theme: AppStyles.light(),
            darkTheme: AppStyles.dark(),
            localizationsDelegates: const [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            locale: TranslationProvider.of(context).flutterLocale,
            supportedLocales: AppLocaleUtils.supportedLocales,
            routerConfig: router,
          );
        });
LenhartStephan commented 1 year ago

Here is a related issue on that topic: https://github.com/flutter/flutter/issues/103009 The main takeaway: It's not feasible to insert a widget below the navigator if you use MaterialApp.router without creating a big mess.

But there is a workaround by exposing the key from the router config (routerConfig.routerDelegate.navigatorKey) and using it's context to display the dialog within upgrade_alert.dart. This would require some changes to this package. (See 'working example' at the bottom of https://github.com/flutter/flutter/issues/103009#issue-1224839612)

These changes could look something like this: https://github.com/LenhartStephan/upgrader/commit/1e3dfff01cc4e85822633c83da4bceb55546a14e Then you'd just have to put the UpgradeAlert with the navigatorKey inside the builder function of MaterialApp:

GoRouter routerConfig = GoRouter(
    routes: [
      GoRoute(path: "/", builder: (_, __) => const HomeScreen()),
    ]);

return MaterialApp.router(
    routerConfig: routerConfig,
    builder: (context, router) => UpgradeAlert(
      upgrader: Upgrader(
        navigatorKey: routerConfig.routerDelegate.navigatorKey,
      ),
      child: router,
    ));
larryaasen commented 1 year ago

I have a fix for this now. Here is the planned documentation which you can now see on the master branch. Please check this out and see it if will work for you.

_When using GoRouter (pacakge gorouter) with upgrader, you may need to provide a navigatorKey to the UpgradeAlert widget so that the correct route context is used. Below is part of the code you will need for this. Also, checkout the example/lib/main-gorouter.dart example for a more complete example.

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Upgrader GoRouter Example',
      routerConfig: routerConfig,
      builder: (context, child) {
        return UpgradeAlert(
          navigatorKey: routerConfig.routerDelegate.navigatorKey,
          child: child ?? Text('child'),
        );
      },
    );
  }
kyeshmz commented 1 year ago

Seems good, would be great to know when it releases!

larryaasen commented 1 year ago

I just published a pre-release version that you can try here: 7.1.0-alpha.2

Please provide feedback here on how this solves your problem with GoRouter.

LenhartStephan commented 1 year ago

The changes look good! (Just a little typo in the GoRouter description: pacakge)

larryaasen commented 1 year ago

@LenhartStephan Thanks for pointing out the typo. I've fixed that.

larryaasen commented 1 year ago

@deepak-kumar-swain @krishnatejakanchi @kyeshmz Have you tried this update?

chrisbin-kutuki commented 1 year ago

@larryaasen Yes. This works.. Thank you for this🎉🎉