[LazyLoadScrollView] : Add support of onStartOfPage
Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
@@ -1,18 +1,18 @@
|
|||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
|
|
||||||
enum LoadingStatus { LOADING, STABLE }
|
enum _LoadingStatus { LOADING, STABLE }
|
||||||
|
|
||||||
/// Signature for EndOfPageListeners
|
/// A widget that wraps a [Widget] and will trigger [onEndOfPage]/[onStartOfPage] when it
|
||||||
typedef EndOfPageListenerCallback = void Function();
|
/// reaches the bottom/start of the list
|
||||||
|
|
||||||
/// A widget that wraps a [Widget] and will trigger [onEndOfPage] when it
|
|
||||||
/// reaches the bottom of the list
|
|
||||||
class LazyLoadScrollView extends StatefulWidget {
|
class LazyLoadScrollView extends StatefulWidget {
|
||||||
/// The [Widget] that this widget watches for changes on
|
/// The [Widget] that this widget watches for changes on
|
||||||
final Widget child;
|
final Widget child;
|
||||||
|
|
||||||
|
/// Called when the [child] reaches the start of the list
|
||||||
|
final VoidCallback onStartOfPage;
|
||||||
|
|
||||||
/// Called when the [child] reaches the end of the list
|
/// Called when the [child] reaches the end of the list
|
||||||
final EndOfPageListenerCallback onEndOfPage;
|
final VoidCallback onEndOfPage;
|
||||||
|
|
||||||
/// The offset to take into account when triggering [onEndOfPage] in pixels
|
/// The offset to take into account when triggering [onEndOfPage] in pixels
|
||||||
final int scrollOffset;
|
final int scrollOffset;
|
||||||
@@ -20,14 +20,15 @@ class LazyLoadScrollView extends StatefulWidget {
|
|||||||
/// Used to determine if loading of new data has finished. You should use set this if you aren't using a FutureBuilder or StreamBuilder
|
/// Used to determine if loading of new data has finished. You should use set this if you aren't using a FutureBuilder or StreamBuilder
|
||||||
final bool isLoading;
|
final bool isLoading;
|
||||||
|
|
||||||
|
/// Initiates a LazyLoadScrollView widget
|
||||||
LazyLoadScrollView({
|
LazyLoadScrollView({
|
||||||
Key key,
|
Key key,
|
||||||
@required this.child,
|
@required this.child,
|
||||||
@required this.onEndOfPage,
|
this.onStartOfPage,
|
||||||
|
this.onEndOfPage,
|
||||||
this.isLoading = false,
|
this.isLoading = false,
|
||||||
this.scrollOffset = 100,
|
this.scrollOffset = 100,
|
||||||
}) : assert(onEndOfPage != null),
|
}) : assert(child != null),
|
||||||
assert(child != null),
|
|
||||||
super(key: key);
|
super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -35,13 +36,13 @@ class LazyLoadScrollView extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _LazyLoadScrollViewState extends State<LazyLoadScrollView> {
|
class _LazyLoadScrollViewState extends State<LazyLoadScrollView> {
|
||||||
LoadingStatus _loadMoreStatus = LoadingStatus.STABLE;
|
_LoadingStatus _loadMoreStatus = _LoadingStatus.STABLE;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void didUpdateWidget(LazyLoadScrollView oldWidget) {
|
void didUpdateWidget(LazyLoadScrollView oldWidget) {
|
||||||
super.didUpdateWidget(oldWidget);
|
super.didUpdateWidget(oldWidget);
|
||||||
if (!widget.isLoading) {
|
if (!widget.isLoading) {
|
||||||
_loadMoreStatus = LoadingStatus.STABLE;
|
_loadMoreStatus = _LoadingStatus.STABLE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,21 +60,37 @@ class _LazyLoadScrollViewState extends State<LazyLoadScrollView> {
|
|||||||
notification.metrics.maxScrollExtent - notification.metrics.pixels <=
|
notification.metrics.maxScrollExtent - notification.metrics.pixels <=
|
||||||
widget.scrollOffset) {
|
widget.scrollOffset) {
|
||||||
if (_loadMoreStatus != null &&
|
if (_loadMoreStatus != null &&
|
||||||
_loadMoreStatus == LoadingStatus.STABLE) {
|
_loadMoreStatus == _LoadingStatus.STABLE) {
|
||||||
_loadMoreStatus = LoadingStatus.LOADING;
|
_loadMoreStatus = _LoadingStatus.LOADING;
|
||||||
widget.onEndOfPage();
|
widget.onEndOfPage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (notification.metrics.minScrollExtent < notification.metrics.pixels &&
|
||||||
|
notification.metrics.pixels - notification.metrics.minScrollExtent <=
|
||||||
|
widget.scrollOffset) {
|
||||||
|
if (_loadMoreStatus != null &&
|
||||||
|
_loadMoreStatus == _LoadingStatus.STABLE) {
|
||||||
|
_loadMoreStatus = _LoadingStatus.LOADING;
|
||||||
|
widget.onStartOfPage();
|
||||||
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (notification is OverscrollNotification) {
|
if (notification is OverscrollNotification) {
|
||||||
if (notification.overscroll > 0) {
|
if (notification.overscroll > 0) {
|
||||||
if (_loadMoreStatus != null &&
|
if (_loadMoreStatus != null &&
|
||||||
_loadMoreStatus == LoadingStatus.STABLE) {
|
_loadMoreStatus == _LoadingStatus.STABLE) {
|
||||||
_loadMoreStatus = LoadingStatus.LOADING;
|
_loadMoreStatus = _LoadingStatus.LOADING;
|
||||||
widget.onEndOfPage();
|
widget.onEndOfPage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (notification.overscroll < 0) {
|
||||||
|
if (_loadMoreStatus != null &&
|
||||||
|
_loadMoreStatus == _LoadingStatus.STABLE) {
|
||||||
|
_loadMoreStatus = _LoadingStatus.LOADING;
|
||||||
|
widget.onStartOfPage();
|
||||||
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user