一、配置路由

  1. class MyApp extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return MaterialApp(
  5. title: "Flutter",
  6. routes: {
  7. "detail":(context)=>MyDetail(),
  8. "home":(context)=>HomeContent()
  9. },
  10. home: Scaffold(
  11. appBar: AppBar(
  12. title: Text('Title'),
  13. ),
  14. body: HomeContent(),
  15. ),
  16. );
  17. }
  18. }

二、跳转

  1. class _HomeContnetState extends State {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Row(
  5. children: <Widget>[
  6. RaisedButton(child: Text("detail"),onPressed: goDetail,),
  7. ],
  8. );
  9. }
  10. void goDetail(){
  11. Navigator.pushNamed(context, "detail");
  12. }
  13. }

三、跳转传参

  • Home页

    1. //home.dart
    2. void getDetail(){
    3. Navigator.of(context).pushNamed("detail",arguments: "10011");
    4. }
  • 详情页

    1. class MyDetail extends StatelessWidget {
    2. @override
    3. Widget build(BuildContext context) {
    4. var args = ModalRoute.of(context).settings.arguments;
    5. print(args);
    6. return Scaffold(
    7. appBar: AppBar(
    8. title: Text('Title'),
    9. ),
    10. body: Container(
    11. child: Text("详情页"),
    12. ),
    13. );
    14. }
    15. }