- 一、首先在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");
网友评论