Example 1

import 'package:dart_printf/dart_printf.dart';
import 'package:flutter/material.dart'; class Book {
final String title;
final String author; Book(this.title, this.author);
} List<Book> books = [
Book('Stranger in a Strange Land', 'Robert A. Heinlein'),
Book('Foundation', 'Isaac Asimov'),
Book('Fahrenheit 451', 'Ray Bradbury'),
]; void main() => runApp(MyApp()); class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => _MyAppState();
} class _MyAppState extends State<MyApp> {
Book _selectedBook; @override
Widget build(BuildContext context) {
return MaterialApp(
home: Navigator(
pages: [
MaterialPage(
child: BooksPage(books: books, onTapped: _handleBookTapped),
),
if (_selectedBook != null) BookDetailsPage(book: _selectedBook)
],
onPopPage: (route, result) {
printf("page result: %o", result);
if (!route.didPop(result)) {
return false;
} // 通过将_selectedBook设置为null来更新页面列表
setState(() {
_selectedBook = null;
}); return true;
},
),
);
} void _handleBookTapped(Book book) {
setState(() {
_selectedBook = book;
});
}
} class BookDetailsPage extends Page {
final Book book; BookDetailsPage({
this.book,
}) : super(key: ValueKey(book)); Route createRoute(BuildContext context) {
return MaterialPageRoute(
settings: this,
builder: (BuildContext context) {
return BookPage(book: book);
},
);
}
} class BooksPage extends StatelessWidget {
final List<Book> books;
final ValueChanged<Book> onTapped; BooksPage({
@required this.books,
@required this.onTapped,
}); @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('books')),
body: ListView(
children: [
for (var book in books)
ListTile(
title: Text(book.title),
subtitle: Text(book.author),
onTap: () => onTapped(book),
)
],
),
);
}
} class BookPage extends StatelessWidget {
final Book book; BookPage({@required this.book}); @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(book.title)),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Column(
children: [
Text(book.author),
RaisedButton(
onPressed: () => Navigator.of(context).pop(book.title),
child: Text('Pop'),
),
],
),
),
),
);
}
}

Example 2

import 'package:dart_printf/dart_printf.dart';
import 'package:flutter/material.dart'; class Book {
final String title;
final String author; Book(this.title, this.author);
} List<Book> books = [
Book('Stranger in a Strange Land', 'Robert A. Heinlein'),
Book('Foundation', 'Isaac Asimov'),
Book('Fahrenheit 451', 'Ray Bradbury'),
];
void main() => runApp(MyApp()); class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => _MyAppState();
} class _MyAppState extends State<MyApp> {
BookRouterDelegate _routerDelegate = BookRouterDelegate();
BookRouteInformationParser _routeInformationParser =
BookRouteInformationParser(); @override
Widget build(BuildContext context) {
return MaterialApp.router(
routerDelegate: _routerDelegate,
routeInformationParser: _routeInformationParser,
);
}
} class BookRouteInformationParser extends RouteInformationParser<BookRoutePath> { /// 通常在web程序中手动更改url是会收到通知
@override
Future<BookRoutePath> parseRouteInformation(RouteInformation routeInformation) async {
printf('localtion: %s', routeInformation.location);
final uri = Uri.parse(routeInformation.location);
// Handle '/'
if (uri.pathSegments.length == 0) {
return BookRoutePath.home();
} // Handle '/book/:id'
if (uri.pathSegments.length == 2) {
if (uri.pathSegments[0] != 'book') return BookRoutePath.unknown();
var remaining = uri.pathSegments[1];
var id = int.tryParse(remaining);
if (id == null) return BookRoutePath.unknown();
return BookRoutePath.details(id);
} // Handle unknown routes
return BookRoutePath.unknown();
} @override
RouteInformation restoreRouteInformation(BookRoutePath path) {
if (path.isUnknown) {
return RouteInformation(location: '/404');
}
if (path.isHomePage) {
return RouteInformation(location: '/');
}
if (path.isDetailsPage) {
return RouteInformation(location: '/book/${path.id}');
}
return null;
}
} class BookRouterDelegate extends RouterDelegate<BookRoutePath>
with ChangeNotifier, PopNavigatorRouterDelegateMixin<BookRoutePath> {
final GlobalKey<NavigatorState> navigatorKey; Book _selectedBook;
bool show404 = false; BookRouterDelegate() : navigatorKey = GlobalKey<NavigatorState>(); BookRoutePath get currentConfiguration {
if (show404) {
return BookRoutePath.unknown();
}
return _selectedBook == null
? BookRoutePath.home()
: BookRoutePath.details(books.indexOf(_selectedBook));
} @override
Widget build(BuildContext context) {
return Navigator(
key: navigatorKey,
pages: [
MaterialPage(
key: ValueKey('BooksListPage'),
child: BooksPage(books: books, onTapped: _handleBookTapped),
),
if (show404)
MaterialPage(key: ValueKey('UnknownPage'), child: UnknownScreen())
else if (_selectedBook != null)
BookDetailsPage(book: _selectedBook)
],
onPopPage: (route, result) {
printf("[page result] %o", result);
if (!route.didPop(result)) {
return false;
} // 通过将_selectedBook设置为null来更新页面列表
_selectedBook = null;
show404 = false;
notifyListeners(); return true;
},
);
} @override
Future<void> setNewRoutePath(BookRoutePath path) async {
printf('path id: %o', path.id);
if (path.isUnknown) {
_selectedBook = null;
show404 = true;
return;
} if (path.isDetailsPage) {
if (path.id < 0 || path.id > books.length - 1) {
show404 = true;
return;
} _selectedBook = books[path.id];
} else {
_selectedBook = null;
} show404 = false;
} void _handleBookTapped(Book book) {
_selectedBook = book;
notifyListeners();
}
} class BookDetailsPage extends Page {
final Book book; BookDetailsPage({
this.book,
}) : super(key: ValueKey(book)); Route createRoute(BuildContext context) {
return MaterialPageRoute(
settings: this,
builder: (BuildContext context) {
return BookPage(book: book);
},
);
}
} class BookRoutePath {
final int id;
final bool isUnknown; BookRoutePath.home()
: id = null,
isUnknown = false; BookRoutePath.details(this.id) : isUnknown = false; BookRoutePath.unknown()
: id = null,
isUnknown = true; bool get isHomePage => id == null; bool get isDetailsPage => id != null;
} class BooksPage extends StatelessWidget {
final List<Book> books;
final ValueChanged<Book> onTapped; BooksPage({
@required this.books,
@required this.onTapped,
}); @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('books')),
body: ListView(
children: [
for (var book in books)
ListTile(
title: Text(book.title),
subtitle: Text(book.author),
onTap: () => onTapped(book),
)
],
),
);
}
} class BookPage extends StatelessWidget {
final Book book; BookPage({@required this.book}); @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(book.title)),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Column(
children: [
Text(book.author),
RaisedButton(
onPressed: () => Navigator.of(context).pop(book.title),
child: Text('Pop'),
),
],
),
),
),
);
}
} class UnknownScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('404')),
body: Center(
child: Text('404!'),
),
);
}
}

See also:

Flutter Navigator2.0的更多相关文章

  1. Flutter 1.0 正式版: Google 的便携 UI 工具包

    Flutter 1.0 正式版: Google 的便携 UI 工具包 文 / Tim Sneath,Google Dart & Flutter 产品组产品经理 Flutter 是 Google ...

  2. Flutter 1.0 正式版: Google 的便携 UI 工具包

    简评:所以 React-Native 和 Flutter 该怎么选? 在 10 个月前的 MWC 上,谷歌发布了 Flutter 的 Beta 版本,给跨平台应用开发带来了一种全新的选择,昨天谷歌正式 ...

  3. Flutter 1.0 正式版: Google 的跨平台 UI 工具包

    今天我们非常高兴的宣布,Flutter 的 1.0 版本正式发布!Flutter 是 Google 为您打造的 UI 工具包,帮助您通过一套代码同时在 iOS 和 Android 上构建媲美原生体验的 ...

  4. 【老孟Flutter】Flutter 2.0 重磅更新

    老孟导读:昨天期待已久的 Flutter 2.0 终于发布了,Web 端终于提正了,春季期间我发布的一篇文章,其中的一个预测就是 Web 正式发布,已经实现了,还有一个预测是:2021年将是 Flut ...

  5. XD to Flutter 2.0 现已发布!

    Flutter 是 Google 的开源 UI 工具包.利用它,只需一套代码库,就能开发出适合移动设备.桌面设备.嵌入式设备以及 web 等多个平台的精美应用.过去几年,对于想要打造多平台应用的开发者 ...

  6. 写在Flutter 1.0之前

    开始 大概有半年没有写东西了,感觉时间过得飞快,18年也过一小半,趁着谷歌大会再为Flutter这个耀眼的新星,再吹一波! 都beta3了,1.0还会远吗 Flutter团队依然不紧不慢,一步一个脚印 ...

  7. 学习Flutter从0开始

    一. 认识Flutter 1.1. 什么是Flutter 先看看官方的解释: Flutter is Google's UI toolkit for building beautiful, native ...

  8. 颤振错误:当前Flutter SDK版本为2.1.0-dev.0.0.flutter-be6309690f?

    我刚刚升级了我的扑动,升级后我无法在Android Studio上运行任何扑动项目.我收到此错误消息. The current Dart SDK version -dev.0.0.flutter-be ...

  9. 编写第一个Flutter App(翻译)

    博客搬迁至http://blog.wangjiegulu.com RSS订阅:http://blog.wangjiegulu.com/feed.xml 以下代码 Github 地址:https://g ...

随机推荐

  1. Python基础(if语句、运算符)

    if语句的简单用法 每条if 语句的核心都是一个值为True 或False 的表达式 (简称条件测试),python根据条件测试的值来判断是否执行if语句后面的代码块,如果为true,执行:为fals ...

  2. 20200927gryz校赛心得

    今天gyh学长给我们办了一场校内模拟赛,特地跑来记录一下心得 昨天晚上问了一下lkp学长,听说题目不卡常,不毒瘤,因此我在考试前20分钟仍在若无其事的练习着刚学的强连通分量,丝毫不慌 结果虽然rank ...

  3. Excel 快速填充:填充柄+数据验证

    鼠标左键拖拽填充或者双击填充 右键拖拽填充: 可以填充等比数列.工作日等等 数据验证: 通过下拉箭头快速选择数据: 选择单元格区域-[数据]-[数据验证]-序列 数据科学交流群,群号:18915878 ...

  4. Spark练习之wordcount,基于排序机制的wordcount

    Spark练习之wordcount 一.原理及其剖析 二.pom.xml 三.使用Java进行spark的wordcount练习 四.使用scala进行spark的wordcount练习 五.基于排序 ...

  5. Java链表(英雄增删查改)

    链表(Linked List)介绍 链表是有序的列表,但是它在内存中是存储如下 小结: 1.链表是以节点的方式来存储,是链式存储. 2.每个节点包含 data 域, next 域:指向下一个节点. 3 ...

  6. Azure Functions(一)什么是 ServerLess

    一,引言 自去年4月份分享过3篇关于 Azure Functions 的文章之后,就一直没有再将 Azure Functions 相关的内容了.今天再次开始将 Azure Functions 相关的课 ...

  7. 【poj 2478】Farey Sequence(数论--欧拉函数 找规律求前缀和)

    题意:定义 Fn 序列表示一串 <1 的分数,分数为最简分数,且分母 ≤n .问该序列的个数.(2≤N≤10^6) 解法:先暴力找规律(代码见屏蔽处),发现 Fn 序列的个数就是 Φ(1)~Φ( ...

  8. 第 45 届国际大学生程序设计竞赛(ICPC)亚洲网上区域赛模拟赛. A.Easy Equation (前缀和/差分)

    题意:RT,给你四个数\(a,b,c,d\),求\(x+y+z=k\)的方案数. 题解:我们可以先枚举\(x\)的值,然后\(x+y\)能取到的范围一定是\([x,x+b]\),也就是说这个区间内每个 ...

  9. AtCoder Beginner Contest 170 D - Not Divisible (数学)

    题意:有一长度为\(n\)的数组,求该数组中有多少元素不能整除其它任一元素的个数. 题解:刚开始写了个分解质因数(我是傻逼),后来发现直接暴力枚举因子即可,注意某个元素出现多次时肯定不满足情况,再特判 ...

  10. WSL2 VS Code远程开发准备

    上一节我们在linux中创建了mvc项目,但是要是在linux中用命令行直接开发的话,就有些扯了. 我们可以使用VS Code进行远程开发,简单来说,就是在windows中打开VS Code,打开Li ...