-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppWidget.cs
More file actions
151 lines (135 loc) · 5.19 KB
/
Copy pathAppWidget.cs
File metadata and controls
151 lines (135 loc) · 5.19 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using UniMob.UI;
using UniMob.UI.Widgets;
using UnityEngine;
namespace Samples.BackButtonSupport
{
public class AppWidget : StatefulWidget
{
public BackButtonController BackButtonController { get; }
public AppWidget(BackButtonController backButtonController)
{
BackButtonController = backButtonController;
}
public override State CreateState() => new AppWidgetState();
}
public class AppWidgetState : HocState<AppWidget>
{
private readonly GlobalKey<NavigatorState> appNavigatorKey = new GlobalKey<NavigatorState>();
private NavigatorState AppNavigator => appNavigatorKey.CurrentState;
public override void InitState()
{
base.InitState();
Widget.BackButtonController.RegisterHandler(StateLifetime, () => HandleBack());
}
public override Widget Build(BuildContext context)
{
var routes = new Dictionary<string, Func<Route>> {["main"] = BuildMainRoute};
return new Navigator("main", routes) {Key = appNavigatorKey,};
}
private bool HandleBack()
{
var handled = AppNavigator.HandleBack();
if (!handled)
{
ShowQuitGameDialog();
}
return true;
}
private async void ShowQuitGameDialog()
{
var quit = await AppNavigator.Push<bool>(BuildQuitRoute());
if (quit)
{
Application.Quit();
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
}
}
private Route BuildMainRoute()
{
return new PageRouteBuilder(
new RouteSettings("main", RouteModalType.Fullscreen),
(context, controller, secondaryAnimation) => new Container
{
Size = WidgetSize.Stretched,
BackgroundColor = Color.white,
Child = new Column
{
Children =
{
new UniMobButton
{
OnClick = () => AppNavigator.Push(BuildDetailRoute()),
Child = DemoUtils.Text(Color.yellow, "Open Details Page >"),
},
new UniMobButton
{
OnClick = () => AppNavigator.Push(BuildAdvancedControlRoute()),
Child = DemoUtils.Text(Color.yellow, "Open Complex Flow Page >"),
},
},
},
}
);
}
private Route BuildDetailRoute()
{
return new PageRouteBuilder(
new RouteSettings("detail", RouteModalType.Fullscreen),
(context, controller, secondaryAnimation) => new Container
{
Size = WidgetSize.Stretched,
BackgroundColor = Color.gray,
Child = DemoUtils.Text(Color.white, "DETAIL PAGE", "Press ESC to return"),
}
).WithPopOnBack(AppNavigator); // Basic: Automatically call 'Navigator.Pop()' on back button click
}
private Route BuildAdvancedControlRoute()
{
// Advanced: Pass back button control to widget
return BackButtonController.Create(bbc => new PageRouteBuilder(
new RouteSettings("advanced", RouteModalType.Fullscreen),
(context, animation, secondaryAnimation) => new CompositeTransition
{
Opacity = animation,
Child = new AdvancedControlWidget(bbc)
{
OnClose = () => AppNavigator.Pop(),
},
},
transitionDuration: 0.2f,
reverseTransitionDuration: 0.2f
));
}
private Route BuildQuitRoute()
{
return new PageRouteBuilder(
new RouteSettings("quit", RouteModalType.Fullscreen),
(context, controller, secondaryAnimation) => new Container
{
Size = WidgetSize.Stretched,
BackgroundColor = Color.gray,
Child = new Column
{
CrossAxisAlignment = CrossAxisAlignment.Center,
Children =
{
DemoUtils.Text(Color.white,
"QUIT POPUP",
"Do you really want to quit app?",
"Press ESC to return"),
new UniMobButton
{
Child = DemoUtils.Text(Color.red, "QUIT"),
OnClick = () => AppNavigator.Pop(true),
},
},
},
}
).WithPopOnBack(AppNavigator, false);
}
}
}