-
Notifications
You must be signed in to change notification settings - Fork 10
/
Navigator.cs
65 lines (49 loc) · 2.28 KB
/
Navigator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
namespace UniMob.UI.Widgets
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class Navigator : StatefulWidget
{
public string InitialRoute { get; }
public Dictionary<string, Func<Route>> Routes { get; }
public Navigator(
string initialRoute,
Dictionary<string, Func<Route>> routes
)
{
InitialRoute = initialRoute;
Routes = routes;
}
public override State CreateState() => new NavigatorState();
public static NavigatorState Of(
BuildContext context,
bool rootNavigator = false,
bool nullOk = false
)
{
var navigator = rootNavigator
? context.RootAncestorStateOfType<NavigatorState>()
: context.AncestorStateOfType<NavigatorState>();
if (!nullOk && navigator == null)
{
throw new Exception(
"Navigator operation requested with a context that does not include a Navigator.\n" +
"The context used to push or pop routes from the Navigator must be that of a " +
"widget that is a descendant of a Navigator widget."
);
}
return navigator;
}
public static Route Push(BuildContext context, Route route) => Of(context).Push(route);
public static Task<TResult> Push<TResult>(BuildContext context, Route route) =>
Of(context).Push<TResult>(route);
public static Route PushNamed(BuildContext context, string routeName) => Of(context).PushNamed(routeName);
public static void Pop(BuildContext context) => Of(context).Pop();
public static Route NewRoot(BuildContext context, Route route) => Of(context).NewRoot(route);
public static Route NewRootNamed(BuildContext context, string routeName) => Of(context).NewRootNamed(routeName);
public static Route Replace(BuildContext context, Route route) => Of(context).Replace(route);
public static Route ReplaceNamed(BuildContext context, string routeName) => Of(context).ReplaceNamed(routeName);
public static void PopTo(BuildContext context, Route route) => Of(context).PopTo(route);
}
}