-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmodal_sheet.dart
400 lines (334 loc) · 11.7 KB
/
modal_sheet.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
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
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import '../foundation/sheet_drag.dart';
import '../foundation/sheet_gesture_tamperer.dart';
import '../internal/float_comp.dart';
const _minFlingVelocityToDismiss = 1.0;
const _minDragDistanceToDismiss = 100.0; // Logical pixels.
const _minReleasedPageForwardAnimationTime = 300; // Milliseconds.
const _releasedPageForwardAnimationCurve = Curves.fastLinearToSlowEaseIn;
class ModalSheetPage<T> extends Page<T> {
const ModalSheetPage({
super.key,
super.name,
super.arguments,
super.restorationId,
this.maintainState = true,
this.barrierDismissible = true,
this.swipeDismissible = false,
this.fullscreenDialog = false,
this.barrierLabel,
this.barrierColor = Colors.black54,
this.transitionDuration = const Duration(milliseconds: 300),
this.transitionCurve = Curves.fastEaseInToSlowEaseOut,
required this.child,
});
/// The content to be shown in the [Route] created by this page.
final Widget child;
/// {@macro flutter.widgets.ModalRoute.maintainState}
final bool maintainState;
final bool fullscreenDialog;
final Color? barrierColor;
final bool barrierDismissible;
final String? barrierLabel;
final bool swipeDismissible;
final Duration transitionDuration;
final Curve transitionCurve;
@override
Route<T> createRoute(BuildContext context) {
return _PageBasedModalSheetRoute(
page: this,
fullscreenDialog: fullscreenDialog,
);
}
}
class _PageBasedModalSheetRoute<T> extends PageRoute<T>
with ModalSheetRouteMixin<T> {
_PageBasedModalSheetRoute({
required ModalSheetPage<T> page,
super.fullscreenDialog,
}) : super(settings: page);
ModalSheetPage<T> get _page => settings as ModalSheetPage<T>;
@override
bool get maintainState => _page.maintainState;
@override
Color? get barrierColor => _page.barrierColor;
@override
String? get barrierLabel => _page.barrierLabel;
@override
bool get barrierDismissible => _page.barrierDismissible;
@override
bool get swipeDismissible => _page.swipeDismissible;
@override
Curve get transitionCurve => _page.transitionCurve;
@override
Duration get transitionDuration => _page.transitionDuration;
@override
String get debugLabel => '${super.debugLabel}(${_page.name})';
@override
Widget buildContent(BuildContext context) => _page.child;
}
class ModalSheetRoute<T> extends PageRoute<T> with ModalSheetRouteMixin<T> {
ModalSheetRoute({
super.settings,
super.fullscreenDialog,
required this.builder,
this.maintainState = true,
this.barrierDismissible = true,
this.barrierLabel,
this.barrierColor = Colors.black54,
this.swipeDismissible = false,
this.transitionDuration = const Duration(milliseconds: 300),
this.transitionCurve = Curves.fastEaseInToSlowEaseOut,
});
final WidgetBuilder builder;
@override
final Color? barrierColor;
@override
final bool barrierDismissible;
@override
final String? barrierLabel;
@override
final bool swipeDismissible;
@override
final bool maintainState;
@override
final Duration transitionDuration;
@override
final Curve transitionCurve;
@override
Widget buildContent(BuildContext context) {
return builder(context);
}
}
mixin ModalSheetRouteMixin<T> on ModalRoute<T> {
bool get swipeDismissible;
Curve get transitionCurve;
@override
bool get opaque => false;
/// Lazily initialized in case `swipeDismissible` is set to false.
late final _swipeDismissibleController = _SwipeDismissibleController(
route: this,
transitionController: controller!,
);
Widget buildContent(BuildContext context);
@override
Widget buildPage(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
return switch (swipeDismissible) {
true => TamperSheetGesture(
tamperer: _swipeDismissibleController,
child: buildContent(context),
),
false => buildContent(context),
};
}
@override
Widget buildTransitions(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) {
final transitionTween = Tween(begin: const Offset(0, 1), end: Offset.zero);
// In the middle of a dismiss gesture drag,
// let the transition be linear to match finger motions.
final curve =
navigator!.userGestureInProgress ? Curves.linear : this.transitionCurve;
return SlideTransition(
position: animation.drive(
transitionTween.chain(CurveTween(curve: curve)),
),
child: child,
);
}
@override
Widget buildModalBarrier() {
void onDismiss() {
if (animation!.isCompleted && !navigator!.userGestureInProgress) {
navigator?.maybePop();
}
}
final barrierColor = this.barrierColor;
if (barrierColor != null && barrierColor.alpha != 0 && !offstage) {
assert(barrierColor != barrierColor.withOpacity(0.0));
return AnimatedModalBarrier(
onDismiss: onDismiss,
dismissible: barrierDismissible,
semanticsLabel: barrierLabel,
barrierSemanticsDismissible: semanticsDismissible,
color: animation!.drive(
ColorTween(
begin: barrierColor.withOpacity(0.0),
end: barrierColor,
).chain(CurveTween(curve: barrierCurve)),
),
);
} else {
return ModalBarrier(
onDismiss: onDismiss,
dismissible: barrierDismissible,
semanticsLabel: barrierLabel,
barrierSemanticsDismissible: semanticsDismissible,
);
}
}
}
class _SwipeDismissibleController with SheetGestureTamperer {
_SwipeDismissibleController({
required this.route,
required this.transitionController,
});
final ModalRoute<dynamic> route;
final AnimationController transitionController;
BuildContext get _context => route.subtreeContext!;
bool get _isUserGestureInProgress => route.navigator!.userGestureInProgress;
set _isUserGestureInProgress(bool inProgress) {
if (inProgress && !_isUserGestureInProgress) {
route.navigator!.didStartUserGesture();
} else if (!inProgress && _isUserGestureInProgress) {
route.navigator!.didStopUserGesture();
}
}
@override
SheetDragUpdateDetails tamperWithDragUpdate(
SheetDragUpdateDetails details,
Offset minPotentialDeltaConsumption,
) {
final dragDelta = switch (details.axisDirection) {
VerticalDirection.up => details.delta.dy,
// We flip the sign of the delta here because all of the following
// logic assumes that the axis direction is upwards.
VerticalDirection.down => -1 * details.delta.dy,
};
final minPDC = minPotentialDeltaConsumption.dy;
assert(details.delta.dy * minPDC >= 0);
final double effectiveDragDelta;
if (!transitionController.isCompleted) {
// Dominantly use the full pixels if it is in the middle of a transition.
effectiveDragDelta = dragDelta;
} else if (dragDelta < 0 &&
FloatComp.distance(MediaQuery.devicePixelRatioOf(_context))
.isNotApprox(dragDelta, minPDC) &&
MediaQuery.viewInsetsOf(_context).bottom == 0) {
// If the drag is downwards and the sheet may not consume the full pixels,
// then use the remaining pixels as the effective drag delta.
effectiveDragDelta = switch (details.axisDirection) {
VerticalDirection.up => dragDelta - minPDC,
VerticalDirection.down => dragDelta + minPDC,
};
assert(dragDelta * effectiveDragDelta >= 0);
} else {
// Otherwise, the drag delta doesn't change the transition progress.
return super.tamperWithDragUpdate(details, minPotentialDeltaConsumption);
}
final viewport = _context.size!.height;
final visibleViewport = viewport * transitionController.value;
assert(0 <= visibleViewport && visibleViewport <= viewport);
final newVisibleViewport =
(visibleViewport + effectiveDragDelta).clamp(0, viewport);
assert(viewport > 0);
final transitionProgress = newVisibleViewport / viewport;
assert(0 <= transitionProgress && transitionProgress <= 1);
_isUserGestureInProgress = transitionProgress < 1;
transitionController.value = transitionProgress;
final viewportDelta = newVisibleViewport - visibleViewport;
final unconsumedDragDelta = switch (details.axisDirection) {
VerticalDirection.up => dragDelta - viewportDelta,
VerticalDirection.down => viewportDelta - dragDelta,
};
return super.tamperWithDragUpdate(
details.copyWith(deltaY: unconsumedDragDelta),
minPotentialDeltaConsumption,
);
}
@override
SheetDragEndDetails tamperWithDragEnd(SheetDragEndDetails details) {
final wasHandled = _handleDragEnd(
velocity: details.velocity,
axisDirection: details.axisDirection,
);
return wasHandled
? super.tamperWithDragEnd(details.copyWith(velocityX: 0, velocityY: 0))
: super.tamperWithDragEnd(details);
}
@override
void onDragCancel(SheetDragCancelDetails details) {
super.onDragCancel(details);
_handleDragEnd(
axisDirection: details.axisDirection,
velocity: Velocity.zero,
);
}
bool _handleDragEnd({
required Velocity velocity,
required VerticalDirection axisDirection,
}) {
if (!_isUserGestureInProgress || transitionController.isAnimating) {
return false;
}
final viewportHeight = _context.size!.height;
final draggedDistance = viewportHeight * (1 - transitionController.value);
final effectiveVelocity = switch (axisDirection) {
VerticalDirection.up => velocity.pixelsPerSecond.dy / viewportHeight,
VerticalDirection.down =>
-1 * velocity.pixelsPerSecond.dy / viewportHeight,
};
final bool invokePop;
if (MediaQuery.viewInsetsOf(_context).bottom > 0) {
// The on-screen keyboard is open.
invokePop = false;
} else if (effectiveVelocity < 0) {
// Flings down.
invokePop = effectiveVelocity.abs() > _minFlingVelocityToDismiss;
} else if (FloatComp.velocity(MediaQuery.devicePixelRatioOf(_context))
.isApprox(effectiveVelocity, 0)) {
assert(draggedDistance >= 0);
// Dragged down enough to dismiss.
invokePop = draggedDistance > _minDragDistanceToDismiss;
} else {
// Flings up.
invokePop = false;
}
final didPop = invokePop && route.popDisposition == RoutePopDisposition.pop;
if (didPop) {
route.navigator!.pop();
} else if (!transitionController.isCompleted) {
// The route won't be popped, so animate the transition
// back to the origin.
final fraction = 1.0 - transitionController.value;
final animationTime = max(
(route.transitionDuration.inMilliseconds * fraction).floor(),
_minReleasedPageForwardAnimationTime,
);
const completedAnimationValue = 1.0;
unawaited(transitionController.animateTo(
completedAnimationValue,
duration: Duration(milliseconds: animationTime),
curve: _releasedPageForwardAnimationCurve,
));
}
if (transitionController.isAnimating) {
// Keep the userGestureInProgress in true state so we don't change the
// curve of the page transition mid-flight since the route's transition
// depends on userGestureInProgress.
late final AnimationStatusListener animationStatusCallback;
animationStatusCallback = (_) {
_isUserGestureInProgress = false;
transitionController.removeStatusListener(animationStatusCallback);
};
transitionController.addStatusListener(animationStatusCallback);
} else {
// Otherwise, reset the userGestureInProgress state immediately.
_isUserGestureInProgress = false;
}
if (invokePop) {
route.onPopInvoked(didPop);
}
return true;
}
}