static_route 2.1.0 static_route: ^2.1.0 copied to clipboard
Provide statically registered page navigation template library.
static_route #
Static page navigation router template library.
Getting Started #
- 添加
static_route
到 pubspec.yaml 文件. import 'package:static_route/static_route.dart';
在每个可导航页面中增加StaticRouteFactory
类型的静态route
变量
class HomePage extends StatelessWidget {
static final route = StaticRouteFactory(
name: 'HomePage',
builder: (BuildContext context, Object? arguments) {
return HomePage();
},
);
...
}
class NextPage extends StatefulWidget {
NextPage(this.content);
final String content;
static final route = StaticRouteFactory(
name: 'NextPage',
builder: (BuildContext context, Object? arguments) {
return NextPage(arguments as String);
},
);
@override
State createState() => _NextPageState();
}
在某个dart文件中,如pages.dart
中注册每个可导航页面的route
列表
final routeTable = <StaticRoute>[
HomePage.route,
NextPage.route,
...
];
在应用入口点MaterialApp
的onGenerateRoute
中调用Iterable<StaticRoute>
的扩展方法generateRoute
注册routeTable
中的路由器
MaterialApp(
...
onGenerateRoute: routeTable.generateRoute,
...
);
Usage #
Navigator.pushNamed(context, NextPage.route.name, arguments: 'from HomePage');