Closed deepak-kumar-swain closed 1 year ago
Yes, currently we are facing issues.
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...')),
)),
);
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.
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,
);
});
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,
));
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'),
);
},
);
}
Seems good, would be great to know when it releases!
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.
The changes look good! (Just a little typo in the GoRouter description: pacakge)
@LenhartStephan Thanks for pointing out the typo. I've fixed that.
@deepak-kumar-swain @krishnatejakanchi @kyeshmz Have you tried this update?
@larryaasen Yes. This works.. Thank you for this🎉🎉
With GoRouter, where to initiate Upgrader