[Flutter] Router Navigation

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Flutter] Router Navigation相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/class' title='class'>class   col   cannot   ret   red   first   code   build   title   

Basic navigation by using 鈥楴avigator.push鈥?& 鈥楴avigator.pop()鈥? for example, we have two screen, screen1 and screen2, we want to navigate between two screens:

// Screen1.dart

import 鈥?/span>package:flutter/material.dart鈥?/span>;
import 鈥?/span>screen2.dart鈥?/span>;

class Screen1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.red,
        title: Text(鈥?/span>Screen 1鈥?/span>),
      ),
      body: Center(
        child: RaisedButton(
          color: Colors.red,
          child: Text(鈥?/span>Go Forwards To Screen 2鈥?/span>),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) {
                return Screen2();
              }),
            );
          },
        ),
      ),
    );
  }
}

 

Screen2.dart:

import 鈥?/span>package:flutter/material.dart鈥?/span>;

class Screen2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.blue,
        title: Text(鈥?/span>Screen 2鈥?/span>),
      ),
      body: Center(
        child: RaisedButton(
          color: Colors.blue,
          child: Text(鈥?/span>Go Back To Screen 1鈥?/span>),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
    );
  }
}

 

Using the named route:

In main.dart, we can list all the routes:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // home: Screen0(),
      initialRoute: 鈥?/span>/鈥?/span>,
      routes: {
        鈥?鈥? (context) => Screen0(),
        鈥?first鈥? (context) => Screen1(),
        鈥?second鈥? (context) => Screen2()
      },
    );
  }
}

To be noticed: 鈥榟ome鈥?& 鈥榠nitialRoute鈥?cannot be used at the same time.

 

       child: Column(
          children: <Widget>[
            RaisedButton(
              color: Colors.red,
              child: Text(鈥?/span>Go To Screen 1鈥?/span>),
              onPressed: () {
                Navigator.pushNamed(context, 鈥?first鈥?/span>);
              },
            ),
            RaisedButton(
              color: Colors.blue,
              child: Text(鈥?/span>Go To Screen 2鈥?/span>),
              onPressed: () {
                Navigator.pushNamed(context, 鈥?second鈥?/span>);
              },
            ),
          ],
        ),

 

以上是关于[Flutter] Router Navigation的主要内容,如果未能解决你的问题,请参考以下文章

Flutter实战之go_router路由组件入门指南

Flutter Boost的router管理

Flutter Boost的router管理

译学习Flutter中新的Navigator和Router系统

[Flutter] Router Navigation

css PureCSS菜单进入WordPress(http://corpocrat.com/2015/08/28/how-to-integrate-purecss-navigation-menu-in