flutter 分享代码(四)
2019-08-05 本文已影响0人
Victory_886
- 一、首先在MaterialApp中设置一下路由。
代码如下:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.red,
),
home: MyHomePage(),
onGenerateRoute: onGenerateRoute, /// 这里是配置路由
);
}
}
- 二、创建一个路由管理类,具体代码如下:
final routesMap = {
"/root1": (context) => new MusicQQPage(),
"/root2": (context) => new MusicXMPage(),
"/root3": (context) => new MusicMainPage(),
};
var onGenerateRoute = (RouteSettings settings) {
final String name = settings.name;
final Function pageContentBuilder = routesMap[name];
if (pageContentBuilder != null) {
Route route;
if (settings.arguments != null) {
route = MaterialPageRoute(
builder: (context) =>
pageContentBuilder(context, arguments: settings.arguments));
} else {
route =
MaterialPageRoute(builder: (context) => pageContentBuilder(context));
}
return route;
}
};
- 三、使用方法如下:
Navigator.pushNamed(context, "/root1");