mirror of
https://github.com/flutter/samples.git
synced 2026-04-02 17:52:56 +00:00
Replace navigation_and_routing with a new sample (#832)
* move snippets into old_snippets directory * add new navigation_and_routing sample * add copyright headers * Apply #827 to old_snippets/ directory and upgrade them to null safety * Code review comments - Move Guard class into parser.dart - Move usage of guards from Delegate to RouteInformationParser - Rename delegate to SimpleRouterDelegate * clean up imports * refactor settings screen, fix bug * avoid conflicting paths /books/new and /books/1 - rename to book/1 * dispose fields in _BookstoreState class * remove /books path This was causing problems * add comment * Change BookstoreAuthScope and BookstoreAuthScope to InheritedNotifier * fix warnings * Make the initial route configurable, set to '/signin' * Enable deep linking https://flutter.dev/docs/development/ui/navigation/deep-linking * use path URL strategy on the web. * remove TODO, add comment
This commit is contained in:
36
navigation_and_routing/lib/src/widgets/author_list.dart
Normal file
36
navigation_and_routing/lib/src/widgets/author_list.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright 2021, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../data.dart';
|
||||
|
||||
class AuthorList extends StatelessWidget {
|
||||
final List<Author> authors;
|
||||
final ValueChanged<Author>? onTap;
|
||||
|
||||
const AuthorList({
|
||||
required this.authors,
|
||||
this.onTap,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView.builder(
|
||||
itemCount: authors.length,
|
||||
itemBuilder: (context, index) {
|
||||
return ListTile(
|
||||
title: Text(
|
||||
authors[index].name,
|
||||
),
|
||||
subtitle: Text(
|
||||
'${authors[index].books.length} books',
|
||||
),
|
||||
onTap: onTap != null ? () => onTap!(authors[index]) : null,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
36
navigation_and_routing/lib/src/widgets/book_list.dart
Normal file
36
navigation_and_routing/lib/src/widgets/book_list.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright 2021, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../data.dart';
|
||||
|
||||
class BookList extends StatelessWidget {
|
||||
final List<Book> books;
|
||||
final ValueChanged<Book>? onTap;
|
||||
|
||||
const BookList({
|
||||
required this.books,
|
||||
this.onTap,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView.builder(
|
||||
itemCount: books.length,
|
||||
itemBuilder: (context, index) {
|
||||
return ListTile(
|
||||
title: Text(
|
||||
books[index].title,
|
||||
),
|
||||
subtitle: Text(
|
||||
books[index].author.name,
|
||||
),
|
||||
onTap: onTap != null ? () => onTap!(books[index]) : null,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright 2021, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class FadeTransitionPage<T> extends Page<T> {
|
||||
final Widget child;
|
||||
|
||||
const FadeTransitionPage({LocalKey? key, required this.child})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
Route<T> createRoute(BuildContext context) {
|
||||
return PageBasedFadeTransitionRoute<T>(this);
|
||||
}
|
||||
}
|
||||
|
||||
class PageBasedFadeTransitionRoute<T> extends PageRoute<T> {
|
||||
PageBasedFadeTransitionRoute(Page page)
|
||||
: super(
|
||||
settings: page,
|
||||
);
|
||||
|
||||
@override
|
||||
Color? get barrierColor => null;
|
||||
|
||||
@override
|
||||
String? get barrierLabel => null;
|
||||
|
||||
@override
|
||||
Duration get transitionDuration => const Duration(milliseconds: 300);
|
||||
|
||||
@override
|
||||
bool get maintainState => true;
|
||||
|
||||
@override
|
||||
Widget buildPage(BuildContext context, Animation<double> animation,
|
||||
Animation<double> secondaryAnimation) {
|
||||
var curveTween = CurveTween(curve: Curves.easeIn);
|
||||
return FadeTransition(
|
||||
opacity: animation.drive(curveTween),
|
||||
child: (settings as FadeTransitionPage).child,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget buildTransitions(BuildContext context, Animation<double> animation,
|
||||
Animation<double> secondaryAnimation, Widget child) {
|
||||
return child;
|
||||
}
|
||||
}
|
||||
25
navigation_and_routing/lib/src/widgets/library_scope.dart
Normal file
25
navigation_and_routing/lib/src/widgets/library_scope.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright 2021, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../data.dart';
|
||||
|
||||
class LibraryScope extends InheritedWidget {
|
||||
final Library library;
|
||||
|
||||
const LibraryScope({
|
||||
Key? key,
|
||||
required this.library,
|
||||
required Widget child,
|
||||
}) : super(key: key, child: child);
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(LibraryScope oldWidget) =>
|
||||
library != oldWidget.library;
|
||||
|
||||
static Library of(BuildContext context) {
|
||||
return context.dependOnInheritedWidgetOfExactType<LibraryScope>()!.library;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user