Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 5dc07a0

Browse files
committed
implement tab view
1 parent a7d9836 commit 5dc07a0

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

Runtime/cupertino/tab_view.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System.Collections.Generic;
2+
using Unity.UIWidgets.foundation;
3+
using Unity.UIWidgets.widgets;
4+
5+
namespace Unity.UIWidgets.cupertino {
6+
public class CupertinoTabView : StatefulWidget {
7+
public CupertinoTabView(
8+
Key key = null,
9+
WidgetBuilder builder = null,
10+
GlobalKey<NavigatorState> navigatorKey = null,
11+
string defaultTitle = null,
12+
Dictionary<string, WidgetBuilder> routes = null,
13+
RouteFactory onGenerateRoute = null,
14+
RouteFactory onUnknownRoute = null,
15+
List<NavigatorObserver> navigatorObservers = null
16+
) : base(key: key) {
17+
this.builder = builder;
18+
this.navigatorKey = navigatorKey;
19+
this.defaultTitle = defaultTitle;
20+
this.routes = routes;
21+
this.onGenerateRoute = onGenerateRoute;
22+
this.onUnknownRoute = onUnknownRoute;
23+
this.navigatorObservers = navigatorObservers ?? new List<NavigatorObserver>();
24+
}
25+
26+
public readonly WidgetBuilder builder;
27+
28+
public readonly GlobalKey<NavigatorState> navigatorKey;
29+
30+
public readonly string defaultTitle;
31+
32+
public readonly Dictionary<string, WidgetBuilder> routes;
33+
34+
public readonly RouteFactory onGenerateRoute;
35+
36+
public readonly RouteFactory onUnknownRoute;
37+
38+
public readonly List<NavigatorObserver> navigatorObservers;
39+
40+
public override State createState() {
41+
return new _CupertinoTabViewState();
42+
}
43+
}
44+
45+
class _CupertinoTabViewState : State<CupertinoTabView> {
46+
HeroController _heroController;
47+
List<NavigatorObserver> _navigatorObservers;
48+
49+
public override void initState() {
50+
base.initState();
51+
this._heroController = CupertinoApp.createCupertinoHeroController();
52+
this._updateObservers();
53+
}
54+
55+
public override void didUpdateWidget(StatefulWidget oldWidget) {
56+
base.didUpdateWidget(oldWidget);
57+
CupertinoTabView _oldWidget = (CupertinoTabView) oldWidget;
58+
if (this.widget.navigatorKey != _oldWidget.navigatorKey
59+
|| this.widget.navigatorObservers != _oldWidget.navigatorObservers) {
60+
this._updateObservers();
61+
}
62+
}
63+
64+
void _updateObservers() {
65+
this._navigatorObservers =
66+
new List<NavigatorObserver>(this.widget.navigatorObservers);
67+
this._navigatorObservers.Add(this._heroController);
68+
}
69+
70+
public override Widget build(BuildContext context) {
71+
return new Navigator(
72+
key: this.widget.navigatorKey,
73+
onGenerateRoute: this._onGenerateRoute,
74+
onUnknownRoute: this._onUnknownRoute,
75+
observers: this._navigatorObservers
76+
);
77+
}
78+
79+
Route _onGenerateRoute(RouteSettings settings) {
80+
string name = settings.name;
81+
WidgetBuilder routeBuilder = null;
82+
string title = null;
83+
if (name == Navigator.defaultRouteName && this.widget.builder != null) {
84+
routeBuilder = this.widget.builder;
85+
title = this.widget.defaultTitle;
86+
}
87+
else if (this.widget.routes != null) {
88+
routeBuilder = this.widget.routes[name];
89+
}
90+
91+
if (routeBuilder != null) {
92+
return new CupertinoPageRoute(
93+
builder: routeBuilder,
94+
title: title,
95+
settings: settings
96+
);
97+
}
98+
99+
if (this.widget.onGenerateRoute != null) {
100+
return this.widget.onGenerateRoute(settings);
101+
}
102+
103+
return null;
104+
}
105+
106+
Route _onUnknownRoute(RouteSettings settings) {
107+
D.assert(() => {
108+
if (this.widget.onUnknownRoute == null) {
109+
throw new UIWidgetsError(
110+
$"Could not find a generator for route {settings} in the {this.GetType()}.\n" +
111+
"Generators for routes are searched for in the following order:\n" +
112+
" 1. For the \"/\" route, the \"builder\" property, if non-null, is used.\n" +
113+
" 2. Otherwise, the \"routes\" table is used, if it has an entry for " +
114+
"the route.\n" +
115+
" 3. Otherwise, onGenerateRoute is called. It should return a " +
116+
"non-null value for any valid route not handled by \"builder\" and \"routes\".\n" +
117+
" 4. Finally if all else fails onUnknownRoute is called.\n" +
118+
"Unfortunately, onUnknownRoute was not set."
119+
);
120+
}
121+
122+
return true;
123+
});
124+
125+
Route result = this.widget.onUnknownRoute(settings);
126+
D.assert(() => {
127+
if (result == null) {
128+
throw new UIWidgetsError(
129+
"The onUnknownRoute callback returned null.\n" +
130+
$"When the {this.GetType()} requested the route {settings} from its " +
131+
"onUnknownRoute callback, the callback returned null. Such callbacks " +
132+
"must never return null."
133+
);
134+
}
135+
136+
return true;
137+
});
138+
return result;
139+
}
140+
}
141+
}

Runtime/cupertino/tab_view.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)