I'm using Generated Routes with Flutter, and I would like to share blocs between them.
But I think that maybe the example solution might not scale the best, since the blocs are instantiated with the router.
Is there a way to instantiate them only when the user access that route?
class AppRouter {
final _counterBloc = CounterBloc();
Route onGenerateRoute(RouteSettings settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _counterBloc,
child: HomePage(),
),
);
case '/counter':
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _counterBloc,
child: CounterPage(),
),
);
default:
return null;
}
}
}
Got it! Flutter has a new keyword called late to do this without any boilerplate.
class AppRouter {
late final CounterBloc _counterBloc = CounterBloc();
Route onGenerateRoute(RouteSettings settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _counterBloc,
child: HomePage(),
),
);
case '/counter':
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _counterBloc,
child: CounterPage(),
),
);
default:
return null;
}
}
}
That's why we love flutter!
Reference: What is Null Safety in Dart?
Related
pushAndRemoveUntil is not proper working with "onGenerateRoute" in MaterialApp but if I have used pushAndRemoveUntil the "routes" in MaterialApp So it's working fine.
current screen name is FiveScreen. and here is the one button and below code in for onclick.
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context)=>ThirdScreen()), ModalRoute.withName(MUtils.second));
if I have use the "routes" in MaterialApp So it's navigate to "SecondScreen" after onbackpress to navigate to "FirstScreen" but If I have Use the the "onGenerateRoute" in MaterialApp so it's redirect to "secondScreen" but after click on the backpress so App is closed.
Thank you in advance.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
/*routes: {
MUtils.home: (context) => HomeScreen(),
MUtils.second: (context) => SecondScreen(),
MUtils.thirdScreen: (context) => ThirdScreen(),
MUtils.fourth: (context) => FourthScreen(),
MUtils.settings: (context) => SettingsScreen(),
},*/
onGenerateRoute:MyRoutes().onGenerateRoute,
//home: const MyHomePage(title: 'Flutter Demo Home Page'),
);}
Below is onGenerate Route class
class MyRoutes{
Route? onGenerateRoute(RouteSettings settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute(
builder: (_) => MyHomePage(
title: "Home Screen",
),
);
case MUtils.home:
return MaterialPageRoute(
builder: (_) => HomeScreen(content: settings.arguments,),
);
case MUtils.second:
return MaterialPageRoute(
builder: (_) => SecondScreen(
),
);case MUtils.thirdScreen:
return MaterialPageRoute(
builder: (_) => ThirdScreen(
),
);
case MUtils.fourth:
return MaterialPageRoute(
builder: (_) => FourthScreen(
),
);
case MUtils.settings:
return MaterialPageRoute(
builder: (_) => SettingsScreen(),
);
default:
return null;
}
}
}
I have draw design to better understand.
Route:- HomeScreen=> 1_Screen=> 2_Screen=> 3_Screen=> 4_Screen=> 5_Screen (Now Navigate to) 2_Screen (onBackPress)=> 1_Screen (onBackPress)=> HomeScreen.
onGenerateRoute:- HomeScreen=> 1_Screen=> 2_Screen=> 3_Screen=> 4_Screen=> 5_Screen (Now Navigate to) 2_Screen (onBackPress)=> App Closed.
So here issue the issue in "onGenerateRoute" when backpress in 2_screen to app is closed but it's working in "Routes".
Mycode
onGenerateRoute: (setting){
Widget myroute = Container(
);
if(setting.name == '/'){
myroute = MyHomePage(
title: "Home Screen",
);
}else if(setting.name == MUtils.home){
myroute = HomeScreen();
}else if(setting.name == MUtils.second){
myroute = SecondScreen();
}else if(setting.name == MUtils.thirdScreen){
myroute = ThirdScreen();
}else if(setting.name == MUtils.fourth){
myroute = FourthScreen();
}else if(setting.name == MUtils.settings){
myroute = SettingsScreen();
}
return MaterialPageRoute(
builder: (_) => myroute,
);
},
Your Code:-
onGenerateRoute: (settings) {
final map = {'/': 1, '1': 2, '2': 3, '3': 4, '4': 5,'5':6};
return MaterialPageRoute(
builder: (ctx) => FooPage(map[settings.name]!),
settings: settings, // <<< this is the difference
);
},
Only the deference is here but I couldn't find the issue in my code.
I am trying to provide a local bloc to a new page, I found some way to do this by using an anonymous route but it doesn't look elegant
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return BlocProvider.value(
value: context.bloc<MyBloc>(),
child: NewPage());
}),
);
What I want is to do the same thing but using a named route and without creating a global bloc as I simply can't
Create app_router.dart file.
app_router.dart
class AppRouter {
final LocalBloc _localBloc = LocalBloc();
Route onGenerateRoute(RouteSettings routeSettings) {
switch (routeSettings.name) {
case '/':
return MaterialPageRoute(
builder: (context) => BlocProvider.value(
value: _localBloc,
child: Home(),
),
);
case '/page1':
return MaterialPageRoute(
builder: (context) => BlocProvider.value(
value: _localBloc,
child: Page1(),
),
);
case '/page2':
return MaterialPageRoute(
builder: (context) => BlocProvider.value(
value: _localBloc,
child: Pag2(),
),
);
}
}
}
Then in your main.dart file
Add onGenerateRoute
final AppRouter _appRouter = AppRouter();
MaterialApp(
title: 'My App',
onGenerateRoute: _appRouter.onGenerateRoute,
),),
In your navigation, you can do this:
Navigator.of(context).pushNamed('/page1');
I am struggling in my app:
After the user logs in I navigate to my HomeView with pushReplaceNamed , so the user should not be able to pop (by dragging) on the HomeView because in my thought it is the root view. However that is not the case.. Even after calling pushReplaceNamed the user can still drag to pop:
I simply call:
Navigator.pushReplacementNamed(
context,
Views.home,
);
On the first start of the App StartView is displayed, from there I navigate to EmailView and from there to LoginView. The code above is inside my LoginView. In all the other views I simply call pushNamed.
What am I missing here? Why can the user drag pop on HomeView?
(I know I can disable it with WillPopScope, but that does not feel right...)
This is my setup in my MaterialApp:
Widget build(BuildContext context) {
return MaterialApp(
title: 'AppFlug',
navigatorKey: Get.key,
theme: ThemeData(
fontFamily: AppTextStyles.montserrat,
primarySwatch: ColorService.createMaterialColor(
AppColors.blue,
),
backgroundColor: AppColors.white,
scaffoldBackgroundColor: AppColors.white,
),
initialRoute:
AuthenticationService.isLoggedIn() ? Views.home : Views.start,
onGenerateRoute: AppRouter.generateRoute,
);
}
And here is my AppRouter:
class AppRouter {
static Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case Views.start:
return MaterialPageRoute(
builder: (context) => const StartView(),
);
case Views.email:
return MaterialPageRoute(
builder: (context) => EmailView(),
);
case Views.signUp:
String email = settings.arguments as String;
return MaterialPageRoute(
builder: (context) => SignUpView(
email: email,
),
);
case Views.login:
String email = settings.arguments as String;
return MaterialPageRoute(
builder: (context) => LoginView(
email: email,
),
);
case Views.home:
return MaterialPageRoute(
builder: (context) => HomeView(),
);
default:
return MaterialPageRoute(
builder: (_) => Scaffold(
body: Center(
child: Text(
'No route defined for ${settings.name}',
),
),
),
);
}
}
}
In your scenario, the user will be able to drag to pop and will return to EmailView cause you replaced login with home, this is what happens right? if u want to pop all of them (StartView, EmailView, LoginView) when you push to HomeView, then use:
Navigator.pushNamedAndRemoveUntil(context, Views.Home, (route) => route.settings.name == '/')
Also, add this to your AppRouter Cases
MaterialPageRoute(
settings: RouteSettings(name: /*name of the route*/),
builder: (context) => ...
With the help of Omar I got it working: so pushReplaceNamed is only replacing the top most route. In my case all I had to do was call:
Navigator.pushNamedAndRemoveUntil(
context,
Views.home,
(route) => false,
);
To quote the doc:
To remove all the routes below the pushed route, use a [RoutePredicate] that always returns false (e.g. (Route route) => false).
I new a bloc/cubit pattern, before I used provider, getx and I want to ask something.
This application have nearly 40 pages and I want to use bloc just 10 pages and close bloc instance when is last page is closed.
Example page 1, I initialize bloc, and it is initialized on the other pages (2, 3, 4...9) will use same bloc/cubit instance and 10 page close bloc instance close
This is router class and cubit initialize here
class Router {
static Route<dynamic> generateRoute(RouteSettings settings) {
final RegisterCubit _registerCubit = RegisterCubit();
switch (settings.name) {
case welcomeRoute:
return MaterialPageRoute(builder: (_) => WelcomePage());
case loginRoute:
return MaterialPageRoute(builder: (_) => LoginPage());
case homeRoute:
return MaterialPageRoute(builder: (_) => HomePage());
case referanceCodePreview:
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _registerCubit,
child: ReferancaCodePreviewView(),
));
case registerOne:
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _registerCubit,
child: RegisterPageOne(),
));
case registerTwo:
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _registerCubit,
child: RegisterPageOTwo(),
));
case registerThree:
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _registerCubit,
child: RegisterPageOThree(),
));
case registerFour:
return MaterialPageRoute(
builder: (_) => BlocProvider.value(
value: _registerCubit,
child: RegisterPageFour(),
));
case registerFive:
return MaterialPageRoute(
builder: (a) => BlocProvider.value(
value: _registerCubit,
child: RegisterPageFive(),
));
This page four and this page not listen to _registerCubit
class RegisterPageFour extends StatefulWidget {
RegisterPageFour({Key? key}) : super(key: key);
#override
State<RegisterPageFour> createState() => _RegisterPageFourState();
}
class _RegisterPageFourState extends State<RegisterPageFour> with
ImageSelectPicker {
#override
Widget build(BuildContext context) {
return BlocConsumer<RegisterCubit, RegisterState>(
listener: (contex, state) {},
builder: (context, state) {
return buildScaffold(context, state);
},
);
}
Navigation PageFour
CustomButton(
buttonWidth: MediaQuery.of(context).size.width,
onPress: () => Navigator.pushNamed(context, "/registerFour"),
title: "İleri"),
Can you help me this issue ?
final RegisterCubit _registerCubit = RegisterCubit();
I suggest you to create object of RegisterCubit outside of generateRoute function.
Something like this,
class Router {
final RegisterCubit _registerCubit = RegisterCubit();
static Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
}
I am using global key to handle my navigations. The main.dart, routes and NavigationService are as below. The main purpose of using the global keys is to make the navigation independent of context so that the network modules can auto log user out via the dio interceptor if the refresh token expires.
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AZY APP',
navigatorKey: locator<NavigationService>().navigatorKey,
onGenerateRoute: router.generateRoute,
home: HomePageScreen())
}}
NavigationService.dart
class NavigationService {
final GlobalKey<NavigatorState> navigatorKey =
new GlobalKey<NavigatorState>();
Future<dynamic> navigateTo(String routeName, {dynamic arguments}) {
return navigatorKey.currentState.pushNamed(routeName, arguments: arguments);
}
pop(value) {
print("pop");
return navigatorKey.currentState.pop(value);
}
goBack() {
print("goback");
return navigatorKey.currentState.pop();
}
popUntil(String desiredRoute) {
return navigatorKey.currentState.popUntil((route) {
print("${route.settings.name}");
return route.settings.name == desiredRoute;
});
}
pushReplacementNamed(String route) {
print("pushReplacementNamed");
return navigatorKey.currentState.pushReplacementNamed(route);
}
}
Routes.dart
Route<dynamic> generateRoute(RouteSettings settings) {
final args = settings.arguments;
switch (settings.name) {
case routes.SplashScreenRoute:
return MaterialPageRoute(builder: (context) => SplashScreen());
case routes.LoginScreenRoute:
return MaterialPageRoute(builder: (context) => LoginScreen());
case routes.MainPageScreenRoute:
return MaterialPageRoute(builder: (context) => MainPageScreen());
case routes.HomePageScreenRoute:
return MaterialPageRoute(builder: (context) => HomePageScreen());
default:
return MaterialPageRoute(
builder: (context) => Scaffold(
body: Center(
child: Text('Error Loading Screen'),
),
),
);
}
}
All the functions in NavigationService works well except popUntil. For every route, settings.name is null so i am unable to using navigatorkey with popUntil.
Log of print("${route.settings.name}").
The problem was that I was not passing route settings to MaterialPageRoute in my router.
MaterialPageRoute(
settings: settings, builder: (context) => SplashScreen());