-
-
Notifications
You must be signed in to change notification settings - Fork 648
Expand file tree
/
Copy pathauto_close_behavior.dart
More file actions
547 lines (470 loc) · 14.8 KB
/
auto_close_behavior.dart
File metadata and controls
547 lines (470 loc) · 14.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_slidable/src/controller.dart';
import 'package:flutter_slidable/src/notifications.dart';
/// A widget that forces the [Slidable] widgets below it to close when another
/// [Slidable] widget with the same [groupTag] opens.
class SlidableAutoCloseBehavior extends StatefulWidget {
/// Creates a [SlidableAutoCloseBehavior].
const SlidableAutoCloseBehavior({
super.key,
this.closeWhenOpened = true,
this.closeWhenTapped = true,
required this.child,
});
/// Indicates whether all the [Slidable] within the same group should be
/// closed when one of the group is opened.
///
/// Defaults to true.
final bool closeWhenOpened;
/// Indicates whether all the [Slidable] within the same group should be
/// closed when one of the group is tapped while one is opened.
///
/// Defaults to true.
final bool closeWhenTapped;
/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.ProxyWidget.child}
final Widget child;
@override
State<SlidableAutoCloseBehavior> createState() =>
_SlidableAutoCloseBehaviorState();
}
class _SlidableAutoCloseBehaviorState extends State<SlidableAutoCloseBehavior> {
final Map<Object?, int> openSlidables = {};
@override
Widget build(BuildContext context) {
return _SlidableAutoCloseData(
closeWhenOpened: widget.closeWhenOpened,
closeWhenTapped: widget.closeWhenTapped,
child: SlidableGroupBehavior<SlidableAutoCloseNotification>(
child: SlidableGroupBehavior<SlidableAutoCloseBarrierNotification>(
onNotification: (notification) {
final key = notification.groupTag;
final previousOpenForThatTag =
openSlidables.putIfAbsent(key, () => 0);
final openForThatTag =
previousOpenForThatTag + (notification.enabled ? 1 : -1);
openSlidables[key] = openForThatTag;
if (openForThatTag == 0 || previousOpenForThatTag == 0) {
return notification;
}
return null;
},
child: widget.child,
),
),
);
}
}
class _SlidableAutoCloseData extends InheritedWidget {
const _SlidableAutoCloseData({
required this.closeWhenOpened,
required this.closeWhenTapped,
required super.child,
});
final bool closeWhenOpened;
final bool closeWhenTapped;
@override
bool updateShouldNotify(_SlidableAutoCloseData oldWidget) {
return oldWidget.closeWhenOpened != closeWhenOpened ||
oldWidget.closeWhenTapped != closeWhenTapped;
}
static _SlidableAutoCloseData? of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<_SlidableAutoCloseData>();
}
}
/// INTERNAL USE
class SlidableAutoCloseBehaviorInteractor extends StatelessWidget {
/// INTERNAL USE
const SlidableAutoCloseBehaviorInteractor({
super.key,
required this.groupTag,
required this.controller,
required this.child,
});
/// {@macro slidable.groupTag}
final Object? groupTag;
/// {@macro slidable.controller}
final SlidableController controller;
/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.ProxyWidget.child}
final Widget child;
@override
Widget build(BuildContext context) {
return SlidableAutoCloseInteractor(
groupTag: groupTag,
controller: controller,
child: SlidableAutoCloseBarrierInteractor(
groupTag: groupTag,
controller: controller,
child: child,
),
);
}
}
/// A notification used to close other [Slidable] widgets with the same
/// [groupTag].
@immutable
class SlidableAutoCloseNotification {
/// Creates a notification that can be used to close other [Slidable] widgets
/// with the same [groupTag].
const SlidableAutoCloseNotification({
required this.groupTag,
required this.controller,
this.closeSelf = false,
});
/// {@macro slidable.groupTag}
final Object? groupTag;
/// {@template slidable.controller}
/// The [SlidableController] associated.
/// {@endtemplate}
final SlidableController controller;
/// Whether the [Slidable] where this notification was sent should also close.
final bool closeSelf;
}
/// INTERNAL USE
class SlidableAutoCloseInteractor extends StatelessWidget {
/// INTERNAL USE
const SlidableAutoCloseInteractor({
super.key,
required this.groupTag,
required this.controller,
required this.child,
});
/// {@macro slidable.groupTag}
final Object? groupTag;
/// {@macro slidable.controller}
final SlidableController controller;
/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.ProxyWidget.child}
final Widget child;
@override
Widget build(BuildContext context) {
return SlidableAutoCloseNotificationSender(
groupTag: groupTag,
controller: controller,
child: SlidableAutoCloseBehaviorListener(
groupTag: groupTag,
controller: controller,
child: child,
),
);
}
}
/// INTERNAL USE
class SlidableAutoCloseBehaviorListener extends StatelessWidget {
/// INTERNAL USE
const SlidableAutoCloseBehaviorListener({
super.key,
required this.groupTag,
required this.controller,
required this.child,
});
/// {@macro slidable.groupTag}
final Object? groupTag;
/// {@macro slidable.controller}
final SlidableController controller;
/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.ProxyWidget.child}
final Widget child;
@override
Widget build(BuildContext context) {
return SlidableGroupBehaviorListener<SlidableAutoCloseNotification>(
onNotification: (SlidableAutoCloseNotification notification) {
if (groupTag == notification.groupTag &&
(notification.closeSelf || notification.controller != controller) &&
!controller.closing) {
controller.close();
}
},
child: child,
);
}
}
/// INTERNAL USE
class SlidableAutoCloseNotificationSender extends StatelessWidget {
/// INTERNAL USE
const SlidableAutoCloseNotificationSender({
super.key,
required this.groupTag,
required this.controller,
required this.child,
});
/// {@macro slidable.groupTag}
final Object? groupTag;
/// {@macro slidable.controller}
final SlidableController controller;
/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.ProxyWidget.child}
final Widget child;
void _handleStatusChanged(BuildContext context, AnimationStatus status) {
final moving =
status == AnimationStatus.forward || status == AnimationStatus.reverse;
if (moving && !controller.closing) {
SlidableGroupNotification.dispatch(
context,
SlidableAutoCloseNotification(
groupTag: groupTag,
controller: controller,
),
assertParentExists: false,
);
}
}
@override
Widget build(BuildContext context) {
return _SlidableNotificationSender(
controller: controller,
onStatusChanged: (status) => _handleStatusChanged(context, status),
enabled: _SlidableAutoCloseData.of(context)?.closeWhenOpened ?? false,
child: child,
);
}
}
/// A notification used to indicate if a barrier should be pub on [Slidable]
@immutable
class SlidableAutoCloseBarrierNotification {
/// Creates a notification to activate/deactivate the barrier on [Slidable]
/// widgets.
const SlidableAutoCloseBarrierNotification({
required this.groupTag,
required this.controller,
this.enabled = false,
});
/// {@macro slidable.groupTag}
final Object? groupTag;
/// {@template slidable.controller}
/// The [SlidableController] associated.
/// {@endtemplate}
final SlidableController controller;
/// Whether the barrier is enabled.
final bool enabled;
}
/// INTERNAL USE
class SlidableAutoCloseBarrierInteractor extends StatelessWidget {
/// INTERNAL USE
const SlidableAutoCloseBarrierInteractor({
super.key,
required this.groupTag,
required this.controller,
required this.child,
});
/// {@macro slidable.groupTag}
final Object? groupTag;
/// {@macro slidable.controller}
final SlidableController controller;
/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.ProxyWidget.child}
final Widget child;
@override
Widget build(BuildContext context) {
return SlidableAutoCloseBarrierNotificationSender(
groupTag: groupTag,
controller: controller,
child: SlidableAutoCloseBarrierBehaviorListener(
groupTag: groupTag,
controller: controller,
child: child,
),
);
}
}
/// INTERNAL USE
class SlidableAutoCloseBarrierNotificationSender extends StatefulWidget {
/// INTERNAL USE
const SlidableAutoCloseBarrierNotificationSender({
super.key,
required this.groupTag,
required this.controller,
required this.child,
});
/// {@macro slidable.groupTag}
final Object? groupTag;
/// {@macro slidable.controller}
final SlidableController controller;
/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.ProxyWidget.child}
final Widget child;
@override
State<SlidableAutoCloseBarrierNotificationSender> createState() =>
_SlidableAutoCloseBarrierNotificationSenderState();
}
class _SlidableAutoCloseBarrierNotificationSenderState
extends State<SlidableAutoCloseBarrierNotificationSender> {
SlidableGroupNotificationDispatcher? dispatcher;
void _handleStatusChanged(AnimationStatus status) {
//TODO(romain): There is a bug if more than one try to open at the same time.
final willBarrierBeEnabled = status != AnimationStatus.dismissed;
final barrierEnabled = dispatcher != null;
if (willBarrierBeEnabled != barrierEnabled) {
dispatcher = SlidableGroupNotification.createDispatcher<
SlidableAutoCloseBarrierNotification>(
context,
assertParentExists: false,
);
dispatchSlidableAutoCloseBarrierNotification(
enabled: willBarrierBeEnabled,
);
if (!willBarrierBeEnabled) {
// We can set the dispatcher to null because we won't need it anymore.
dispatcher = null;
}
}
}
void dispatchSlidableAutoCloseBarrierNotification({required bool enabled}) {
final notification = SlidableAutoCloseBarrierNotification(
groupTag: widget.groupTag,
controller: widget.controller,
enabled: enabled,
);
dispatcher?.dispatch(notification);
}
@override
void dispose() {
if (dispatcher != null) {
// If we still have a dispatcher, it means that this widget was disposed
// while the barrier was still enabled for this group.
// We need to release the barrier.
// In Flutter 3, [SchedulerBinding.instance] is not nullable, but since
// we want to support Flutter 2, this is a simple way to do it without
// having a build warning.
// ignore: unnecessary_nullable_for_final_variable_declarations
final SchedulerBinding? schedulerBinding = SchedulerBinding.instance;
schedulerBinding?.addPostFrameCallback((Duration _) {
// We call it in the next frame to avoid to rebuild a widget that is
// already rebuilding.
dispatchSlidableAutoCloseBarrierNotification(enabled: false);
});
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return _SlidableNotificationSender(
controller: widget.controller,
onStatusChanged: _handleStatusChanged,
enabled: _SlidableAutoCloseData.of(context)?.closeWhenTapped ?? false,
child: widget.child,
);
}
}
/// INTERNAL USE
class SlidableAutoCloseBarrierBehaviorListener extends StatefulWidget {
/// INTERNAL USE
const SlidableAutoCloseBarrierBehaviorListener({
super.key,
required this.groupTag,
required this.controller,
required this.child,
});
/// {@macro slidable.groupTag}
final Object? groupTag;
/// {@macro slidable.controller}
final SlidableController controller;
/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.ProxyWidget.child}
final Widget child;
@override
_SlidableAutoCloseBarrierBehaviorListenerState createState() =>
_SlidableAutoCloseBarrierBehaviorListenerState();
}
class _SlidableAutoCloseBarrierBehaviorListenerState
extends State<SlidableAutoCloseBarrierBehaviorListener> {
bool absorbing = false;
void handleOnTap() {
if (!widget.controller.closing) {
SlidableGroupNotification.dispatch(
context,
SlidableAutoCloseNotification(
groupTag: widget.groupTag,
controller: widget.controller,
closeSelf: true,
),
assertParentExists: false,
);
}
}
@override
Widget build(BuildContext context) {
return SlidableGroupBehaviorListener<SlidableAutoCloseBarrierNotification>(
onNotification: (SlidableAutoCloseBarrierNotification notification) {
if (widget.groupTag == notification.groupTag) {
if (mounted) {
setState(() {
absorbing = notification.enabled;
});
}
}
},
child: GestureDetector(
onTap: absorbing ? handleOnTap : null,
child: AbsorbPointer(
absorbing: absorbing,
child: widget.child,
),
),
);
}
}
/// INTERNAL USE
class _SlidableNotificationSender extends StatefulWidget {
/// INTERNAL USE
const _SlidableNotificationSender({
required this.controller,
required this.onStatusChanged,
required this.enabled,
required this.child,
});
final SlidableController controller;
final AnimationStatusListener onStatusChanged;
final Widget child;
final bool enabled;
@override
_SlidableNotificationSenderState createState() =>
_SlidableNotificationSenderState();
}
class _SlidableNotificationSenderState
extends State<_SlidableNotificationSender> {
@override
void initState() {
super.initState();
addListeners(widget);
}
@override
void didUpdateWidget(_SlidableNotificationSender oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.controller != widget.controller ||
oldWidget.onStatusChanged != widget.onStatusChanged) {
removeListeners(oldWidget);
addListeners(widget);
}
}
@override
void dispose() {
removeListeners(widget);
super.dispose();
}
void handleStatusChanged(AnimationStatus status) {
if (widget.enabled) {
widget.onStatusChanged(status);
}
}
void addListeners(_SlidableNotificationSender widget) {
widget.controller.animation.addStatusListener(handleStatusChanged);
}
void removeListeners(_SlidableNotificationSender widget) {
widget.controller.animation.removeStatusListener(handleStatusChanged);
}
@override
Widget build(BuildContext context) {
return widget.child;
}
}