using_routes_manager
Using a route manager can help you manage and organize your app's navigation by defining and handling routes more efficiently. Here's how to use a simple custom route manager in a Flutter app:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Route Manager Example',
initialRoute: '/',
onGenerateRoute: RouteManager.generateRoute,
);
}
}
class RouteManager {
static Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (context) => HomeScreen());
case '/second':
return MaterialPageRoute(builder: (context) => SecondScreen());
default:
return MaterialPageRoute(builder: (context) => UnknownScreen());
}
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home Screen')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/second');
},
child: Text('Go to Second Screen'),
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Second Screen')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Back to Home Screen'),
),
),
);
}
}
class UnknownScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Unknown Screen')),
body: Center(
child: Text('Unknown Route'),
),
);
}
}
In this example, we've created a custom RouteManager
class to handle route generation. The generateRoute
method of the RouteManager
is called for each route, allowing you to define custom routes and their corresponding widgets. The onGenerateRoute
property in MaterialApp
is set to RouteManager.generateRoute
.
This approach allows you to have a central place to manage and configure your app's routes, making it easier to maintain and scale your navigation as your app grows. You can add more cases in RouteManager.generateRoute
to handle additional routes in your app.
Last updated