Pujith Vaddi
Pujith Vaddi

Reputation: 11

How to write the syntax for onTap() event to direct to other/new pages dynamically in a dynamic ListView in flutter?

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

Answers (1)

Luis Miguel Mantilla
Luis Miguel Mantilla

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}'),
        ),
      );
    });
}}}
  1. 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, )

  2. 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

Related Questions