注册命名路由
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: "home", //默认启动页面
//注册路由表
routes: {
"intentPage":(context) => IntentPage(),
"home":(context) => MyHomePage(title: 'Flutter Demo Home Page'),
},
);
}
}
跳转发送参数
class _TabBarPage2State extends State<TabBarPage2> {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
child: RaisedButton(
child: Text('跳转传值'),
onPressed: (){
//第一个参数 命名路由的名字,第二个是要传输的数据
Navigator.of(context).pushNamed("intentPage",arguments: nameBean("张三", 18));
//nameBean 是自定义的一个数据实体类
},
),
),
);
}
}
class nameBean{
String name;
int age;
nameBean(this.name,this.age);
}
接受数据
class _IntentPageState extends State<IntentPage> {
nameBean name;
@override
Widget build(BuildContext context) {
name=ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: AppBar(
title: Text('传值页面'),
),
body: Text('内容==${name.name}==${name.age}'),
);
}