初始化
This commit is contained in:
27
lib/router/app_routes.dart
Normal file
27
lib/router/app_routes.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'package:flutter/cupertino.dart' hide RouterConfig;
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import 'config/route_paths.dart';
|
||||
import 'config/router_config.dart';
|
||||
import 'modules/home_routes.dart';
|
||||
|
||||
List<RouterConfig> routeConfigs = [...homeRoutes];
|
||||
|
||||
//for循环遍历
|
||||
List<RouteBase> routes = routeConfigs.map((item) {
|
||||
return GoRoute(
|
||||
path: item.path,
|
||||
builder: (context,state){
|
||||
return item.child(state);
|
||||
}
|
||||
);
|
||||
}).toList();
|
||||
|
||||
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
//变量命名
|
||||
GoRouter goRouter = GoRouter(
|
||||
navigatorKey: navigatorKey,
|
||||
initialLocation: RoutePaths.home,
|
||||
routes: routes,
|
||||
);
|
||||
3
lib/router/config/route_paths.dart
Normal file
3
lib/router/config/route_paths.dart
Normal file
@@ -0,0 +1,3 @@
|
||||
class RoutePaths {
|
||||
static const home = "/";
|
||||
}
|
||||
16
lib/router/config/router_config.dart
Normal file
16
lib/router/config/router_config.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class RouterConfig {
|
||||
String path;
|
||||
FutureOr<String?> Function(BuildContext, GoRouterState)? redirect;
|
||||
Widget Function(GoRouterState) child;
|
||||
|
||||
RouterConfig({
|
||||
required this.path,
|
||||
required this.child,
|
||||
this.redirect,
|
||||
});
|
||||
}
|
||||
13
lib/router/modules/home_routes.dart
Normal file
13
lib/router/modules/home_routes.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
import 'package:app/pages/home/home_page.dart';
|
||||
import 'package:app/router/config/router_config.dart';
|
||||
|
||||
import '../config/route_paths.dart';
|
||||
|
||||
List<RouterConfig> homeRoutes = [
|
||||
RouterConfig(
|
||||
path: RoutePaths.home,
|
||||
child: (state) {
|
||||
return HomePage();
|
||||
},
|
||||
)
|
||||
];
|
||||
Reference in New Issue
Block a user