Flutter 中的路由通俗的讲就是页面跳转。在 Flutter 中通过 Navigator 组件管理路由导航,并提供了管理堆栈的方法。如:Navigator.push 和 Navigator.pop。Flutter 中给我们提供了两种配置路由跳转的方式:1、基本路由 2、命名路由 

项目准备

由于页面跳转需要有多个页面,所以在项目开始前,需要准备多个页面,这里是复用了前面导航项目,然后在pages文件夹下面添加Form.dart和Search.dart两个文件。

Search.dart

import 'package:flutter/material.dart';

class SearchPage extends StatelessWidget {
const SearchPage({Key key}) : super(key: key); @override
Widget build(BuildContext context) {
return Scaffold(
appBar:AppBar(
title: Text("搜索页面"),
) ,
body: Text("搜索页面内容区域"),
);
}
}

基本路由

首先实现基本的页面跳转:在HomPage中点击按钮,页面跳转到SearchPage。要完成上述过程,需要分三步

  1. 需要在 HomPage 中引入 SearchPage.dart 
  2. 触发事件
  3. 页面跳转
import 'package:flutter/material.dart';

import '../Search.dart';

class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key); _HomePageState createState() => _HomePageState();
} class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text("跳转到搜索页面"),
onPressed: () {
//路由跳转
Navigator.of(context).push(
MaterialPageRoute(
builder: (context)=>SearchPage()
)
); },
color: Theme.of(context).accentColor,
textTheme: ButtonTextTheme.primary
),
SizedBox(height: 20), ],
);
}
}

基本路由跳转传值

上面仅仅是实现了页面跳转,但是在很多情况下,页面跳转时伴随着数据传递的,下面,实现从CategoryPage跳转到Form.dart页面,并且传递相关数据。

首先需要在CategoryPage页面中进行页面跳转时,写入需要传递的值

Category.dart

import 'package:flutter/material.dart';

import '../Form.dart';

class CategoryPage extends StatefulWidget {
CategoryPage({Key key}) : super(key: key); _CategoryPageState createState() => _CategoryPageState();
} class _CategoryPageState extends State<CategoryPage> {
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text("跳转到表单页面"),
onPressed: (){ Navigator.of(context).push(
MaterialPageRoute(
builder: (context)=>FormPage(title:'我是跳转传值')
)
);
},
)
],
);
}
}

然后在Form.dart中获取传递过来的值。

需要注意的是,这里在获取页面跳转传值时,不同的写法有着不同的作用:

这种写法代表title为可选传值,拥有默认值。

这种写法代表title为必传参数。

Form.dart

import 'package:flutter/material.dart';

class FormPage extends StatelessWidget {

  String title;
FormPage({this.title="表单"}); @override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Text('返回'),
onPressed: (){
Navigator.of(context).pop();
},
),
appBar: AppBar(
title: Text(this.title),
),
body: ListView(
children: <Widget>[ ListTile(
title: Text('我是表单页面'),
),
ListTile(
title: Text('我是表单页面'),
),
ListTile(
title: Text('我是表单页面'),
),
ListTile(
title: Text('我是表单页面'),
)
],
),
);
}
}

上面的例子中,还在Form.dart中添加了一个返回路由的按钮。

   

代码下载:点这里(747f)

Flutter 中的基本路由的更多相关文章

  1. Flutter中的普通路由与命名路由(Navigator组件)

    Flutter 中的路由通俗的讲就是页面跳转.在 Flutter 中通过 Navigator 组件管理路由导航.并提供了管理堆栈的方法.如:Navigator.push 和 Navigator.pop ...

  2. flutter中的命名路由

    命名路由是区别于基本路由的一种存在,方便于大型项目中路由的统一管理,现在,在前面基本路由的项目基础上实现实现命名路由. 使用步骤 路由配置 命名路由在使用前,需要在根组件main.dart中进行简单的 ...

  3. Flutter中的替换路由、返回到根路由

    替换路由 当我们有三个页面,页面1,页面2,页面3. 期望点击页面1按钮,跳转到页面2,页面2点击返回,跳转到页面1: 点击页面2按钮,跳转到页面3,页面3点击返回,跳转到页面1,而不是页面2. 这时 ...

  4. Flutter中管理路由栈的方法和应用

    原文地址:https://www.jianshu.com/p/5df089d360e4 本文首先讲的Flutter中的路由,然后主要讲下Flutter中栈管理的几种方法. 了解下Route和Navig ...

  5. Flutter 中的路由

    Flutter 中的路由通俗的讲就是页面跳转.在 Flutter 中通过 Navigator 组件管理路由导航. 并提供了管理堆栈的方法.如:Navigator.push 和 Navigator.po ...

  6. 【老孟Flutter】Flutter 中与平台相关的生命周期

    老孟导读:关于生命周期的文章共有2篇,一篇(此篇)是介绍 Flutter 中Stateful 组件的生命周期. 第二篇是 Flutter 中与平台相关的生命周期, 博客地址:http://laomen ...

  7. flutter中的路由跳转

    在前面的基本路由和命名路由中,都演示了如何进行路由跳转,并且在路由跳转以后,可以借用系统自带的按钮就行返回上一级,当然了,也可以自定义按钮返回上一级. 返回上一级 在前面的例子中,当从Home.dar ...

  8. flutter 中的AppBar

    在flutter中的很多页面中,都会有下面这段代码: 对应就是下图中的红色线框区域,被称作AppBar顶部导航. 项目准备 在使用AppBar之前,我们先新建一个tabBar的项目: 然后在pages ...

  9. flutter中使用redux之多界面互动

    在上一篇文章,我们介绍了如何在flutter中使用redux.在上一篇文章的例子中,我们使用了单界面集成redux,但是在实际项目中,我们通常涉及多个模块,每个模块涉及多个界面,那么如何使用redux ...

随机推荐

  1. The server time zone value 'EDT' is unrecognized or represents more than one time zone

    解决: (1)使用 server mysql start命令启动mysql (2)在mysql中执行show variables like '%time_zone%'; (3)输入select now ...

  2. python2.7+RobotFramework的UI自动化环境搭建

    robotFramework是一种比较常见的自动化测试框架,此篇记录环境搭建 目录 1.软件准备 2.执行安装 1.软件准备 python-2.7.15.amd64.msi              ...

  3. pycharm5.0.4简易使用说明

    前言:学习自动化,需要使用pycharm,以下是简易使用说明 1.注册破解 2.行号和背景色 3.打断点 1.注册破解 打开pycharm5.0.4,点击菜单栏的help->register.. ...

  4. 用C#调用C++DLL提示找不到DLL解决方法【转】

    用C#调用自己写的C++ DLL(x64),总是提示找不到DLL,调试可以,发布release老是提示找不到DLL(dll文件确定存在) 原因:Visual C++的DLL分发方式没选:调试默认选择: ...

  5. stringstream流分割空格

    1205 单词翻转 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze       题目描述 Description 给出一个英语句子,希望你把句子里的单词顺序都翻转 ...

  6. scite配置文件及常用设置

    在linux系统中,SciTE的用户设置文件为 ~/.SciTEUser.properties,优先级高于全局配置文件. scite是个不错的IDE工具,只是本人发现,在开发团队中和其他成员的编辑工具 ...

  7. Pandas的拼接操作

    pandas的拼接操作 pandas的拼接分为两种: 级联:pd.concat, pd.append 合并:pd.merge, pd.join import pandas as pd import n ...

  8. ptmx

    ptmx DESCRIPTION The file /dev/ptmx is a character file with major number 5 and minor number 2, usua ...

  9. 开源企业IM-免费企业即时通讯-ENTBOOST V2014.183 Linux版本号正式公布

    版权声明:本文为博主原创文章,欢迎转载,转载请尽量保持原文章完整,谢谢! https://blog.csdn.net/yanghz/article/details/37807975 ENTBOOST, ...

  10. angularjs calling order

    Here's the calling order: app.config()app.run()directive's compile functions (if they are found in t ...