small fixups
This commit is contained in:
parent
856072e0b0
commit
09904536e7
2 changed files with 33 additions and 43 deletions
|
|
@ -16,7 +16,7 @@ final class ChatScroll<T>({
|
|||
static ChatScroll<T> use<T>({
|
||||
required AsyncValue<IList<T>?> controllerData,
|
||||
required String Function(T item) id,
|
||||
required Future<bool> Function() loadOlder,
|
||||
required Future<void> Function() loadOlder,
|
||||
required Future<void> Function() onReachedBottom,
|
||||
}) {
|
||||
final historyListController = useRef(ListController());
|
||||
|
|
@ -59,35 +59,25 @@ final class ChatScroll<T>({
|
|||
);
|
||||
}, [controllerData, anchorIdValue]);
|
||||
|
||||
Future<void> loadOlderItems() async {
|
||||
if (controllerData.isLoading) return;
|
||||
|
||||
await loadOlder();
|
||||
}
|
||||
|
||||
useEffect(() {
|
||||
const loadThreshold = 500.0;
|
||||
const topThreshold = 500.0;
|
||||
const bottomThreshold = 50.0;
|
||||
|
||||
void checkPosition() {
|
||||
Future<void> checkPosition() async {
|
||||
if (!scrollController.hasClients) return;
|
||||
|
||||
final position = scrollController.position;
|
||||
|
||||
if (position.extentAfter <= loadThreshold) {
|
||||
loadOlderItems();
|
||||
}
|
||||
|
||||
if (position.extentBefore <= bottomThreshold) {
|
||||
if (position.extentAfter <= topThreshold) {
|
||||
await loadOlder();
|
||||
} else if (position.extentBefore <= bottomThreshold) {
|
||||
onReachedBottom();
|
||||
}
|
||||
}
|
||||
|
||||
scrollController.addListener(checkPosition);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
checkPosition();
|
||||
});
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => checkPosition());
|
||||
|
||||
return () {
|
||||
scrollController.removeListener(checkPosition);
|
||||
|
|
|
|||
Loading…
Reference in a new issue