2020-09-17 19:43:26 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
|
|
|
2020-09-17 19:49:42 +02:00
|
|
|
import '../hooks/ref.dart';
|
|
|
|
|
2020-09-18 23:24:58 +02:00
|
|
|
class InfiniteScrollController {
|
|
|
|
Function() clear;
|
|
|
|
|
|
|
|
InfiniteScrollController() {
|
|
|
|
usedBeforeCreation() => throw Exception(
|
|
|
|
'Tried to use $runtimeType before it being initialized');
|
|
|
|
|
|
|
|
clear = usedBeforeCreation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dispose() {
|
|
|
|
clear = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 19:05:00 +02:00
|
|
|
/// `ListView.builder` with asynchronous data fetching
|
2020-09-17 19:43:26 +02:00
|
|
|
class InfiniteScroll<T> extends HookWidget {
|
|
|
|
final int batchSize;
|
|
|
|
final Widget loadingWidget;
|
|
|
|
final Widget Function(T data) builder;
|
|
|
|
final Future<List<T>> Function(int page, int batchSize) fetchMore;
|
2020-09-18 23:24:58 +02:00
|
|
|
final InfiniteScrollController controller;
|
2020-09-28 19:55:10 +02:00
|
|
|
final Widget prepend;
|
|
|
|
final EdgeInsetsGeometry padding;
|
2020-09-17 19:43:26 +02:00
|
|
|
|
2021-01-03 18:21:56 +01:00
|
|
|
const InfiniteScroll({
|
2020-09-17 19:43:26 +02:00
|
|
|
this.batchSize = 10,
|
2020-09-29 21:45:18 +02:00
|
|
|
this.prepend = const SizedBox.shrink(),
|
2020-09-28 19:55:10 +02:00
|
|
|
this.padding,
|
2020-09-17 19:43:26 +02:00
|
|
|
this.loadingWidget =
|
|
|
|
const ListTile(title: Center(child: CircularProgressIndicator())),
|
2020-09-18 23:29:38 +02:00
|
|
|
@required this.builder,
|
|
|
|
@required this.fetchMore,
|
2020-09-18 23:24:58 +02:00
|
|
|
this.controller,
|
2020-09-17 19:43:26 +02:00
|
|
|
}) : assert(builder != null),
|
2020-09-18 23:24:58 +02:00
|
|
|
assert(fetchMore != null),
|
|
|
|
assert(batchSize > 0);
|
2020-09-17 19:43:26 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final data = useState<List<T>>([]);
|
2020-09-17 19:49:42 +02:00
|
|
|
final hasMore = useRef(true);
|
|
|
|
final isFetching = useRef(false);
|
2020-09-17 19:43:26 +02:00
|
|
|
|
2020-09-18 23:24:58 +02:00
|
|
|
useEffect(() {
|
|
|
|
if (controller != null) {
|
2020-10-04 21:51:26 +02:00
|
|
|
controller.clear = () {
|
|
|
|
data.value = [];
|
|
|
|
hasMore.current = true;
|
|
|
|
};
|
2020-09-18 23:24:58 +02:00
|
|
|
return controller.dispose;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
final page = data.value.length ~/ batchSize + 1;
|
|
|
|
|
2020-09-17 19:43:26 +02:00
|
|
|
return ListView.builder(
|
2020-09-28 19:55:10 +02:00
|
|
|
padding: padding,
|
|
|
|
// +2 for the loading widget and prepend widget
|
|
|
|
itemCount: data.value.length + 2,
|
2020-09-17 19:43:26 +02:00
|
|
|
itemBuilder: (_, i) {
|
2020-09-28 19:55:10 +02:00
|
|
|
if (i == 0) {
|
2020-09-29 21:34:05 +02:00
|
|
|
return prepend;
|
2020-09-28 19:55:10 +02:00
|
|
|
}
|
|
|
|
i -= 1;
|
|
|
|
|
2020-09-17 19:43:26 +02:00
|
|
|
// reached the bottom, fetch more
|
|
|
|
if (i == data.value.length) {
|
|
|
|
// if there are no more, skip
|
2020-09-17 19:49:42 +02:00
|
|
|
if (!hasMore.current) {
|
2020-09-29 21:45:18 +02:00
|
|
|
return SizedBox.shrink();
|
2020-09-17 19:43:26 +02:00
|
|
|
}
|
|
|
|
|
2020-09-17 19:49:42 +02:00
|
|
|
// if it's already fetching more, skip
|
|
|
|
if (!isFetching.current) {
|
|
|
|
isFetching.current = true;
|
2020-09-18 23:24:58 +02:00
|
|
|
fetchMore(page, batchSize).then((newData) {
|
2020-09-17 19:49:42 +02:00
|
|
|
// if got less than the batchSize, mark the list as done
|
2020-09-18 23:24:58 +02:00
|
|
|
if (newData.length < batchSize) {
|
2020-09-17 19:49:42 +02:00
|
|
|
hasMore.current = false;
|
|
|
|
}
|
2020-09-18 23:31:54 +02:00
|
|
|
// append new data
|
2020-09-18 23:24:58 +02:00
|
|
|
data.value = [...data.value, ...newData];
|
2020-09-17 19:49:42 +02:00
|
|
|
}).whenComplete(() => isFetching.current = false);
|
|
|
|
}
|
2020-09-17 19:43:26 +02:00
|
|
|
|
|
|
|
return loadingWidget;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not last element, render list item
|
|
|
|
return builder(data.value[i]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|