|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/widgets.dart'; |
| 6 | +import 'package:meta/meta.dart'; |
| 7 | +import 'package:meta/meta_meta.dart'; |
| 8 | + |
| 9 | +import 'go_route.dart'; |
| 10 | +import 'go_router_state.dart'; |
| 11 | + |
| 12 | +/// Baseclass for supporting |
| 13 | +/// [typed routing](https://gorouter.dev/typed-routing). |
| 14 | +/// |
| 15 | +/// Subclasses must override one of [build], [buildPage], or [redirect]. |
| 16 | +abstract class GoRouteData { |
| 17 | + /// Allows subclasses to have `const` constructors. |
| 18 | + /// |
| 19 | + /// [GoRouteData] is abstract and cannot be instantiated directly. |
| 20 | + const GoRouteData(); |
| 21 | + |
| 22 | + /// Creates the [Widget] for `this` route. |
| 23 | + /// |
| 24 | + /// Subclasses must override one of [build], [buildPage], or [redirect]. |
| 25 | + /// |
| 26 | + /// Corresponds to [GoRoute.builder]. |
| 27 | + Widget build(BuildContext context) => throw UnimplementedError( |
| 28 | + 'One of `build` or `buildPage` must be implemented.', |
| 29 | + ); |
| 30 | + |
| 31 | + /// A page builder for this route. |
| 32 | + /// |
| 33 | + /// Subclasses can override this function to provide a custom [Page]. |
| 34 | + /// |
| 35 | + /// Subclasses must override one of [build], [buildPage], or [redirect]. |
| 36 | + /// |
| 37 | + /// Corresponds to [GoRoute.pageBuilder]. |
| 38 | + /// |
| 39 | + /// By default, returns a [Page] instance that is ignored, causing a default |
| 40 | + /// [Page] implementation to be used with the results of [build]. |
| 41 | + Page<void> buildPage(BuildContext context) => const NoOpPage(); |
| 42 | + |
| 43 | + /// An optional redirect function for this route. |
| 44 | + /// |
| 45 | + /// Subclasses must override one of [build], [buildPage], or [redirect]. |
| 46 | + /// |
| 47 | + /// Corresponds to [GoRoute.redirect]. |
| 48 | + String? redirect() => null; |
| 49 | + |
| 50 | + /// A helper function used by generated code. |
| 51 | + /// |
| 52 | + /// Should not be used directly. |
| 53 | + static String $location(String path, {Map<String, String>? queryParams}) => |
| 54 | + Uri.parse(path) |
| 55 | + .replace( |
| 56 | + queryParameters: |
| 57 | + // Avoid `?` in generated location if `queryParams` is empty |
| 58 | + queryParams?.isNotEmpty == true ? queryParams : null, |
| 59 | + ) |
| 60 | + .toString(); |
| 61 | + |
| 62 | + /// A helper function used by generated code. |
| 63 | + /// |
| 64 | + /// Should not be used directly. |
| 65 | + static GoRoute $route<T extends GoRouteData>({ |
| 66 | + required String path, |
| 67 | + required T Function(GoRouterState) factory, |
| 68 | + List<GoRoute> routes = const <GoRoute>[], |
| 69 | + }) { |
| 70 | + T factoryImpl(GoRouterState state) { |
| 71 | + final Object? extra = state.extra; |
| 72 | + |
| 73 | + // If the "extra" value is of type `T` then we know it's the source |
| 74 | + // instance of `GoRouteData`, so it doesn't need to be recreated. |
| 75 | + if (extra is T) { |
| 76 | + return extra; |
| 77 | + } |
| 78 | + |
| 79 | + return (_stateObjectExpando[state] ??= factory(state)) as T; |
| 80 | + } |
| 81 | + |
| 82 | + Widget builder(BuildContext context, GoRouterState state) => |
| 83 | + factoryImpl(state).build(context); |
| 84 | + |
| 85 | + Page<void> pageBuilder(BuildContext context, GoRouterState state) => |
| 86 | + factoryImpl(state).buildPage(context); |
| 87 | + |
| 88 | + String? redirect(GoRouterState state) => factoryImpl(state).redirect(); |
| 89 | + |
| 90 | + return GoRoute( |
| 91 | + path: path, |
| 92 | + builder: builder, |
| 93 | + pageBuilder: pageBuilder, |
| 94 | + redirect: redirect, |
| 95 | + routes: routes, |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /// Used to cache [GoRouteData] that corresponds to a given [GoRouterState] |
| 100 | + /// to minimize the number of times it has to be deserialized. |
| 101 | + static final Expando<GoRouteData> _stateObjectExpando = Expando<GoRouteData>( |
| 102 | + 'GoRouteState to GoRouteData expando', |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +/// Annotation for types that support typed routing. |
| 107 | +@Target(<TargetKind>{TargetKind.library, TargetKind.classType}) |
| 108 | +class TypedGoRoute<T extends GoRouteData> { |
| 109 | + /// Instantiates a new instance of [TypedGoRoute]. |
| 110 | + const TypedGoRoute({ |
| 111 | + required this.path, |
| 112 | + this.routes = const <TypedGoRoute<GoRouteData>>[], |
| 113 | + }); |
| 114 | + |
| 115 | + /// The path that corresponds to this rout. |
| 116 | + /// |
| 117 | + /// See [GoRoute.path]. |
| 118 | + final String path; |
| 119 | + |
| 120 | + /// Child route definitions. |
| 121 | + /// |
| 122 | + /// See [GoRoute.routes]. |
| 123 | + final List<TypedGoRoute<GoRouteData>> routes; |
| 124 | +} |
| 125 | + |
| 126 | +/// Internal class used to signal that the default page behavior should be used. |
| 127 | +@internal |
| 128 | +class NoOpPage extends Page<void> { |
| 129 | + /// Creates an instance of NoOpPage; |
| 130 | + const NoOpPage(); |
| 131 | + |
| 132 | + @override |
| 133 | + Route<void> createRoute(BuildContext context) => |
| 134 | + throw UnsupportedError('Should never be called'); |
| 135 | +} |
0 commit comments