-
Notifications
You must be signed in to change notification settings - Fork 1
/
navigation_bar.dart
328 lines (294 loc) · 10.9 KB
/
navigation_bar.dart
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:platty/src/widgets/material_patcher.dart';
import 'package:platty/src/widgets/platform.dart';
// copied from iOS source.
const double _kNavBarPersistentHeight = 44.0;
const Color _kDefaultNavBarBackgroundColor = Color(0xCCF8F8F8);
const double _defaultElevationAndroid = 4.0;
const bool _defaultCenterTitleAndroid = false;
abstract class PNavigationBarBase extends PlatformAdaptingWidget {
/// Leave null for default behavior on each platform.
/// See [CupertinoNavigationBar.leading]
/// See [AppBar.leading]
final Widget leading;
/// The list of actions to apply here. The [CupertinoNavigationBar]
/// only takes the first widget supplied here.
/// See [AppBar.actions]
/// See [CupertinoNavigationBar.trailing]
final List<Widget> actions;
/// On Android this is left-aligned.
/// On iOS this is center-aligned.
/// See [CupertinoNavigationBar.middle]
/// See [AppBar.title]
final Widget title;
/// Android only-widget that appears at the bottom of the [AppBar].
/// A function since it may not be rendered.
/// See [AppBar.bottom]
final PreferredSizeWidget androidBottom;
/// See [AppBar.elevation]
final double androidElevation;
/// See [AppBar.flexibleSpace]
final Widget androidFlexibleSpace;
/// See [AppBar.centerTitle]
final bool androidCenterTitle;
/// See [AppBar.titleSpacing]
final double androidTitleSpacing;
final Color backgroundColor;
/// See [CupertinoNavigationBar.previousPageTitle]
final String iosPreviousPageTitle;
/// See [CupertinoNavigationBar.padding]
final EdgeInsetsDirectional iosPadding;
final Color iconColor;
final Object iosHeroTag;
/// if true, iOS will make background color and icon tint similar to android.
final bool iosMirrorAndroid;
final TextTheme textTheme;
PNavigationBarBase(
{Key key,
@required this.leading,
@required this.actions,
@required this.title,
@required PreferredSizeWidget Function() androidBottom,
@required this.backgroundColor,
@required this.iosPreviousPageTitle,
@required this.iosPadding,
@required this.androidElevation,
@required this.androidFlexibleSpace,
@required this.androidCenterTitle,
@required this.androidTitleSpacing,
@required this.iconColor,
@required this.iosHeroTag,
this.textTheme,
this.iosMirrorAndroid = true,
TargetPlatform renderPlatform})
: this.androidBottom = androidBottom != null ? androidBottom() : null,
super(key: key, renderPlatform: renderPlatform);
getPrimaryIOSAction() =>
actions != null && actions.length >= 1 ? actions?.first : null;
IconThemeData applyIconColor(ThemeData theme) {
return iconColor != null
? theme.iconTheme.copyWith(color: iconColor)
: null;
}
Color get iosBackgroundColor =>
(iosMirrorAndroid ? backgroundColor : null) ??
_kDefaultNavBarBackgroundColor;
Color getIosIconColor(BuildContext context) =>
this.iconColor ??
// patch cupertino theme insistence on using color from icon theme
(iosMirrorAndroid
? Theme.of(context)?.iconTheme?.color
: CupertinoTheme.of(context)?.primaryColor) ??
IconTheme.of(context)?.color ??
CupertinoColors.activeBlue;
Widget getIOSTitle(BuildContext context) {
if (iosMirrorAndroid) {
final AppBarTheme appBarTheme = AppBarTheme.of(context);
final ThemeData theme = Theme.of(context);
TextStyle centerStyle = textTheme?.headline6 ??
appBarTheme.textTheme?.headline6 ??
theme.primaryTextTheme.headline6;
return DefaultTextStyle(style: centerStyle, child: title);
} else {
return title;
}
}
}
/// A widget that attempts to consolidate the different behaviors of each platform into
/// one single, [PlatformAdaptingWidget].
class PNavigationBar extends PNavigationBarBase
implements ObstructingPreferredSizeWidget {
PNavigationBar(
{Key key,
Widget leading,
List<Widget> actions,
Widget title,
PreferredSizeWidget Function() androidBottom,
Color backgroundColor,
String iosPreviousPageTitle,
EdgeInsetsDirectional iosPadding,
double androidElevation = _defaultElevationAndroid,
Widget androidFlexibleSpace,
bool androidCenterTitle = _defaultCenterTitleAndroid,
double androidTitleSpacing = NavigationToolbar.kMiddleSpacing,
Color iconColor,
Object iosHeroTag,
bool iosMirrorAndroid = true,
TextTheme textTheme,
TargetPlatform renderPlatform})
: super(
key: key,
renderPlatform: renderPlatform,
title: title,
actions: actions,
androidBottom: androidBottom,
backgroundColor: backgroundColor,
iosPadding: iosPadding,
androidElevation: androidElevation,
androidFlexibleSpace: androidFlexibleSpace,
iconColor: iconColor,
iosHeroTag: iosHeroTag,
androidTitleSpacing: androidTitleSpacing,
androidCenterTitle: androidCenterTitle,
iosPreviousPageTitle: iosPreviousPageTitle,
leading: leading,
iosMirrorAndroid: iosMirrorAndroid,
textTheme: textTheme,
);
@override
Size get preferredSize {
// TODO: better way of adapting platform here. We do not respect PTheme.
if (defaultTargetPlatform == TargetPlatform.android) {
return Size.fromHeight(
kToolbarHeight + (androidBottom?.preferredSize?.height ?? 0.0));
} else {
return Size.fromHeight(_kNavBarPersistentHeight);
}
}
@override
get renderMaterial => (BuildContext context) {
final theme = Theme.of(context);
return AppBar(
leading: leading,
title: title,
actions: actions,
backgroundColor: backgroundColor,
elevation: androidElevation,
flexibleSpace: androidFlexibleSpace,
centerTitle: androidCenterTitle,
titleSpacing: androidTitleSpacing,
iconTheme: applyIconColor(theme),
bottom: androidBottom,
textTheme: textTheme,
);
};
@override
get renderCupertino => (BuildContext context) {
return MaterialPatcher(
child: iosHeroTag != null
? CupertinoNavigationBar(
heroTag: iosHeroTag,
transitionBetweenRoutes: false,
leading: leading,
middle: getIOSTitle(context),
trailing: getPrimaryIOSAction(),
backgroundColor: iosBackgroundColor,
previousPageTitle: iosPreviousPageTitle,
padding: iosPadding,
)
: CupertinoNavigationBar(
leading: leading,
middle: getIOSTitle(context),
trailing: getPrimaryIOSAction(),
backgroundColor: iosBackgroundColor,
previousPageTitle: iosPreviousPageTitle,
padding: iosPadding,
));
};
/// True if the navigation bar's background color has no transparency.
/// See [CupertinoNavigationBar.shouldFullyObstruct]
@override
bool shouldFullyObstruct(BuildContext context) {
return iosBackgroundColor.alpha == 0xFF;
}
}
/// Sliver implementation of the [PNavigationBar].
class PSliverNavigationBar extends PNavigationBarBase {
/// See [SliverAppBar.floating]
final bool androidFloating;
/// See [SliverAppBar.pinned]
final bool androidPinned;
/// See [SliverAppBar.snap]
final bool androidSnap;
/// See [CupertinoSliverNavigationBar.largeTitle]
final Widget iosLargeTitle;
PSliverNavigationBar(
{Key key,
Widget leading,
List<Widget> actions,
Widget title,
PreferredSizeWidget Function() androidBottom,
Color backgroundColor,
this.androidFloating = false,
this.androidPinned = false,
this.androidSnap = false,
Widget androidFlexibleSpace,
EdgeInsetsDirectional iosPadding,
this.iosLargeTitle,
String iosPreviousPageTitle,
double androidElevation,
bool androidCenterTitle = _defaultCenterTitleAndroid,
double androidTitleSpacing = NavigationToolbar.kMiddleSpacing,
Object iosHeroTag,
Color iconColor,
TextTheme textTheme,
bool iosMirrorAndroid = true,
TargetPlatform renderPlatform})
: super(
key: key,
renderPlatform: renderPlatform,
leading: leading,
actions: actions,
title: title,
androidBottom: androidBottom,
androidFlexibleSpace: androidFlexibleSpace,
androidCenterTitle: androidCenterTitle,
androidElevation: androidElevation,
androidTitleSpacing: androidTitleSpacing,
backgroundColor: backgroundColor,
iconColor: iconColor,
iosHeroTag: iosHeroTag,
iosPadding: iosPadding,
iosPreviousPageTitle: iosPreviousPageTitle,
iosMirrorAndroid: iosMirrorAndroid,
textTheme: textTheme,
);
@override
get renderMaterial => (BuildContext context) {
final theme = Theme.of(context);
return SliverAppBar(
leading: leading,
title: title,
actions: actions,
bottom: androidBottom,
backgroundColor: backgroundColor,
pinned: androidPinned,
floating: androidFloating,
snap: androidSnap,
flexibleSpace: androidFlexibleSpace,
elevation: androidElevation,
centerTitle: androidCenterTitle,
titleSpacing: androidTitleSpacing,
iconTheme: applyIconColor(theme),
textTheme: textTheme,
);
};
@override
get renderCupertino => (BuildContext context) {
return iosHeroTag != null
? CupertinoSliverNavigationBar(
heroTag: iosHeroTag,
padding: iosPadding,
// no large title specified, utilize title as middle.,
middle: iosLargeTitle == null ? getIOSTitle(context) : null,
leading: leading,
trailing: getPrimaryIOSAction(),
backgroundColor: iosBackgroundColor,
largeTitle: iosLargeTitle,
previousPageTitle: iosPreviousPageTitle,
)
: CupertinoSliverNavigationBar(
padding: iosPadding,
// no large title specified, utilize title as middle.,
middle: iosLargeTitle == null ? getIOSTitle(context) : null,
leading: leading,
trailing: getPrimaryIOSAction(),
backgroundColor: iosBackgroundColor,
largeTitle: iosLargeTitle,
previousPageTitle: iosPreviousPageTitle,
);
};
}