Here is generateRoute:
static Route<dynamic> generateRoute(RouteSettings settings) {
final args = settings.arguments as Map;
switch (settings.name) {
case '/productPage':
return CupertinoPageRoute(builder: (BuildContext context) => ProductPage(id: args["id"]));
default:
return CupertinoPageRoute(builder: (BuildContext context) => const HomePage(title: "HomePage",));
}
}
And I am using pushNamed when the user clicks on local notification as follows:
void listenToNotificationStream() => notificationService.behaviorSubject.listen((payload) {
navigatorKey.currentState!.pushNamed('/productPage', arguments: {"id": "c9dff26a-117a-4700-8965-05395c6dc4f5"});
});
The code above takes the user to the product page but when I click on the home button:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const HomePage(title: "HomePage"),
))
It calls generateRoute method with settings for the product page, not the home page.
Any help with this issue or am I doing anything wrong?
Related
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 now doing a flutter project. and i use cubit for state management. for navigation i had made a file for routing.using generate route. i have given the router file code below. does anyone had an idea about this.i used block providers to give cubit and navigated to pages using named route.i works when we manually type the path in the browser tab and when we pressed back the url path is not refreshed.
class AppRouter {
/* ADD REPOSITORY TO APP ROUTER */
/* ADD REPOSITORY TO APP ROUTER */
Repository repository;
AppRouter() {
repository = new Repository(apiService: ApiService());
}
Route generateRoute(RouteSettings settings) {
switch (settings.name) {
case "/":
return MaterialPageRoute(
settings: RouteSettings(name: '/'),
builder: (_) => MultiBlocProvider(providers: [
BlocProvider<HomeCubit>(
create: (BuildContext context) {
return HomeCubit(repository: repository);
},
),
BlocProvider<UserCubit>(
create: (BuildContext context) {
return UserCubit(repository: repository);
},
)
],child:SplashScreen()
case "/register":
return MaterialPageRoute(
settings: RouteSettings(name: '/REGISTER'),
builder: (_) => MultiBlocProvider(providers: [
BlocProvider<HomeCubit>(
create: (BuildContext context) {
return HomeCubit(repository: repository);
},
),
BlocProvider<SearchCubit>(
create: (BuildContext context) =>
SearchCubit(repository: repository),
),
BlocProvider<UserCubit>(
create: (BuildContext context) =>
UserCubit(repository: repository),
)
], child: RegisterScreen()));
case '/signin':
return MaterialPageRoute(
builder: (_) => BlocProvider(
create: (BuildContext context) =>
UserCubit(repository: repository),
child: SigninScreen()),
settings: RouteSettings(name: '/signin'));
default:
return MaterialPageRoute(builder: (_) {
return Scaffold(
body: Center(
child: Text('Error! No route Found...',
style: TextStyle(color: Colors.white,fontWeight:FontWeight.bold),),
),
);
});
}
}
}
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());
I want to switch between the login screen and Home screen based on bool value(user.status) from the model class below
class User extends ChangeNotifier {
int phoneNumber;
bool status = false;
notifyListeners();
}
The bool User.status value is flipped from below function
User _user = Provider.of<User>(context);
...
...
if (form.validate()) {
_user.status = true;
}
The below function has to listen to the changes in the status value from the User model and change the screen to Home().
class Wrapper extends StatelessWidget {
#override
Widget build(BuildContext context) {
User authStatus = Provider.of<User>(context);
return authStatus.status ? Home() : Auth();
}
}
I don't have any errors, all the values are updating accordingly but the Wrapper() is not being rebuilt after listening to the changes from ChangeNotifier
Here's how I do it with Provider :
routes: {
"/": (context) => MainPage(),
"/detail": (context) => UserDetailPage(),
},
builder: (context, child) {
return Consumer<UsersProvider>(
child: child,
builder: (context, provider, child) {
final value = provider.user;
if (!value.status) {
return Navigator(
onGenerateRoute: (settings) => MaterialPageRoute(
settings: settings, builder: (context) => LoginPage()),
);
}
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => UsersProvider()),
ChangeNotifierProvider(
create: (context) => InvoicesProvider()),
ChangeNotifierProvider(create: (context) => EventsProvider()),
],
child: child,
);
},
);
},
Basically use builder in main.dart and defines routes, then inside builder use Consumer were child is the initial route MainPage() so if the user already login they will go there, and if not, base on status they will redirect to LoginPage(). I hope you can understand feel free to comment
First of all, I'm not setting my routs in the MaterialApp like this
new MaterialApp(
home: new Screen1(),
routes: <String, WidgetBuilder> {
'/screen1': (BuildContext context) => new Screen1(),
'/screen2' : (BuildContext context) => new Screen2(),
'/screen3' : (BuildContext context) => new Screen3(),
'/screen4' : (BuildContext context) => new Screen4()
},
)
Instead, I'm routing from different places in my app by pushing new rout like this:
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) =>
Screen3(someInputData)));
How I can pop screens from the current one into screen number 2 for example?
Asumming: Screen1 -> Screen2 -> Screen3 -> Screen4
When you open the Screen2 , you could do something like this:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Screen2(),
settings: RouteSettings(name: '/screen2')),
);
And when you want to go back from Screen4 to Screen2 :
Navigator.popUntil(context, ModalRoute.withName("/screen2"));
If you just want to go back to the previous screen use:
Navigator.of(context).pop();