forked from UnityTech/UIWidgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavigationSample.cs
More file actions
186 lines (167 loc) · 7.22 KB
/
NavigationSample.cs
File metadata and controls
186 lines (167 loc) · 7.22 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using Unity.UIWidgets.animation;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.material;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
using DialogUtils = Unity.UIWidgets.widgets.DialogUtils;
using TextStyle = Unity.UIWidgets.painting.TextStyle;
namespace UIWidgetsSample {
public class NavigationSample : UIWidgetsSamplePanel {
protected override Widget createWidget() {
return new WidgetsApp(
initialRoute: "/",
textStyle: new TextStyle(fontSize: 24),
pageRouteBuilder: this.pageRouteBuilder,
routes: new Dictionary<string, WidgetBuilder> {
{"/", (context) => new HomeScreen()},
{"/detail", (context) => new DetailScreen()}
});
}
protected override PageRouteFactory pageRouteBuilder {
get {
return (RouteSettings settings, WidgetBuilder builder) =>
new PageRouteBuilder(
settings: settings,
pageBuilder: (BuildContext context, Animation<float> animation,
Animation<float> secondaryAnimation) => builder(context),
transitionsBuilder: (BuildContext context, Animation<float>
animation, Animation<float> secondaryAnimation, Widget child) =>
new _FadeUpwardsPageTransition(
routeAnimation: animation,
child: child
)
);
}
}
}
class HomeScreen : StatelessWidget {
public override Widget build(BuildContext context) {
return new NavigationPage(
body: new Container(
color: new Color(0xFF888888),
child: new Center(
child: new CustomButton(onPressed: () => { Navigator.pushNamed(context, "/detail"); },
child: new Text("Go to Detail"))
)),
title: "Home"
);
}
}
class DetailScreen : StatelessWidget {
public override Widget build(BuildContext context) {
return new NavigationPage(
body: new Container(
color: new Color(0xFF1389FD),
child: new Center(
child: new Column(
children: new List<Widget>() {
new CustomButton(onPressed: () => { Navigator.pop(context); }, child: new Text("Back")),
new CustomButton(
onPressed: () => {
_Dialog.showDialog(context, builder: (BuildContext c) => new Dialog());
}, child: new Text("Show Dialog"))
}
)
)),
title: "Detail");
}
}
class Dialog : StatelessWidget {
public override Widget build(BuildContext context) {
return new Center(child: new Container(
color: new Color(0xFFFF0000),
width: 100,
height: 80,
child: new Center(
child: new Text("Hello Dialog")
)));
}
}
class _FadeUpwardsPageTransition : StatelessWidget {
internal _FadeUpwardsPageTransition(
Key key = null,
Animation<float> routeAnimation = null, // The route's linear 0.0 - 1.0 animation.
Widget child = null
) : base(key: key) {
this._positionAnimation = _bottomUpTween.chain(_fastOutSlowInTween).animate(routeAnimation);
this._opacityAnimation = _easeInTween.animate(routeAnimation);
this.child = child;
}
static Tween<Offset> _bottomUpTween = new OffsetTween(
begin: new Offset(0.0f, 0.25f),
end: Offset.zero
);
static Animatable<float> _fastOutSlowInTween = new CurveTween(curve: Curves.fastOutSlowIn);
static Animatable<float> _easeInTween = new CurveTween(curve: Curves.easeIn);
readonly Animation<Offset> _positionAnimation;
readonly Animation<float> _opacityAnimation;
public readonly Widget child;
public override Widget build(BuildContext context) {
return new SlideTransition(
position: this._positionAnimation,
child: new FadeTransition(
opacity: this._opacityAnimation,
child: this.child
)
);
}
}
class NavigationPage : StatelessWidget {
public readonly Widget body;
public readonly string title;
public NavigationPage(Widget body = null, string title = null) {
this.title = title;
this.body = body;
}
public override Widget build(BuildContext context) {
Widget back = null;
if (Navigator.of(context).canPop()) {
back = new CustomButton(onPressed: () => { Navigator.pop(context); },
child: new Text("Go Back"));
back = new Column(mainAxisAlignment: MainAxisAlignment.center, children: new List<Widget>() {back});
}
return new Container(
child: new Column(
children: new List<Widget>() {
new ConstrainedBox(constraints: new BoxConstraints(maxHeight: 80),
child: new DecoratedBox(
decoration: new BoxDecoration(color: new Color(0XFFE1ECF4)),
child: new NavigationToolbar(leading: back,
middle: new Text(this.title, textAlign: TextAlign.center)))),
new Flexible(child: this.body)
}
)
);
}
}
static class _Dialog {
public static void showDialog(BuildContext context,
bool barrierDismissible = true, WidgetBuilder builder = null) {
DialogUtils.showGeneralDialog(
context: context,
pageBuilder: (BuildContext buildContext, Animation<float> animation,
Animation<float> secondaryAnimation) => {
return builder(buildContext);
},
barrierDismissible: barrierDismissible,
barrierColor: new Color(0x8A000000),
transitionDuration: TimeSpan.FromMilliseconds(150),
transitionBuilder: _buildMaterialDialogTransitions
);
}
static Widget _buildMaterialDialogTransitions(BuildContext context,
Animation<float> animation, Animation<float> secondaryAnimation, Widget child) {
return new FadeTransition(
opacity: new CurvedAnimation(
parent: animation,
curve: Curves.easeOut
),
child: child
);
}
}
}