Android学习笔记(四):Flutter路由

未改造代码

import 'package:flutter/material.dart';

import 'navigator_demo.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: LoginPage(),
    );
  }
}
import 'package:flutter/material.dart';

class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("登录"),
      ),
      body: ElevatedButton(
          onPressed: () async { // 异步操作,会等到有返回值,再走
            var result = await Navigator.of(context).push(MaterialPageRoute(
              builder: (context) {
                return MenuPage(
                  title: "导航菜单",
                );
              },
              settings: RouteSettings(
                name: "menu",
                arguments: "",
              ),
              maintainState: false,   // 为false时,前一个页面loginPage不缓存
              fullscreenDialog: false   // 页面跳转时,动画不太一样
            ));
            print(result);
          },
          child: Text("登录按钮")
      ),
    );
  }
}

class MenuPage extends StatelessWidget {
  final String title;

  const MenuPage({Key key, @required this.title}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: ElevatedButton(
        onPressed: () {
          Navigator.of(context).pop({
            "name": "因为参数是Object,所以可以返回个对象"
          });
        },
        child: Text("返回按钮"),
      ),
    );
  }
}

第一遍改造:参数传递发生变化

import 'package:flutter/material.dart';

class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("登录"),
      ),
      body: ElevatedButton(
          onPressed: () {
            Navigator.of(context).push(MaterialPageRoute(
              builder: (context) {
                return MenuPage();
              },
              settings: RouteSettings(
                name: "menu",
                arguments: {"name", "张三"},  //也是Object类型的,所以可以随便传
              ),
              maintainState: false,   // 为false时,前一个页面loginPage不缓存
              fullscreenDialog: false   // 页面跳转时,动画不太一样
            )).then((value) => print(value));
          },
          child: Text("登录按钮")
      ),
    );
  }
}

class MenuPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    dynamic args = ModalRoute.of(context).settings.arguments;

    return Scaffold(
      appBar: AppBar(
        title: Text("titile" + "     " + args.toString()),
      ),
      body: ElevatedButton(
        onPressed: () {
          Navigator.of(context).pop({
            "name": "因为参数是Object,所以可以返回个对象"
          });
        },
        child: Text("返回按钮"),
      ),
    );
  }
}

第二次改造:路由表简化

import 'package:flutter/material.dart';

import 'navigator_demo.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      routes: {
        "/": (context) => LoginPage(),
        "menu": (context) => MenuPage(),
      },
      // initialRoute: "menu",  // 修改首页默认路由
    );
  }
}
import 'package:flutter/material.dart';

class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("登录"),
      ),
      body: ElevatedButton(
          onPressed: () {
            Navigator.of(context)
                .pushNamed("menu", arguments: "菜单导航")
                .then((value) => print(value));
          },
          child: Text("登录按钮")
      ),
    );
  }
}

class MenuPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    dynamic args = ModalRoute.of(context).settings.arguments;

    return Scaffold(
      appBar: AppBar(
        title: Text("titile" + "     " + args.toString()),
      ),
      body: ElevatedButton(
        onPressed: () {
          Navigator.of(context).pop({
            "name": "因为参数是Object,所以可以返回个对象"
          });
        },
        child: Text("返回按钮"),
      ),
    );
  }
}

第三次改造:路由守卫

import 'package:flutter/material.dart';

import 'navigator_demo.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      routes: {
        "/": (context) => LoginPage(),
      },
      onGenerateRoute: (s) {    // s的类型写不写都行,dart会自动识别泛型
        // TODO 如果要求登录,这里拦截,并给弹窗提示
        switch (s.name) {
          case "menu":
            return MaterialPageRoute(builder: (context) {
              return MenuPage();
            },
            settings: s);
            break;
          default:
        }
      },
      // initialRoute: "menu",  // 修改首页默认路由
    );
  }
}

你可能感兴趣的:(android,flutter,android)