Instantly share code, notes, and snippets.
Created
December 9, 2021 08:01
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save chonghorizons/c43ef845b7c5b2b4527d939ef535c41a to your computer and use it in GitHub Desktop.
Fixing the Medium writeup linked from Navigator2/Router desciption page. https://medium.com/flutter/learning-flutters-new-navigation-and-routing-system-7c9068155ade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
// based on https://medium.com/flutter/learning-flutters-new-navigation-and-routing-system-7c9068155ade | |
// but update for null safety by Howard "Horizons" Chong, @chonghorizons on github | |
void main() { | |
runApp(BooksApp()); | |
} | |
class Book { | |
final String title; | |
final String author; | |
Book(this.title, this.author); | |
Book.empty() | |
: title = '', | |
author = ''; | |
bool isEmpty() => title == ''; | |
} | |
class BooksApp extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() => _BooksAppState(); | |
} | |
class _BooksAppState extends State<BooksApp> { | |
BookRouterDelegate _routerDelegate = BookRouterDelegate(); | |
BookRouteInformationParser _routeInformationParser = | |
BookRouteInformationParser(); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp.router( | |
title: 'Books App', | |
routerDelegate: _routerDelegate, | |
routeInformationParser: _routeInformationParser, | |
); | |
} | |
} | |
class BookRouteInformationParser extends RouteInformationParser<BookRoutePath> { | |
@override | |
Future<BookRoutePath> parseRouteInformation( | |
RouteInformation routeInformation) async { | |
print('BookRouteInformationParser'); | |
print(routeInformation.location); | |
if (routeInformation.location == null) { | |
print('Null RouteInformation on startup'); | |
} | |
final uri = Uri.parse(routeInformation.location ?? | |
'/'); // could be empty, I think it is empty on opp launch | |
// 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) { | |
print('restoring'); | |
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 = Book.empty(); | |
bool show404 = false; | |
List<Book> books = [ | |
Book('Left Hand of Darkness', 'Ursula K. Le Guin'), | |
Book('Too Like the Lightning', 'Ada Palmer'), | |
Book('Kindred', 'Octavia E. Butler'), | |
]; | |
BookRouterDelegate() : navigatorKey = GlobalKey<NavigatorState>(); | |
BookRoutePath get currentConfiguration { | |
if (show404) { | |
return BookRoutePath.unknown(); | |
} | |
return (_selectedBook.isEmpty()) | |
? BookRoutePath.home() | |
: BookRoutePath.details(books.indexOf(_selectedBook)); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Navigator( | |
key: navigatorKey, | |
pages: [ | |
MaterialPage( | |
key: ValueKey('BooksListPage'), | |
child: BooksListScreen( | |
books: books, | |
onTapped: _handleBookTapped, | |
), | |
), | |
if (show404) | |
MaterialPage(key: ValueKey('UnknownPage'), child: UnknownScreen()) | |
else if (!_selectedBook.isEmpty()) | |
BookDetailsPage(book: _selectedBook) | |
], | |
onPopPage: (route, result) { | |
if (!route.didPop(result)) { | |
return false; | |
} | |
// Update the list of pages by setting _selectedBook to empty | |
_selectedBook = Book.empty(); | |
show404 = false; | |
notifyListeners(); | |
return true; | |
}, | |
); | |
} | |
@override | |
Future<void> setNewRoutePath(BookRoutePath path) async { | |
if (path.isUnknown) { | |
_selectedBook = Book.empty(); | |
// noBook = true; | |
show404 = true; | |
return; | |
} | |
if (path.isDetailsPage) { | |
// if (path.id < 0 || path.id > books.length - 1) { | |
if (path.id! < 0 || path.id! > books.length - 1) { | |
show404 = true; | |
// noBook = true; | |
return; | |
} | |
_selectedBook = books[path.id!]; | |
} else { | |
_selectedBook = Book.empty(); | |
} | |
show404 = false; | |
} | |
void _handleBookTapped(Book book) { | |
_selectedBook = book; | |
// noBook = false; | |
notifyListeners(); | |
} | |
} | |
class BookDetailsPage extends Page { | |
final Book book; | |
BookDetailsPage({required this.book}) : super(key: ValueKey(book)); | |
Route createRoute(BuildContext context) { | |
return MaterialPageRoute( | |
settings: this, | |
builder: (BuildContext context) { | |
return BookDetailsScreen(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 BooksListScreen extends StatelessWidget { | |
final List<Book> books; | |
final ValueChanged<Book> onTapped; | |
BooksListScreen({ | |
required this.books, | |
required this.onTapped, | |
}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: ListView( | |
children: [ | |
for (var book in books) | |
ListTile( | |
title: Text(book.title), | |
subtitle: Text(book.author), | |
onTap: () => onTapped(book), | |
) | |
], | |
), | |
); | |
} | |
} | |
class BookDetailsScreen extends StatelessWidget { | |
final Book book; | |
BookDetailsScreen({ | |
required this.book, | |
}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
if (book != null) ...[ | |
Text(book.title, style: Theme.of(context).textTheme.headline6), | |
Text(book.author, style: Theme.of(context).textTheme.subtitle1), | |
], | |
], | |
), | |
), | |
); | |
} | |
} | |
class UnknownScreen extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: Center( | |
child: Text('404!'), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment