cupertino_bottom_sheet.dart
12.8 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter/cupertino.dart' show CupertinoTheme, CupertinoApp;
import 'package:flutter/material.dart'
show
Colors,
MaterialLocalizations,
Theme,
debugCheckHasMaterialLocalizations;
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import '../../modal_bottom_sheet.dart';
const double _kPreviousPageVisibleOffset = 10;
const Radius _kDefaultTopRadius = Radius.circular(12);
const BoxShadow _kDefaultBoxShadow =
BoxShadow(blurRadius: 10, color: Colors.black12, spreadRadius: 5);
/// Cupertino Bottom Sheet Container
///
/// Clip the child widget to rectangle with top rounded corners and adds
/// top padding(+safe area padding). This padding [_kPreviousPageVisibleOffset]
/// is the height that will be displayed from previous route.
class _CupertinoBottomSheetContainer extends StatelessWidget {
final Widget child;
final Color? backgroundColor;
final Radius topRadius;
final BoxShadow? shadow;
const _CupertinoBottomSheetContainer({
Key? key,
required this.child,
this.backgroundColor,
required this.topRadius,
this.shadow,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final topSafeAreaPadding = MediaQuery.of(context).padding.top;
final topPadding = _kPreviousPageVisibleOffset + topSafeAreaPadding;
final shadow = this.shadow ?? _kDefaultBoxShadow;
BoxShadow(blurRadius: 10, color: Colors.black12, spreadRadius: 5);
final backgroundColor = this.backgroundColor ??
CupertinoTheme.of(context).scaffoldBackgroundColor;
return Padding(
padding: EdgeInsets.only(top: topPadding),
child: ClipRRect(
borderRadius: BorderRadius.vertical(top: topRadius),
child: Container(
decoration:
BoxDecoration(color: backgroundColor, boxShadow: [shadow]),
width: double.infinity,
child: MediaQuery.removePadding(
context: context,
removeTop: true, //Remove top Safe Area
child: child,
),
),
),
);
}
}
Future<T?> showCupertinoModalBottomSheet<T>({
required BuildContext context,
required WidgetBuilder builder,
Color? backgroundColor,
double? elevation,
double? closeProgressThreshold,
ShapeBorder? shape,
Clip? clipBehavior,
Color? barrierColor,
bool expand = false,
AnimationController? secondAnimation,
Curve? animationCurve,
Curve? previousRouteAnimationCurve,
bool useRootNavigator = false,
bool bounce = true,
bool? isDismissible,
bool enableDrag = true,
Radius topRadius = _kDefaultTopRadius,
Duration? duration,
RouteSettings? settings,
Color? transitionBackgroundColor,
BoxShadow? shadow,
SystemUiOverlayStyle? overlayStyle,
}) async {
assert(debugCheckHasMediaQuery(context));
final hasMaterialLocalizations =
Localizations.of<MaterialLocalizations>(context, MaterialLocalizations) !=
null;
final barrierLabel = hasMaterialLocalizations
? MaterialLocalizations.of(context).modalBarrierDismissLabel
: '';
final result =
await Navigator.of(context, rootNavigator: useRootNavigator).push(
CupertinoModalBottomSheetRoute<T>(
builder: builder,
containerBuilder: (context, _, child) => _CupertinoBottomSheetContainer(
child: child,
backgroundColor: backgroundColor,
topRadius: topRadius,
shadow: shadow,
),
secondAnimationController: secondAnimation,
expanded: expand,
closeProgressThreshold: closeProgressThreshold,
barrierLabel: barrierLabel,
elevation: elevation,
bounce: bounce,
shape: shape,
clipBehavior: clipBehavior,
isDismissible: isDismissible ?? expand == false ? true : false,
modalBarrierColor: barrierColor ?? Colors.black12,
enableDrag: enableDrag,
topRadius: topRadius,
animationCurve: animationCurve,
previousRouteAnimationCurve: previousRouteAnimationCurve,
duration: duration,
settings: settings,
transitionBackgroundColor: transitionBackgroundColor ?? Colors.black,
overlayStyle: overlayStyle),
);
return result;
}
class CupertinoModalBottomSheetRoute<T> extends ModalBottomSheetRoute<T> {
final Radius topRadius;
final Curve? previousRouteAnimationCurve;
final BoxShadow? boxShadow;
// Background color behind all routes
// Black by default
final Color? transitionBackgroundColor;
final SystemUiOverlayStyle? overlayStyle;
CupertinoModalBottomSheetRoute({
required WidgetBuilder builder,
WidgetWithChildBuilder? containerBuilder,
double? closeProgressThreshold,
String? barrierLabel,
double? elevation,
ShapeBorder? shape,
Clip? clipBehavior,
AnimationController? secondAnimationController,
Curve? animationCurve,
Color? modalBarrierColor,
bool bounce = true,
bool isDismissible = true,
bool enableDrag = true,
required bool expanded,
Duration? duration,
RouteSettings? settings,
ScrollController? scrollController,
this.boxShadow = _kDefaultBoxShadow,
this.transitionBackgroundColor,
this.topRadius = _kDefaultTopRadius,
this.previousRouteAnimationCurve,
this.overlayStyle,
}) : super(
closeProgressThreshold: closeProgressThreshold,
scrollController: scrollController,
containerBuilder: containerBuilder,
builder: builder,
bounce: bounce,
barrierLabel: barrierLabel,
secondAnimationController: secondAnimationController,
modalBarrierColor: modalBarrierColor,
isDismissible: isDismissible,
enableDrag: enableDrag,
expanded: expanded,
settings: settings,
animationCurve: animationCurve,
duration: duration,
);
@override
Widget buildTransitions(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) {
final paddingTop = MediaQuery.of(context).padding.top;
final distanceWithScale = (paddingTop + _kPreviousPageVisibleOffset) * 0.9;
final offsetY = secondaryAnimation.value * (paddingTop - distanceWithScale);
final scale = 1 - secondaryAnimation.value / 10;
return AnimatedBuilder(
builder: (context, child) => Transform.translate(
offset: Offset(0, offsetY),
child: Transform.scale(
scale: scale,
child: child,
alignment: Alignment.topCenter,
),
),
child: child,
animation: secondaryAnimation,
);
}
@override
Widget getPreviousRouteTransition(
BuildContext context, Animation<double> secondAnimation, Widget child) {
return _CupertinoModalTransition(
secondaryAnimation: secondAnimation,
body: child,
animationCurve: previousRouteAnimationCurve,
topRadius: topRadius,
backgroundColor: transitionBackgroundColor ?? Colors.black,
overlayStyle: overlayStyle,
);
}
}
class _CupertinoModalTransition extends StatelessWidget {
final Animation<double> secondaryAnimation;
final Radius topRadius;
final Curve? animationCurve;
final Color backgroundColor;
final SystemUiOverlayStyle? overlayStyle;
final Widget body;
const _CupertinoModalTransition({
Key? key,
required this.secondaryAnimation,
required this.body,
required this.topRadius,
this.backgroundColor = Colors.black,
this.animationCurve,
this.overlayStyle,
}) : super(key: key);
@override
Widget build(BuildContext context) {
var startRoundCorner = 0.0;
final paddingTop = MediaQuery.of(context).padding.top;
if (Theme.of(context).platform == TargetPlatform.iOS && paddingTop > 20) {
startRoundCorner = 38.5;
//https://kylebashour.com/posts/finding-the-real-iphone-x-corner-radius
}
final curvedAnimation = CurvedAnimation(
parent: secondaryAnimation,
curve: animationCurve ?? Curves.easeOut,
);
return AnnotatedRegion<SystemUiOverlayStyle>(
value: overlayStyle ?? SystemUiOverlayStyle.light,
child: AnimatedBuilder(
animation: curvedAnimation,
child: body,
builder: (context, child) {
final progress = curvedAnimation.value;
final yOffset = progress * paddingTop;
final scale = 1 - progress / 10;
final radius = progress == 0
? 0.0
: (1 - progress) * startRoundCorner + progress * topRadius.x;
return Stack(
children: <Widget>[
Container(color: backgroundColor),
Transform.translate(
offset: Offset(0, yOffset),
child: Transform.scale(
scale: scale,
alignment: Alignment.topCenter,
child: ClipRRect(
borderRadius: BorderRadius.circular(radius),
child: child),
),
),
],
);
},
),
);
}
}
class CupertinoScaffoldInheirted extends InheritedWidget {
final AnimationController? animation;
final Radius? topRadius;
const CupertinoScaffoldInheirted({
this.animation,
required super.child,
this.topRadius,
}) : super();
@override
bool updateShouldNotify(InheritedWidget oldWidget) {
return false;
}
}
// Support
class CupertinoScaffold extends StatefulWidget {
static CupertinoScaffoldInheirted? of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<CupertinoScaffoldInheirted>();
final Widget body;
final Radius topRadius;
final Color transitionBackgroundColor;
final SystemUiOverlayStyle? overlayStyle;
const CupertinoScaffold({
Key? key,
required this.body,
this.topRadius = _kDefaultTopRadius,
this.transitionBackgroundColor = Colors.black,
this.overlayStyle,
}) : super(key: key);
@override
State<StatefulWidget> createState() => _CupertinoScaffoldState();
static Future<T?> showCupertinoModalBottomSheet<T>({
required BuildContext context,
double? closeProgressThreshold,
required WidgetBuilder builder,
Curve? animationCurve,
Curve? previousRouteAnimationCurve,
Color? backgroundColor,
Color? barrierColor,
bool expand = false,
bool useRootNavigator = false,
bool bounce = true,
bool? isDismissible,
bool enableDrag = true,
Duration? duration,
RouteSettings? settings,
BoxShadow? shadow,
SystemUiOverlayStyle? overlayStyle,
}) async {
assert(debugCheckHasMediaQuery(context));
final isCupertinoApp =
context.findAncestorWidgetOfExactType<CupertinoApp>() != null;
var barrierLabel = '';
if (!isCupertinoApp) {
assert(debugCheckHasMaterialLocalizations(context));
barrierLabel = MaterialLocalizations.of(context).modalBarrierDismissLabel;
}
final topRadius = CupertinoScaffold.of(context)!.topRadius;
final result = await Navigator.of(context, rootNavigator: useRootNavigator)
.push(CupertinoModalBottomSheetRoute<T>(
closeProgressThreshold: closeProgressThreshold,
builder: builder,
secondAnimationController: CupertinoScaffold.of(context)!.animation,
containerBuilder: (context, _, child) => _CupertinoBottomSheetContainer(
child: child,
backgroundColor: backgroundColor,
topRadius: topRadius ?? _kDefaultTopRadius,
shadow: shadow,
),
expanded: expand,
barrierLabel: barrierLabel,
bounce: bounce,
isDismissible: isDismissible ?? expand == false ? true : false,
modalBarrierColor: barrierColor ?? Colors.black12,
enableDrag: enableDrag,
topRadius: topRadius ?? _kDefaultTopRadius,
animationCurve: animationCurve,
previousRouteAnimationCurve: previousRouteAnimationCurve,
duration: duration,
settings: settings,
overlayStyle: overlayStyle,
));
return result;
}
}
class _CupertinoScaffoldState extends State<CupertinoScaffold>
with TickerProviderStateMixin {
late AnimationController animationController;
@override
void initState() {
animationController =
AnimationController(duration: Duration(milliseconds: 350), vsync: this);
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return CupertinoScaffoldInheirted(
animation: animationController,
topRadius: widget.topRadius,
child: _CupertinoModalTransition(
secondaryAnimation: animationController,
body: widget.body,
topRadius: widget.topRadius,
backgroundColor: widget.transitionBackgroundColor,
overlayStyle: widget.overlayStyle,
),
);
}
}