router_management 1.3.3 router_management: ^1.3.3 copied to clipboard
Router Management is a package with powered tools for routing your flutter application
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:router_management/router_management.dart';
//Initialize your app
void main() {
AppRoutes.setRoutes();
runApp(App());
}
//Create your AppRoutes class
class AppRoutes {
AppRoutes._internal();
static final router = RouterController();
static const intro = '/intro';
static const home = '/home';
static void setRoutes() {
router.addRoute(
intro,
(BuildContext context) => IntroScreen(),
useNativeTransitions: !kIsWeb || Platform.isIOS,
);
router.addRoute(
home,
(BuildContext context) => HomeScreen(),
useNativeTransitions: !kIsWeb || Platform.isIOS,
);
}
}
//Configure your class that hold your app configuration with MaterialApp or other WidgetApp
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
onGenerateRoute: AppRoutes.router.onGenerateRoute,
initialRoute: AppRoutes.intro,
//...
);
}
}
//Introduction screen navigating to Home screen with arguments. You can navigate without arguments!
class IntroScreen extends StatefulWidget {
@override
_IntroScreenState createState() => _IntroScreenState();
}
class _IntroScreenState extends State<IntroScreen> {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback(callback);
}
void callback(_) async {
await Future.delayed(const Duration(seconds: 3));
final argument = <String, dynamic>{'initialized': true};
Navigator.pushReplacementNamed(
context,
AppRoutes.home,
arguments: argument,
);
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.blue,
);
}
}
//Home scree that initialize after Intro screen and read arguments.
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
void didChangeDependencies() {
super.didChangeDependencies();
final Map<String, dynamic> arguments =
ModalRoute.of(context).settings.arguments;
print(arguments);
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.amber,
);
}
}