mirror of https://github.com/flutter/samples.git
navigation_and_routing: a bunch of cleanup (#886)
Fail early on nullable context objects - no code paths allow null Eliminate superfluous private function Use a function over an abstract class with one methodpull/887/head
parent
ad6dc454f2
commit
ecf716dcab
@ -1,48 +0,0 @@
|
|||||||
// 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/foundation.dart';
|
|
||||||
import 'package:flutter/widgets.dart';
|
|
||||||
|
|
||||||
/// A mock authentication service
|
|
||||||
class BookstoreAuth extends ChangeNotifier {
|
|
||||||
bool _signedIn = false;
|
|
||||||
|
|
||||||
bool get signedIn => _signedIn;
|
|
||||||
|
|
||||||
Future<void> signOut() async {
|
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 200));
|
|
||||||
// Sign out.
|
|
||||||
_signedIn = false;
|
|
||||||
notifyListeners();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<bool> signIn(String username, String password) async {
|
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 200));
|
|
||||||
|
|
||||||
// Sign in. Allow any password.
|
|
||||||
_signedIn = true;
|
|
||||||
notifyListeners();
|
|
||||||
return _signedIn;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
bool operator ==(Object other) =>
|
|
||||||
other is BookstoreAuth && other._signedIn == _signedIn;
|
|
||||||
|
|
||||||
@override
|
|
||||||
int get hashCode => _signedIn.hashCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
class BookstoreAuthScope extends InheritedNotifier<BookstoreAuth> {
|
|
||||||
const BookstoreAuthScope({
|
|
||||||
required BookstoreAuth notifier,
|
|
||||||
required Widget child,
|
|
||||||
Key? key,
|
|
||||||
}) : super(key: key, notifier: notifier, child: child);
|
|
||||||
|
|
||||||
static BookstoreAuth? of(BuildContext context) => context
|
|
||||||
.dependOnInheritedWidgetOfExactType<BookstoreAuthScope>()
|
|
||||||
?.notifier;
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
// 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 '../routing.dart';
|
|
||||||
import 'auth.dart';
|
|
||||||
|
|
||||||
/// An implementation of [RouteGuard] that redirects to /signIn
|
|
||||||
class BookstoreRouteGuard implements RouteGuard<ParsedRoute> {
|
|
||||||
final BookstoreAuth auth;
|
|
||||||
|
|
||||||
BookstoreRouteGuard({
|
|
||||||
required this.auth,
|
|
||||||
});
|
|
||||||
|
|
||||||
/// Redirect to /signin if the user isn't signed in.
|
|
||||||
@override
|
|
||||||
Future<ParsedRoute> redirect(ParsedRoute from) async {
|
|
||||||
final signedIn = auth.signedIn;
|
|
||||||
final signInRoute = ParsedRoute('/signin', '/signin', {}, {});
|
|
||||||
|
|
||||||
// Go to /signin if the user is not signed in
|
|
||||||
if (!signedIn && from != signInRoute) {
|
|
||||||
return signInRoute;
|
|
||||||
}
|
|
||||||
// Go to /books if the user is signed in and tries to go to /signin.
|
|
||||||
else if (signedIn && from == signInRoute) {
|
|
||||||
return ParsedRoute('/books/popular', '/books/popular', {}, {});
|
|
||||||
}
|
|
||||||
return from;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue