In the world of mobile app development, efficient navigation is a crucial aspect of providing a smooth user experience. Flutter, a popular cross-platform framework, offers an intuitive way to manage navigation and routing within your app. In this tutorial, we’ll guide you through the process of creating a Flutter application with named routes and parameter passing. By the end of this tutorial, you’ll have a fully functional Flutter app with multiple screens that you can navigate between seamlessly.
Setting Up Routing Constants:
Create a constants.dart file to define routing constants:
To start, create a constants.dart file to define routing constants. These constants will be used to specify the routes throughout your app.
const String homeRoute = '/';
const String feedRoute = '/feed';
const String detailsRoute = '/details';
const String settingsRoute = '/settings';Create a router.dart file to handle routing logic:
Get Eman Yaqoob’s stories in your inbox
Join Medium for free to get updates from this writer.
Next, create a router.dart file to handle the routing logic. This router will determine which screen to display based on the specified route.
import 'package:flutter/material.dart';
import 'constants.dart';
import 'views/home.dart';
import 'views/feed.dart';
import 'views/details.dart';
import 'views/settings.dart';
class Router {
static Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case homeRoute:
return MaterialPageRoute(builder: (_) => Home());
case feedRoute:
return MaterialPageRoute(builder: (_) => Feed());
case detailsRoute:
var data = settings.arguments as String;
return MaterialPageRoute(builder: (_) => Details(data));
case settingsRoute:
return MaterialPageRoute(builder: (_) => Settings());
default:
return MaterialPageRoute(
builder: (_) => Scaffold(
body: Center(
child: Text('No route defined for ${settings.name}'),
),
),
);
}
}
}Create view files: home.dart, feed.dart, details.dart, and settings.dart.
Now, create separate view files for each screen: home.dart, feed.dart, details.dart, and settings.dart. These files will define the content and structure of each screen.
Here’s a simplified example of the view files
// home.dart
import 'package:flutter/material.dart';
import '../constants.dart';
import '../router.dart';
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.pushNamed(context, feedRoute);
},
child: Icon(Icons.arrow_forward),
),
body: Center(child: Text('Home')),
);
}
}
// feed.dart
import 'package:flutter/material.dart';
import '../constants.dart';
import '../router.dart';
class Feed extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.pushNamed(context, detailsRoute, arguments: 'Data from Feed');
},
child: Icon(Icons.arrow_forward),
),
body: Center(child: Text('Feed')),
);
}
}
// details.dart
import 'package:flutter/material.dart';
class Details extends StatelessWidget {
final String data;
Details(this.data);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('Details: $data')),
);
}
}
// settings.dart
import 'package:flutter/material.dart';
class Settings extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('Settings')),
);
}
}Setting Up the Main Application:
In your main.dart file, set up the main application by defining a MaterialApp widget. This widget specifies the initial route and how to generate routes using the Router class you created.
import 'package:flutter/material.dart';
import 'router.dart';
import 'constants.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
onGenerateRoute: Router.generateRoute,
initialRoute: homeRoute,
);
}
}Now you have a complete Flutter application with 3–4 screens using named routes and parameters. When you run the app, you should be able to navigate between the different screens by tapping on the floating action buttons and see the data being passed to the Details screen.
This approach allows for efficient navigation between screens while passing data when needed. As you continue to develop your Flutter app, you can expand upon this foundation to create a fully-featured and user-friendly mobile application. Happy Coding!










