Reputation: 11
I have created a code in which multiple ListTile widgets are created dynamically based on the list of items i store. I want to add onTap() functionality to it so that it redirects to the respective pages i have created.
Here i need how to redirect to a particular page based on the onTap() which is contained in a list view.
from the above code i don't know how to direct to a respective page based on the onTap() event.
Upvotes: 1
Views: 512
Reputation: 1748
I can suggest the following: 1. Create a class to handle only the routes of your application, as you can see below:
class Router {
static Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => HomeScreen());
case 'screen_1':
return MaterialPageRoute(builder: (_) => Screen1());
case 'screen_x':
return MaterialPageRoute(builder: (_) => ScreenX());
default:
return MaterialPageRoute(builder: (_) {
return Scaffold(
body: Center(
child: Text('No route defined for ${settings.name}'),
),
);
});
}}}
Use the onGenerateRoute property of the MaterialApp widget, to tell you to call that generator when the app tries to navigate to a named path, also you can use the initialRoute property if you want:
MaterialApp( debugShowCheckedModeBanner: false, initialRoute: '/', onGenerateRoute: Router.generateRoute, )
In the dynamic list you are generating, use the following:
Navigator.pushNamed (context, "screen_x");
where "screen_x" can be a property of the dynamic list object.
Upvotes: 1