Line data Source code
1 : part of '../main.dart';
2 :
3 : /// A [VRouteElement] which enable nesting
4 : ///
5 : /// [widgetBuilder] gives you a [Widget] which is what you should use as the child to nest
6 : /// This [Widget] will be the one present in the [VRouteElement] in [nestedRoutes] corresponding
7 : /// to the current route
8 : ///
9 : /// {@tool snippet}
10 : ///
11 : /// If you want to nest ProfileWidget in MyScaffold at the path '/profile',
12 : /// here is what you can do:
13 : ///
14 : /// ```dart
15 : /// VNester(
16 : /// widgetBuilder: (child) => MyScaffold(child: child),
17 : /// nestedRoutes: [
18 : /// VWidget(
19 : /// path: '/profile',
20 : /// widget: ProfileWidget(),
21 : /// ),
22 : /// ],
23 : /// )
24 : /// ```
25 : /// {@end-tool}
26 : ///
27 : ///
28 : /// {@tool snippet}
29 : ///
30 : /// Note that you can also use stackedRoutes if you want to nest AND stack by using nestedRoutes
31 : /// AND stackedRoutes:
32 : ///
33 : /// ```dart
34 : /// VNester(
35 : /// path: '/home',
36 : /// widgetBuilder: (child) => MyScaffold(child: child),
37 : /// nestedRoutes: [
38 : /// VWidget(
39 : /// path: 'profile',
40 : /// alias: [':_(settings)'] // This is used because we want to display ProfileWidget while SettingsWidgets is on top of MyScaffold
41 : /// widget: ProfileWidget(),
42 : /// ),
43 : /// ],
44 : /// stackedRoutes: [
45 : /// VWidget(
46 : /// path: 'settings',
47 : /// widget: SettingsWidget(),
48 : /// ),
49 : /// ],
50 : /// )
51 : /// ```
52 : ///
53 : /// Also see:
54 : /// * [VNester] for a [VRouteElement] similar to [VNesterBase] but which take path information
55 : /// {@end-tool}
56 : class VNesterBase extends VRouteElementBuilder {
57 : /// A list of [VRouteElement] which widget will be accessible in [widgetBuilder]
58 : final List<VRouteElement> nestedRoutes;
59 :
60 : /// A list of routes which:
61 : /// - path NOT starting with '/' will be relative to [path]
62 : /// - widget or page will be stacked on top of [_rootVRouter]
63 : final List<VRouteElement> stackedRoutes;
64 :
65 : /// A function which creates the [VRouteElement._rootVRouter] associated to this [VRouteElement]
66 : ///
67 : /// [child] will be the [VRouteElement._rootVRouter] of the matched [VRouteElement] in
68 : /// [nestedRoutes]
69 : final Widget Function(Widget child) widgetBuilder;
70 :
71 : /// A LocalKey that will be given to the page which contains the given [widget]
72 : ///
73 : /// This key mostly controls the page animation. If a page remains the same but the key is changes,
74 : /// the page gets animated
75 : /// The key is by default the value of the current [path] (or [aliases]) with
76 : /// the path parameters replaced
77 : ///
78 : /// Do provide a constant [key] if you don't want this page to animate even if [path] or
79 : /// [aliases] path parameters change
80 : final LocalKey? key;
81 :
82 : /// A name for the route which will allow you to easily navigate to it
83 : /// using [VRouter.of(context).pushNamed]
84 : ///
85 : /// Note that [name] should be unique w.r.t every [VRouteElement]
86 : final String? name;
87 :
88 : /// The duration of [VWidgetBase.buildTransition]
89 : final Duration? transitionDuration;
90 :
91 : /// The reverse duration of [VWidgetBase.buildTransition]
92 : final Duration? reverseTransitionDuration;
93 :
94 : /// Create a custom transition effect when coming to and
95 : /// going to this route
96 : /// This has the priority over [VRouter.buildTransition]
97 : ///
98 : /// Also see:
99 : /// * [VRouter.buildTransition] for default transitions for all routes
100 : final Widget Function(Animation<double> animation,
101 : Animation<double> secondaryAnimation, Widget child)? buildTransition;
102 :
103 3 : VNesterBase({
104 : required this.widgetBuilder,
105 : required this.nestedRoutes,
106 : this.transitionDuration,
107 : this.reverseTransitionDuration,
108 : this.buildTransition,
109 : this.key,
110 : this.name,
111 : this.stackedRoutes = const [],
112 : });
113 :
114 3 : @override
115 3 : List<VRouteElement> buildRoutes() => [
116 3 : VNesterPageBase(
117 3 : key: key,
118 3 : name: name,
119 3 : nestedRoutes: nestedRoutes,
120 3 : stackedRoutes: stackedRoutes,
121 3 : widgetBuilder: widgetBuilder,
122 2 : pageBuilder: (LocalKey key, Widget child, String? name) =>
123 2 : VBasePage.fromPlatform(
124 : key: key,
125 : child: child,
126 : name: name,
127 2 : buildTransition: buildTransition,
128 2 : transitionDuration: transitionDuration,
129 2 : reverseTransitionDuration: reverseTransitionDuration,
130 : ),
131 : ),
132 : ];
133 : }
|