Widget build(BuildContext context) {
return MaterialApp(
title: ‘Flutter_Android混合开发’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(initParams: initParams),
);
}
}
class HomePage extends StatefulWidget {
final String initParams;
const HomePage({Key key, this.initParams}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: _widgetRoute(widget.initParams),
),
);
}
}
///路由转发
Widget _widgetRoute(String route) {
switch (route) {
case “route1”:
return route1Widget();
case “route2”:
return route2Widget();
default:
return notFoundWidget();
}
}
Widget route1Widget() {
return Center(
child: Text(
“this is route1Widget”,
style: TextStyle(color: Colors.red, fontSize: 20),
),
);
}
Widget route2Widget() {
return Center(
child: Text(
“this is route2Widget”,
style: TextStyle(color: Colors.blue, fontSize: 20),
),
);
}
Widget notFoundWidget() {
return Center(
child: Text(
“未匹配到路由111”,
style: TextStyle(fontSize: 40),
),
);
}
我们现在将加载Flutter时的initialRoute参数替换为 “route1”,那页面将加载route1Widget,替换为 “route2”,将加载route2Widget。否则将展示notFoundWidget。当然我们可以直接传路由参数,但是因为这个参数本身是一个字符串,所以我们可以来搞