1 MaterialApp 注册路由表
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp( // **切记不能在MaterialApp 相关的Widget 中执行跳转**
home: TowPage(),
theme: ThemeData(primaryColor: Colors.blue),
//注册路由表
routes: {
"intentPage": (
BuildContext context,
) =>
IntentPage(),
},
);
}
}
## 2 页面执行跳转 携带参数
class TowPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('132'),
),
drawer: Drawer(
child: Text('123'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.pushNamed(context, 'intentPage', arguments: {'title':'参数'});
},
child: Text('跳转'),
),
);
}
}
## 3 页面接受参数
class IntentPage extends StatelessWidget {
var data;
@override
Widget build(BuildContext context) {
data = ModalRoute.of(context)!.settings.arguments;
return Scaffold(
appBar: AppBar(
title: Text(data['title']),
),
body: Text('第二页'),
);
}
}
|