1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 22:09:06 +00:00
Files
samples/navigation_and_routing/lib/src/routing/route_state.dart
Kevin Moore fa7dec1475 Cleanup to navigation_and_routing sample (#881)
- make more things private and final, where possible
- remove unused members
- used expression bodies, where possible
2021-08-26 09:16:27 -07:00

50 lines
1.4 KiB
Dart

// 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';
import 'parsed_route.dart';
import 'parser.dart';
/// The current route state. To change the current route, call obtain the state
/// using `RouteStateScope.of(context)` and call `go()`:
///
/// ```
/// RouteStateScope.of(context).go('/book/2');
/// ```
class RouteState extends ChangeNotifier {
TemplateRouteParser parser;
ParsedRoute _route;
RouteState(this.parser) : _route = parser.initialRoute;
ParsedRoute get route => _route;
set route(ParsedRoute route) {
// Don't notify listeners if the path hasn't changed.
if (_route == route) return;
_route = route;
notifyListeners();
}
Future<void> go(String route) async {
this.route =
await parser.parseRouteInformation(RouteInformation(location: route));
}
}
/// Provides the current [RouteState] to descendent widgets in the tree.
class RouteStateScope extends InheritedNotifier<RouteState> {
const RouteStateScope({
required RouteState notifier,
required Widget child,
Key? key,
}) : super(key: key, notifier: notifier, child: child);
static RouteState? of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<RouteStateScope>()?.notifier;
}