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();
Related
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?
I am working on a flutter app. i have set up router in the app with named routes. on going to my home page it shows as locahost:1234/#/home. and the path is correct. but from there when a navigate back to previouse page it still showing the same path locahost:1234/#/home. the path url is not changing on navigating back. if any one has an idea?
my router file is as follows:
/* 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<SearchCubit>(
create: (BuildContext context) =>
SearchCubit(repository: repository),
),
BlocProvider<UserCubit>(
create: (BuildContext context) {
return UserCubit(repository: repository);
},
)
], child:userToken==null?SplashScreen():NavScreen()
// SplashScreen()
));
case "/getstarted":
return MaterialPageRoute(
settings: RouteSettings(name: '/getstarted'),
builder: (_) => BlocProvider(
create: (BuildContext context) =>
ProfileCubit(repository: repository),
child: OnboardingScreen()));
//return MaterialPageRoute(builder: (_) => VideoDetailScreen());
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 '/home':
return MaterialPageRoute(
settings: RouteSettings(name: '/home'),
builder: (_) => BlocProvider(
create: (BuildContext context) =>
HomeCubit(repository: repository),
child: NavScreen()));
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),),
),
);
}
);
}
}
}
Just add settings: settings, to MaterialPageRoute
Page url not changing in flutter, but the page content changes fine
You have to use Flutter Navigator 2.0 to achieve Dynamic and Deep linking i.e Changing URLs on page change and getting the functionality of Browser's back and reload button.
The easiest way to achieve this is by using go_router Flutter package, which has very easy setup and documentation.
In this you can define your paths which changes on page change.
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),),
),
);
});
}
}
}
Let's consider we have a list of pages and based on some logic, we want to display the corresponding route using Navigator. How do we do that?
final List<dynamic> components = [ PageA, PageB ]
..
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => new components[0]()),
);
There is routes attribute within material app widget. You should use that:
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Sample',
home: Wrapper(),
routes: {
CarsList.routeName: (ctx) => PageOne(),
CarDisplay.routeName: (ctx) => PageTwo(),
OrdersScreen.routeName: (ctx) => PageThree(),
EditCar.routeName: (ctx) => PageFour(),
},
),
Before using that make sure that you have a static variable named as routeName in every widget.
static const routeName = '/PageOne';
I'm not entirely sure I understand you issuer but if your question is where/how you can put a condition there are two ways of doing so:
Let's say that based on condition you when to navigate to either components[0]() or components[1]() :
First:
final List<dynamic> components = [ PageA, PageB ]
..
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => condition ? new components[0]() : new component[1]()
),
);
Second:
final List<dynamic> components = [ PageA, PageB ]
..
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
if (condition) {
return new components[0]()
} else {
return new component[1]()
}
),
);
/*This is my code
On the main page:
*/
home: MyHomePage(),
routes: <String, WidgetBuilder> {
'/screen1': (BuildContext context) => new NewCreateProfile()
},
// Want to come on this page with Results
Navigator.of(context).popUntil((route) => route.isFirst );
Define final variable in first screen use required
Set the value of results to a variable by setstate
Navigator.push( context,MaterialPageRoute(
builder: (context) => firstscreen(result),
),
);