I. Direct jump in push mode

Ordinary jump

Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => Page1()));
Copy the code

Jump and receive parameters with parameters

      Navigator.push(context,
          MaterialPageRoute(
              builder: (BuildContext context) => Page2(),
              settings: RouteSettings(name: "Route name",arguments: "This is the parameter passed in from page 2.")));Copy the code
final String message = ModalRoute.of(context).settings.arguments as String;
Copy the code

The constructor takes arguments directly

Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => Page3("This is the parameter that was passed in."))); Final String message = modalroute.of (context).settings.arguments as String;Copy the code

Returns with parameters after the jump

      Future result = Navigator.of(context).push(MaterialPageRoute(
          builder: (ctx) {
            return Page3("Parameters passed in, hit return, return with parameters."); })); Result. then((res) {setState(() {
          _handleMessage = res;
        });
      });
Copy the code
// Return the argument navigator.of (context).pop("This is the parameter returned to carry.");
Copy the code











2. Named route jump

Configure routes on the entry page

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo', routes: wgrouter. routes, initialRoute: wgrouter. main,// WGRouter.generateRoute, onUnknownRoute: WGRouter.unknownRoute, // home: MainPage(), ); }}Copy the code
class WGRouter {

  static final main = 'main';
  static final pathPage1 = 'page_1';
  static final pathPage2 = 'page_2';
  static final pathPage3 = 'page_3';

  static final Map<String, WidgetBuilder> routes = {
    main: (ctx) => MainPage(),
    pathPage1: (ctx){
      return Page1();
    },
    pathPage2: (ctx) => Page2(),
  };


  static final RouteFactory generateRoute = (settings) {
    if (settings.name == pathPage3) {
      return MaterialPageRoute(
          builder: (ctx) {
            returnPage3(settings.arguments); }); }return null;
  };

  static final RouteFactory unknownRoute = (settings) {
    return MaterialPageRoute(
        builder: (ctx) {
          returnWGUnknownPage(); }); }; }Copy the code

Named route jump

Navigator.of(context).pushNamed(WGRouter.pathPage1);
Copy the code

Named routes carry parameter jumps

Navigator.of(context).pushNamed(WGRouter.pathPage2,arguments:"Parameters carried by the named Route");
Copy the code

Iii. Complete code and display diagram

class MainPage extends StatefulWidget {

  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {

  final _titles = ['Normal jump'.'Normal jump with parameters'.'Constructor passes arguments directly'.'Return with parameters'.'Named Route'."Named Route with Parameters".'the generate routing'.'Undefined page'];

  var _handleMessage = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('home'),),
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Text(_handleMessage),
            buildJumpButton(context,_titles[0]),
            buildJumpButton(context,_titles[1]),
            buildJumpButton(context,_titles[2]),
            buildJumpButton(context,_titles[3]),
            buildJumpButton(context,_titles[4]),
            buildJumpButton(context,_titles[5]),
            buildJumpButton(context,_titles[6]),
            buildJumpButton(context,_titles[7]),
          ],
        ),
      ),
    );
  }



  Container buildJumpButton(BuildContext context,String title) {
    return Container(
      width: 300,
      padding: EdgeInsets.only(top: 30),
      child: CupertinoButton(
        child: Text(title),
        color: Color.fromARGB(255, Random.secure().nextInt(255), Random.secure().nextInt(255), Random.secure().nextInt(255)),
        onPressed:()=>_jumpToPage(context,title),
      ),
    );
  }




  void _jumpToPage(BuildContext context,String title) {


    if(title == _titles[0]){
      Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => Page1()));
//      Navigator.push(context, CupertinoPageRoute(builder: (BuildContext context) => Page1()));
//      Navigator.push(context, PageRouteBuilder(builder: (BuildContext context) => Page1()));
      return;
    }

    if(title == _titles[1]){
      Navigator.push(context,
          MaterialPageRoute(
              builder: (BuildContext context) => Page2(),
              settings: RouteSettings(name: "Route name",arguments: "This is the parameter passed in from page 2.")));return;
    }

    if(title == _titles[2]){
      Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => Page3("This is the parameter that was passed in.")));
      return;
    }

    if(title == _titles[3]){
      Future result = Navigator.of(context).push(MaterialPageRoute(
          builder: (ctx) {
            return Page3("Parameters passed in, hit return, return with parameters."); })); result.then((res) {setState(() {
          _handleMessage = res;
        });
      });
      return;
    }

    if(title == _titles[4]){
      Navigator.of(context).pushNamed(WGRouter.pathPage1);
      return;
    }

    if(title == _titles[5]){
      Navigator.of(context).pushNamed(WGRouter.pathPage2,arguments:"Parameters carried by the named Route");
      return;
    }

    if(title == _titles[6]){
      Navigator.of(context).pushNamed(WGRouter.pathPage3,arguments:"Generate route jump");
      return;
    }

    if(title == _titles[7]){
      Navigator.of(context).pushNamed("God knows what path this is.");
      return; }}}Copy the code