sheet_route.dart
11.9 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
// 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:math' as math;
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
import 'package:sheet/route.dart';
import 'package:sheet/sheet.dart';
/// Value extracted from the official sketch iOS UI kit
/// It is the top offset that will be displayed from the bottom route
const double _kPreviousRouteVisibleOffset = 10.0;
/// Value extracted from the official sketch iOS UI kit
const Radius _kCupertinoSheetTopRadius = Radius.circular(10.0);
/// Estimated Round corners for iPhone X, XR, 11, 11 Pro
/// https://kylebashour.com/posts/finding-the-real-iphone-x-corner-radius
/// It used to animate the bottom route with a top radius that matches
/// the frame radius. If the device doesn't have round corners it will use
/// Radius.zero
const Radius _kRoundedDeviceRadius = Radius.circular(38.5);
/// Minimal distance from the top of the screen to the top of the previous route
/// It will be used ff the top safe area is less than this value.
/// In iPhones the top SafeArea is more or equal to this distance.
const double _kSheetMinimalOffset = 10;
/// Value extracted from the official sketch iOS UI kit for iPhone X, XR, 11, 11 Pro
/// The status bar height is bigger for devices with rounded corners, this is
/// used to detect if an iPhone has round corners or not
const double _kRoundedDeviceStatusBarHeight = 20;
const Curve _kCupertinoSheetCurve = Curves.easeOutExpo;
const Curve _kCupertinoTransitionCurve = Curves.linear;
/// Wraps the child into a cupertino modal sheet appearance. This is used to
/// create a [SheetRoute].
///
/// Clip the child widget to rectangle with top rounded corners and adds
/// top padding and top safe area.
class _CupertinoSheetDecorationBuilder extends StatelessWidget {
const _CupertinoSheetDecorationBuilder({
required this.child,
required this.topRadius,
this.backgroundColor,
});
/// The child contained by the modal sheet
final Widget child;
/// The color to paint behind the child
final Color? backgroundColor;
/// The top corners of this modal sheet are rounded by this Radius
final Radius topRadius;
@override
Widget build(BuildContext context) {
return CupertinoUserInterfaceLevel(
data: CupertinoUserInterfaceLevelData.elevated,
child: Builder(
builder: (BuildContext context) {
return Container(
clipBehavior: Clip.hardEdge,
decoration: BoxDecoration(
borderRadius: BorderRadius.vertical(top: topRadius),
color: backgroundColor ??
CupertinoColors.systemBackground.resolveFrom(context),
),
child: MediaQuery.removePadding(
context: context,
removeTop: true,
child: child,
),
);
},
),
);
}
}
/// A modal route that overlays a widget over the current route and animates
/// it from the bottom with a cupertino modal sheet appearance
///
/// Clip the child widget to rectangle with top rounded corners and adds
/// top padding and top safe area.
///
/// * [CupertinoSheetPage], which is the [Page] version of this class
class CupertinoSheetRoute<T> extends SheetRoute<T> {
CupertinoSheetRoute({
required WidgetBuilder builder,
super.stops,
double initialStop = 1,
super.settings,
Color? backgroundColor,
super.maintainState = true,
super.fit,
super.draggable = true,
}) : super(
builder: (BuildContext context) {
return _CupertinoSheetDecorationBuilder(
child: Builder(builder: builder),
backgroundColor: backgroundColor,
topRadius: _kCupertinoSheetTopRadius,
);
},
animationCurve: _kCupertinoSheetCurve,
initialExtent: initialStop,
);
final SheetController _sheetController = SheetController();
@override
SheetController createSheetController() {
return _sheetController;
}
@override
Color? get barrierColor => Colors.transparent;
@override
bool get barrierDismissible => true;
@override
Widget buildSheet(BuildContext context, Widget child) {
SheetPhysics? effectivePhysics = BouncingSheetPhysics(
parent: SnapSheetPhysics(
stops: stops ?? <double>[0, 1],
parent: physics,
));
if (!draggable) {
effectivePhysics = const NeverDraggableSheetPhysics();
}
final MediaQueryData mediaQuery = MediaQuery.of(context);
final double topMargin =
math.max(_kSheetMinimalOffset, mediaQuery.padding.top) +
_kPreviousRouteVisibleOffset;
return Sheet.raw(
initialExtent: initialExtent,
decorationBuilder: decorationBuilder,
fit: fit,
maxExtent: mediaQuery.size.height - topMargin,
physics: effectivePhysics,
controller: sheetController,
child: child,
);
}
@override
Widget buildTransitions(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) {
final double topPadding = MediaQuery.of(context).padding.top;
final double topOffset = math.max(_kSheetMinimalOffset, topPadding);
return AnimatedBuilder(
animation: secondaryAnimation,
child: CupertinoUserInterfaceLevel(
data: CupertinoUserInterfaceLevelData.elevated,
child: child,
),
builder: (BuildContext context, Widget? child) {
final double progress = secondaryAnimation.value;
final double scale = 1 - progress / 10;
final double distanceWithScale =
(topOffset + _kPreviousRouteVisibleOffset) * 0.9;
final Offset offset =
Offset(0, progress * (topOffset - distanceWithScale));
return Transform.translate(
offset: offset,
child: Transform.scale(
scale: scale,
child: child,
alignment: Alignment.topCenter,
),
);
},
);
}
@override
bool canDriveSecondaryTransitionForPreviousRoute(
Route<dynamic> previousRoute) {
return previousRoute is! CupertinoSheetRoute;
}
@override
Widget buildSecondaryTransitionForPreviousRoute(BuildContext context,
Animation<double> secondaryAnimation, Widget child) {
final Animation<double> delayAnimation = CurvedAnimation(
parent: _sheetController.animation,
curve: Interval(
initialExtent == 1 ? 0 : initialExtent,
1,
curve: Curves.linear,
),
);
final Animation<double> secondaryAnimation = CurvedAnimation(
parent: _sheetController.animation,
curve: Interval(0, initialExtent, curve: Curves.linear),
);
return CupertinoSheetBottomRouteTransition(
body: child,
sheetAnimation: delayAnimation,
secondaryAnimation: secondaryAnimation,
);
}
}
/// Animation for previous route when a [CupertinoSheetRoute] enters/exits
@internal
class CupertinoSheetBottomRouteTransition extends StatelessWidget {
const CupertinoSheetBottomRouteTransition({
super.key,
required this.sheetAnimation,
required this.secondaryAnimation,
required this.body,
});
final Widget body;
final Animation<double> sheetAnimation;
final Animation<double> secondaryAnimation;
// Currently iOS does not provide any way to detect the radius of the
// screen device. Right not we detect if the safe area has the size
// for the device that contain a notch as they are the ones right
// now that has corners with radius
Radius _getRadiusForDevice(MediaQueryData mediaQuery) {
final double topPadding = mediaQuery.padding.top;
// Round corners for iPhone devices from X to the newest version
final bool isRoundedDevice = defaultTargetPlatform == TargetPlatform.iOS &&
topPadding > _kRoundedDeviceStatusBarHeight;
return isRoundedDevice ? _kRoundedDeviceRadius : Radius.zero;
}
@override
Widget build(BuildContext context) {
final double topPadding = MediaQuery.of(context).padding.top;
final double topOffset = math.max(_kSheetMinimalOffset, topPadding);
final Radius deviceCorner = _getRadiusForDevice(MediaQuery.of(context));
final CurvedAnimation curvedAnimation = CurvedAnimation(
parent: sheetAnimation,
curve: _kCupertinoTransitionCurve,
);
return AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle.light,
child: AnimatedBuilder(
animation: secondaryAnimation,
child: body,
builder: (BuildContext context, Widget? child) {
final double progress = curvedAnimation.value;
final double scale = 1 - progress / 10;
final Radius radius = progress == 0
? Radius.zero
: Radius.lerp(deviceCorner, _kCupertinoSheetTopRadius, progress)!;
return Stack(
children: <Widget>[
Container(color: CupertinoColors.black),
// TODO(jaime): Add ColorFilter based on CupertinoUserInterfaceLevelData
// https://github.com/jamesblasco/modal_bottom_sheet/pull/44/files
Transform.translate(
offset: Offset(0, progress * topOffset),
child: Transform.scale(
scale: scale,
alignment: Alignment.topCenter,
child: ClipRRect(
borderRadius: BorderRadius.vertical(top: radius),
child: ColorFiltered(
colorFilter: ColorFilter.mode(
(CupertinoTheme.brightnessOf(context) == Brightness.dark
? CupertinoColors.inactiveGray
: Colors.black)
.withOpacity(secondaryAnimation.value * 0.1),
BlendMode.srcOver,
),
child: child,
),
),
),
),
],
);
},
),
);
}
}
/// A modal page that overlays a widget over the current route and animates
/// it from the bottom with a cupertino modal sheet appearance
///
/// Clip the child widget to rectangle with top rounded corners and adds
/// top padding and top safe area.
///
/// The type `T` specifies the return type of the route which can be supplied as
/// the route is popped from the stack via [Navigator.pop] by providing the
/// optional `result` argument.
///
/// See also:
///
///
/// * [CupertinoSheetRoute], which is the [PageRoute] version of this class
class CupertinoSheetPage<T> extends Page<T> {
/// Creates a material page.
const CupertinoSheetPage({
required this.child,
this.maintainState = true,
super.key,
super.name,
super.arguments,
});
/// The content to be shown in the [Route] created by this page.
final Widget child;
/// {@macro flutter.widgets.modalRoute.maintainState}
final bool maintainState;
@override
Route<T> createRoute(BuildContext context) {
return _PageBasedCupertinoSheetRoute<T>(page: this);
}
}
// A page-based version of SheetRoute.
//
// This route uses the builder from the page to build its content. This ensures
// the content is up to date after page updates.
class _PageBasedCupertinoSheetRoute<T> extends CupertinoSheetRoute<T> {
_PageBasedCupertinoSheetRoute({
required CupertinoSheetPage<T> page,
super.stops,
super.initialStop,
super.backgroundColor,
super.maintainState,
}) : super(
settings: page,
builder: (BuildContext context) {
return (ModalRoute.of(context)!.settings as CupertinoSheetPage<T>)
.child;
},
);
CupertinoSheetPage<T> get _page => settings as CupertinoSheetPage<T>;
@override
bool get maintainState => _page.maintainState;
@override
String get debugLabel => '${super.debugLabel}(${_page.name})';
}