-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathobserver_widget.dart
More file actions
653 lines (591 loc) · 21.9 KB
/
observer_widget.dart
File metadata and controls
653 lines (591 loc) · 21.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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
* @Author: LinXunFeng linxunfeng@yeah.net
* @Repo: https://github.com/LinXunFeng/flutter_scrollview_observer
* @Date: 2022-08-08 00:20:03
*/
import 'dart:async';
import 'dart:collection';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:scrollview_observer/src/common/models/observer_handle_contexts_result_model.dart';
import 'package:scrollview_observer/src/common/observer_controller.dart';
import 'package:scrollview_observer/src/common/observer_listener.dart';
import 'package:scrollview_observer/src/common/observer_typedef.dart';
import 'package:scrollview_observer/src/common/observer_widget_scope.dart';
import 'package:scrollview_observer/src/common/observer_widget_tag_manager.dart';
import 'package:scrollview_observer/src/common/typedefs.dart';
import 'package:scrollview_observer/src/notification.dart';
import 'package:scrollview_observer/src/utils/src/log.dart';
import 'models/observe_model.dart';
class ObserverWidget<C extends ObserverController, M extends ObserveModel,
N extends ScrollViewOnceObserveNotification> extends StatefulWidget {
/// The subtree below this widget.
final Widget child;
/// This is for when you have multiple nested [ObserverWidget] widgets and
/// you want to get the corresponding [ObserverWidgetState].
///
/// It must be a unique string in the tree.
final String? tag;
/// An object that can be used to dispatch a [ListViewOnceObserveNotification]
/// or [GridViewOnceObserveNotification].
final C? sliverController;
/// The callback of getting all sliver's buildContext.
final List<BuildContext> Function()? sliverContexts;
/// The callback of getting observed result map.
final OnObserveAllCallback<M>? onObserveAll;
/// The callback of getting observed result for first sliver.
final OnObserveCallback<M>? onObserve;
/// Calculate offset.
final double leadingOffset;
/// Calculate offset dynamically
/// If this callback is implemented, the [leadingOffset] property will be
/// invalid.
final double Function()? dynamicLeadingOffset;
/// After the internal logic figure out the first child widget, if the
/// proportion of the size of the child widget blocked to its own size exceeds
/// the value [toNextOverPercent], the next child widget will be the first
/// child widget.
final double toNextOverPercent;
/// A predicate for [ScrollNotification], used to determine whether
/// observation can be triggered.
///
/// Generally combined with [defaultScrollNotificationPredicate] to check
/// whether `notification.depth == 0`, which means that the notification did
/// not bubble through any intervening scrolling widgets.
/// This can avoid the unnecessary observation calculations caused by
/// intervening scrolling widgets, which in turn improves performance.
final ScrollNotificationPredicate? scrollNotificationPredicate;
/// Used to set types those can trigger observe automatically.
///
/// Defaults to [.scrollStart, .scrollUpdate, .scrollEnd]
final List<ObserverAutoTriggerObserveType>? autoTriggerObserveTypes;
/// Used to set the prerequisite for triggering the [onObserve] callback.
///
/// Defaults to [ObserverTriggerOnObserveType.displayingItemsChange].
final ObserverTriggerOnObserveType triggerOnObserveType;
/// Used to find the target RenderSliver.
///
/// The default is to find [RenderSliverList], [RenderSliverFixedExtentList]
/// and [RenderSliverGrid].
final bool Function(RenderObject?)? customTargetRenderSliverType;
/// It allows you to customize observation logic when original logic doesn't
/// fit your needs.
final M? Function(BuildContext)? customHandleObserve;
const ObserverWidget({
Key? key,
required this.child,
this.tag,
this.sliverController,
this.sliverContexts,
this.onObserveAll,
this.onObserve,
this.leadingOffset = 0,
this.dynamicLeadingOffset,
this.toNextOverPercent = 1,
this.scrollNotificationPredicate,
this.autoTriggerObserveTypes,
this.triggerOnObserveType =
ObserverTriggerOnObserveType.displayingItemsChange,
this.customHandleObserve,
this.customTargetRenderSliverType,
}) : assert(toNextOverPercent > 0 && toNextOverPercent <= 1),
super(key: key);
@override
State<ObserverWidget> createState() =>
ObserverWidgetState<C, M, N, ObserverWidget<C, M, N>>();
/// Returning the closest instance of this class that encloses the given
/// context.
///
/// If you give a tag, it will give priority find the corresponding instance
/// of this class with the given tag and return it.
///
/// If there is no [ObserverWidget] widget, then null is returned.
///
/// Calling this method will create a dependency on the closest
/// [ObserverWidget] in the [context], if there is one.
///
/// See also:
///
/// * [ObserverWidget.of], which is similar to this method, but asserts if no
/// [ObserverWidget] instance is found.
static ObserverWidgetState<C, M, N, T>? maybeOf<
C extends ObserverController,
M extends ObserveModel,
N extends ScrollViewOnceObserveNotification,
T extends ObserverWidget<C, M, N>>(
BuildContext context, {
String? tag,
}) {
BuildContext? _ctx;
if (tag != null) {
final tagManager = ObserverWidgetTagManager.maybeOf(context);
_ctx = tagManager?.context(tag);
}
return (_ctx ?? context)
.dependOnInheritedWidgetOfExactType<ObserverWidgetScope<C, M, N, T>>()
?.observerWidgetState;
}
/// Returning the closest instance of this class that encloses the given
/// context.
///
/// If you give a tag, it will give priority find the corresponding instance
/// of this class with the given tag and return it.
///
/// If no instance is found, this method will assert in debug mode, and throw
/// an exception in release mode.
///
/// Calling this method will create a dependency on the closest
/// [ObserverWidget] in the [context].
///
/// See also:
///
/// * [ObserverWidget.maybeOf], which is similar to this method, but returns
/// null if no [ObserverWidget] instance is found.
static ObserverWidgetState<C, M, N, T> of<
C extends ObserverController,
M extends ObserveModel,
N extends ScrollViewOnceObserveNotification,
T extends ObserverWidget<C, M, N>>(
BuildContext context, {
String? tag,
}) {
final observerState = maybeOf<C, M, N, T>(
context,
tag: tag,
);
assert(() {
if (observerState == null) {
throw FlutterError(
'$T.of() was called with a context that does not contain a '
'$T widget.\n'
'No $T widget ancestor could be found starting from the '
'context that was passed to $T.of(). This can happen '
'because you are using a widget that looks for a $T '
'ancestor, but no such ancestor exists.\n'
'The context used was:\n'
' $context',
);
}
return true;
}());
return observerState!;
}
}
class ObserverWidgetState<
C extends ObserverController,
M extends ObserveModel,
N extends ScrollViewOnceObserveNotification,
T extends ObserverWidget<C, M, N>> extends State<T> {
/// Target sliver [BuildContext]
List<BuildContext> targetSliverContexts = [];
/// The last observation result
Map<BuildContext, M> lastResultMap = {};
/// Default values for the widget's autoTriggerObserveTypes property.
List<ObserverAutoTriggerObserveType> get innerAutoTriggerObserveTypes =>
widget.autoTriggerObserveTypes ??
[
ObserverAutoTriggerObserveType.scrollStart,
ObserverAutoTriggerObserveType.scrollUpdate,
ObserverAutoTriggerObserveType.scrollEnd
];
/// Mapping [ObserverAutoTriggerObserveType] to [ScrollNotification].
List<Type> get innerAutoTriggerObserveScrollNotifications =>
innerAutoTriggerObserveTypes.map((type) {
switch (type) {
case ObserverAutoTriggerObserveType.scrollStart:
return ScrollStartNotification;
case ObserverAutoTriggerObserveType.scrollUpdate:
return ScrollUpdateNotification;
case ObserverAutoTriggerObserveType.scrollEnd:
return ScrollEndNotification;
}
}).toList();
/// Whether can handle observe.
bool innerCanHandleObserve = true;
/// The [BuildContext] of the [ObserverWidgetScope].
BuildContext? scopeContext;
/// The listener list state for a [ObserverWidget] returned by
/// [ObserverWidget.of].
///
/// It supports a listener list instead of just a single observation
/// callback (such as onObserve and onObserveAll).
@protected
@visibleForTesting
LinkedList<ObserverListenerEntry<M>>? innerListeners =
LinkedList<ObserverListenerEntry<M>>();
bool _debugAssertNotDisposed() {
assert(() {
if (innerListeners == null) {
throw FlutterError(
'A $runtimeType was used after being disposed.\n'
'Once you have called dispose() on a $runtimeType, it can no longer be used.',
);
}
return true;
}());
return true;
}
@override
void initState() {
super.initState();
_setupSliverController(isInitState: true);
}
@override
void didUpdateWidget(covariant T oldWidget) {
super.didUpdateWidget(oldWidget);
_checkTagChange(oldWidget);
}
@override
void dispose() {
assert(_debugAssertNotDisposed());
innerListeners?.clear();
innerListeners = null;
super.dispose();
}
@override
Widget build(BuildContext context) {
// Placed at the deepest level for convenient subsequent operations using
// its context.
Widget resultWidget = ObserverWidgetScope<C, M, N, T>(
child: widget.child,
observerWidgetState: this,
onCreateElement: _handleScopeContext,
);
resultWidget = NotificationListener<N>(
onNotification: (notification) {
final result = handleContexts(
isForceObserve: notification.isForce,
isFromObserveNotification: true,
isDependObserveCallback: notification.isDependObserveCallback,
);
final sliverController = widget.sliverController;
if (sliverController is ObserverControllerForNotification) {
sliverController.innerHandleDispatchOnceObserveComplete(
resultModel: result,
);
}
return false;
},
child: NotificationListener<ScrollNotification>(
onNotification: (notification) {
// If the scrollNotificationPredicate returns false, the notification
// will be ignored.
if (!(widget.scrollNotificationPredicate?.call(notification) ??
true)) {
return false;
}
// If the notification.runtimeType is not in the list of
// innerAutoTriggerObserveScrollNotifications that can trigger
// observation, the notification will be ignored.
if (innerAutoTriggerObserveScrollNotifications
.contains(notification.runtimeType)) {
final isIgnoreInnerCanHandleObserve =
ScrollUpdateNotification != notification.runtimeType;
WidgetsBinding.instance.endOfFrame.then((_) {
// Need to wait for frame end to avoid inaccurate observation
// result, reasons as follows
//
// ======================== WEB ========================
//
// Getting bad observation result because scrolling in Flutter Web
// with mouse wheel is not smooth.
// https://github.com/flutter/flutter/issues/78708
// https://github.com/flutter/flutter/issues/78634
//
// issue
// https://github.com/LinXunFeng/flutter_scrollview_observer/issues/31
//
// ======================== APP ========================
//
// When using ScrollController's animateTo with a value exceeding
// the maximum scroll range, it will lead to inaccurate
// observation result.
//
// issue
// https://github.com/fluttercandies/flutter_scrollview_observer/issues/113
handleContexts(
isIgnoreInnerCanHandleObserve: isIgnoreInnerCanHandleObserve,
);
});
}
return false;
},
child: resultWidget,
),
);
// When nesting multiple ObserverWidgets, ensure that only one
// ObserverWidgetTagManager is at the top.
if (ObserverWidgetTagManager.maybeOf(context) == null) {
resultWidget = ObserverWidgetTagManager(
child: resultWidget,
);
}
return resultWidget;
}
/// Setup sliver controller
_setupSliverController({bool isInitState = false}) {
final sliverController = widget.sliverController;
if (sliverController == null) return;
sliverController.innerReset();
sliverController.innerNeedOnceObserveCallBack = () {
handleContexts();
};
sliverController.innerReattachCallBack = () {
targetSliverContexts.clear();
_setupSliverController();
};
ambiguate(WidgetsBinding.instance)?.addPostFrameCallback((timeStamp) {
targetSliverContexts = fetchTargetSliverContexts();
sliverController.sliverContexts = targetSliverContexts;
if (isInitState && sliverController is ObserverControllerForScroll) {
sliverController.innerInitialIndexPosition();
}
});
}
/// Fetch target sliver [BuildContext]s
List<BuildContext> fetchTargetSliverContexts() {
List<BuildContext> ctxs = targetSliverContexts;
if (ctxs.isEmpty) {
final sliverListContexts = widget.sliverContexts;
if (sliverListContexts != null) {
ctxs = sliverListContexts();
} else {
List<BuildContext> _ctxs = [];
void visitor(Element element) {
if (isTargetSliverContextType(element.renderObject)) {
/// Find the target sliver context
_ctxs.add(element);
return;
}
element.visitChildren(visitor);
}
try {
// https://github.com/LinXunFeng/flutter_scrollview_observer/issues/35
context.visitChildElements(visitor);
} catch (e) {
Log.warning(
'This widget has been unmounted, so the State no longer has a context (and should be considered defunct). \n'
'Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.',
);
}
ctxs = _ctxs;
}
}
return ctxs;
}
/// Fetch offset from [leadingOffset] or [dynamicLeadingOffset].
double fetchLeadingOffset() {
var offset = widget.leadingOffset;
if (widget.dynamicLeadingOffset != null) {
offset = widget.dynamicLeadingOffset!();
}
return offset;
}
/// Determine whether it is the type of the target sliver.
bool isTargetSliverContextType(RenderObject? obj) {
if (widget.customTargetRenderSliverType != null) {
return widget.customTargetRenderSliverType!.call(obj);
}
return obj is RenderSliverList;
}
/// Update [innerCanHandleObserve] according to the
/// [ObserverController.observeIntervalForScrolling].
updateInnerCanHandleObserve() async {
final observeInterval =
widget.sliverController?.observeIntervalForScrolling ?? Duration.zero;
if (Duration.zero == observeInterval) {
innerCanHandleObserve = true;
return;
}
if (!innerCanHandleObserve) return;
innerCanHandleObserve = false;
await Future.delayed(observeInterval);
innerCanHandleObserve = true;
}
/// Handle all buildContext
ObserverHandleContextsResultModel<M>? handleContexts({
bool isForceObserve = false,
bool isFromObserveNotification = false,
bool isDependObserveCallback = true,
bool isIgnoreInnerCanHandleObserve = true,
}) {
if (!isIgnoreInnerCanHandleObserve) {
if (!innerCanHandleObserve) {
return null;
}
updateInnerCanHandleObserve();
}
final isForbidObserveCallback =
widget.sliverController?.isForbidObserveCallback ?? false;
final onObserve = isForbidObserveCallback ? null : widget.onObserve;
final onObserveAll = isForbidObserveCallback ? null : widget.onObserveAll;
if (isDependObserveCallback) {
if (onObserve == null &&
onObserveAll == null &&
(innerListeners?.isEmpty ?? true)) {
return null;
}
}
final isHandlingScroll =
widget.sliverController?.innerIsHandlingScroll ?? false;
if (isHandlingScroll) {
return null;
}
List<BuildContext> ctxs = fetchTargetSliverContexts();
Map<BuildContext, M> resultMap = {};
Map<BuildContext, M> changeResultMap = {};
M? changeResultModel;
for (var i = 0; i < ctxs.length; i++) {
final ctx = ctxs[i];
final targetObserveModel = handleObserve(ctx);
if (targetObserveModel == null) continue;
resultMap[ctx] = targetObserveModel;
if (isForceObserve ||
widget.triggerOnObserveType ==
ObserverTriggerOnObserveType.directly) {
changeResultMap[ctx] = targetObserveModel;
} else {
final lastResultModel = lastResultMap[ctx];
if (lastResultModel == null) {
changeResultMap[ctx] = targetObserveModel;
} else if (lastResultModel != targetObserveModel) {
changeResultMap[ctx] = targetObserveModel;
}
}
// Getting observed result for first listView.
if (i == 0 && changeResultMap[ctx] != null) {
changeResultModel = changeResultMap[ctx];
}
}
lastResultMap = resultMap;
if (isDependObserveCallback &&
onObserve != null &&
changeResultModel != null) {
onObserve(changeResultModel);
}
if (isDependObserveCallback &&
onObserveAll != null &&
changeResultMap.isNotEmpty) {
onObserveAll(changeResultMap);
}
_notifyListeners(changeResultMap);
return ObserverHandleContextsResultModel(
changeResultModel: changeResultModel,
changeResultMap: changeResultMap,
);
}
M? handleObserve(BuildContext ctx) {
if (widget.customHandleObserve != null) {
return widget.customHandleObserve?.call(ctx);
}
return null;
}
void _handleScopeContext(BuildContext ctx) async {
scopeContext = ctx;
final tag = widget.tag ?? '';
if (tag.isEmpty) return;
await WidgetsBinding.instance.endOfFrame;
assert(ctx.mounted);
final tagManager = ObserverWidgetTagManager.maybeOf(ctx);
tagManager?.set(tag, ctx);
}
void _checkTagChange(T oldWidget) async {
final oldTag = oldWidget.tag ?? '';
final tag = widget.tag ?? '';
if (tag == oldWidget) return;
// Execute after the current frame ends to avoid getting an outdated
// ObserverWidgetTagManager.
await WidgetsBinding.instance.endOfFrame;
final _scopeContext = scopeContext;
if (_scopeContext == null) return;
assert(_scopeContext.mounted);
final tagManager = ObserverWidgetTagManager.maybeOf(_scopeContext);
tagManager?.remove(oldTag);
if (tag.isNotEmpty) {
tagManager?.set(tag, _scopeContext);
}
}
/// Add [OnObserveCallback] and [OnObserveAllCallback] that will be called
/// each time a result is observed.
void addListener({
BuildContext? context,
OnObserveCallback<M>? onObserve,
OnObserveAllCallback<M>? onObserveAll,
}) {
assert(_debugAssertNotDisposed());
assert(
onObserve != null || onObserveAll != null,
'At least one callback must be provided.',
);
innerListeners?.add(ObserverListenerEntry<M>(
context: context,
onObserve: onObserve,
onObserveAll: onObserveAll,
));
}
/// Remove the specified [OnObserveCallback] and [OnObserveAllCallback].
void removeListener({
BuildContext? context,
OnObserveCallback<M>? onObserve,
OnObserveAllCallback<M>? onObserveAll,
}) {
assert(_debugAssertNotDisposed());
assert(
onObserve != null || onObserveAll != null,
'At least one callback must be provided.',
);
final _listeners = innerListeners;
if (_listeners == null) return;
for (final ObserverListenerEntry<M> entry in _listeners) {
if (entry.context == context &&
entry.onObserve == onObserve &&
entry.onObserveAll == onObserveAll) {
entry.unlink();
return;
}
}
}
void _notifyListeners(
Map<BuildContext, M> changeResultMap,
) {
if (changeResultMap.isEmpty) return;
final _listeners = innerListeners;
if (_listeners == null || _listeners.isEmpty) return;
final List<ObserverListenerEntry<M>> localListeners =
List<ObserverListenerEntry<M>>.of(_listeners);
for (final ObserverListenerEntry<M> entry in localListeners) {
try {
if (entry.list != null) {
entry.onObserveAll?.call(changeResultMap);
if (entry.onObserve != null) {
// If sliverContext is not specified, the first one in
// targetSliverContexts is taken.
BuildContext? _sliverContext = entry.context;
if (_sliverContext == null && targetSliverContexts.isNotEmpty) {
_sliverContext = targetSliverContexts.first;
}
final result = changeResultMap[_sliverContext];
if (result == null) continue;
entry.onObserve?.call(result);
}
}
} catch (exception, stack) {
FlutterError.reportError(FlutterErrorDetails(
exception: exception,
stack: stack,
library: 'scrollview_observer',
context:
ErrorDescription('while dispatching result for $runtimeType'),
informationCollector: () => <DiagnosticsNode>[
DiagnosticsProperty<ObserverWidgetState>(
'The $runtimeType sending result was',
this,
style: DiagnosticsTreeStyle.errorProperty,
),
],
));
}
}
}
}