Skip to content

Commit 52fa3b8

Browse files
committed
feat(ChatScrollObserver): add customAdjustPositionDelta
1 parent 84d4d64 commit 52fa3b8

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

lib/src/utils/src/chat/chat_observer_scroll_physics_mixin.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,27 @@ mixin ChatObserverScrollPhysicsMixin on ScrollPhysics {
5252
mode: observer.innerMode,
5353
changeCount: observer.changeCount,
5454
));
55-
final delta = model.layoutOffset - observer.innerRefItemLayoutOffset;
55+
56+
// Customize the delta of the adjustPosition.
57+
double? customDelta = observer.customAdjustPositionDelta?.call(
58+
ChatScrollObserverCustomAdjustPositionDeltaModel(
59+
oldPosition: oldPosition,
60+
newPosition: newPosition,
61+
isScrolling: isScrolling,
62+
velocity: velocity,
63+
observer: observer,
64+
currentItemModel: model,
65+
),
66+
);
67+
68+
// Calculate the final delta.
69+
//
70+
// If the customDelta is not null, use the customDelta.
71+
// Otherwise, use the layoutOffset minus innerRefItemLayoutOffset to get
72+
// the difference in the leading offset of the item.
73+
final delta =
74+
customDelta ?? (model.layoutOffset - observer.innerRefItemLayoutOffset);
75+
5676
return adjustPosition + delta;
5777
}
5878

lib/src/utils/src/chat/chat_scroll_observer.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class ChatScrollObserver {
7373
/// The mode of processing.
7474
ChatScrollObserverHandleMode innerMode = ChatScrollObserverHandleMode.normal;
7575

76+
/// Customize the delta of the adjustPosition.
77+
ChatScrollObserverCustomAdjustPositionDelta? customAdjustPositionDelta;
78+
7679
/// Observation result of reference subparts after ScrollView children update.
7780
ListViewObserveDisplayingChildModel? observeRefItem() {
7881
return observerController.observeItem(
@@ -109,6 +112,7 @@ class ChatScrollObserver {
109112
int refItemRelativeIndexAfterUpdate = 0,
110113
int refItemIndex = 0,
111114
int refItemIndexAfterUpdate = 0,
115+
ChatScrollObserverCustomAdjustPositionDelta? customAdjustPositionDelta,
112116
}) async {
113117
innerMode = mode;
114118
this.isRemove = isRemove;
@@ -196,6 +200,7 @@ class ChatScrollObserver {
196200
innerRefItemIndex = _innerRefItemIndex;
197201
innerRefItemIndexAfterUpdate = _innerRefItemIndexAfterUpdate;
198202
innerRefItemLayoutOffset = _innerRefItemLayoutOffset;
203+
this.customAdjustPositionDelta = customAdjustPositionDelta;
199204

200205
// When the heights of items are similar, the viewport will not call
201206
// [performLayout], In this case, the [adjustPositionForNewDimensions] of

lib/src/utils/src/chat/chat_scroll_observer_model.dart

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
* @Repo: https://github.com/LinXunFeng/flutter_scrollview_observer
44
* @Date: 2023-05-13 10:33:00
55
*/
6-
import 'chat_scroll_observer_typedefs.dart';
6+
import 'package:flutter/material.dart';
7+
8+
import 'package:scrollview_observer/src/common/models/observe_displaying_child_model_mixin.dart';
9+
import 'package:scrollview_observer/src/utils/src/chat/chat_scroll_observer.dart';
10+
import 'package:scrollview_observer/src/utils/src/chat/chat_scroll_observer_typedefs.dart';
711

812
class ChatScrollObserverHandlePositionResultModel {
913
/// The type of processing location.
@@ -21,3 +25,32 @@ class ChatScrollObserverHandlePositionResultModel {
2125
required this.changeCount,
2226
});
2327
}
28+
29+
class ChatScrollObserverCustomAdjustPositionDeltaModel {
30+
/// The old position.
31+
final ScrollMetrics oldPosition;
32+
33+
/// The new position.
34+
final ScrollMetrics newPosition;
35+
36+
/// Whether the ScrollView is scrolling.
37+
final bool isScrolling;
38+
39+
/// The current velocity of the scroll position.
40+
final double velocity;
41+
42+
/// The [ChatScrollObserver] instance.
43+
final ChatScrollObserver observer;
44+
45+
/// The observation result of the current item.
46+
final ObserveDisplayingChildModelMixin currentItemModel;
47+
48+
ChatScrollObserverCustomAdjustPositionDeltaModel({
49+
required this.oldPosition,
50+
required this.newPosition,
51+
required this.isScrolling,
52+
required this.velocity,
53+
required this.observer,
54+
required this.currentItemModel,
55+
});
56+
}

lib/src/utils/src/chat/chat_scroll_observer_typedefs.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
* @Date: 2022-10-31 14:57:45
55
*/
66

7+
import 'package:scrollview_observer/src/utils/src/chat/chat_scroll_observer_model.dart';
8+
9+
/// Customize the delta of the adjustPosition.
10+
typedef ChatScrollObserverCustomAdjustPositionDelta = double Function(
11+
ChatScrollObserverCustomAdjustPositionDeltaModel,
12+
);
13+
714
enum ChatScrollObserverHandlePositionType {
815
/// Nothing will be done.
916
none,

0 commit comments

Comments
 (0)