Jonatas Borges

add ScrollMixin and isLoadingMore to controller status

... ... @@ -152,51 +152,36 @@ class RxStatus {
final bool isError;
final bool isSuccess;
final bool isEmpty;
final bool isLoadingMore;
final String errorMessage;
RxStatus._({
this.isEmpty,
this.isLoading,
this.isError,
this.isSuccess,
this.isEmpty = false,
this.isLoading = false,
this.isError = false,
this.isSuccess = false,
this.errorMessage,
this.isLoadingMore = false,
});
factory RxStatus.loading() {
return RxStatus._(
isLoading: true,
isError: false,
isSuccess: false,
isEmpty: false,
);
return RxStatus._(isLoading: true);
}
factory RxStatus.loadingMore() {
return RxStatus._(isSuccess: true, isLoadingMore: true);
}
factory RxStatus.success() {
return RxStatus._(
isLoading: false,
isError: false,
isSuccess: true,
isEmpty: false,
);
return RxStatus._(isSuccess: true);
}
factory RxStatus.error([String message]) {
return RxStatus._(
isLoading: false,
isError: true,
isSuccess: false,
isEmpty: false,
errorMessage: message,
);
return RxStatus._(isError: true, errorMessage: message);
}
factory RxStatus.empty() {
return RxStatus._(
isLoading: false,
isError: false,
isSuccess: false,
isEmpty: true,
);
return RxStatus._(isEmpty: true);
}
}
... ...
// ignore: prefer_mixin
import 'package:flutter/widgets.dart';
import '../../../instance_manager.dart';
import '../rx_flutter/rx_disposable.dart';
import '../rx_flutter/rx_notifier.dart';
... ... @@ -26,6 +27,50 @@ abstract class GetxController extends DisposableInterface with ListNotifier {
}
}
mixin ScrollMixin on GetLifeCycleBase {
final ScrollController scroll = ScrollController();
@override
void onInit() {
super.onInit();
scroll.addListener(_listener);
}
bool _canFetchBottom = true;
bool _canFetchTop = true;
void _listener() {
if (scroll.position.atEdge) {
_checkIfCanLoadMore();
}
}
Future<void> _checkIfCanLoadMore() async {
if (scroll.position.pixels == 0) {
if (!_canFetchTop) return;
_canFetchTop = false;
await onTopScroll();
_canFetchTop = true;
} else {
if (!_canFetchBottom) return;
_canFetchBottom = false;
await onEndScroll();
_canFetchBottom = true;
}
}
Future<void> onEndScroll();
Future<void> onTopScroll();
@override
void onClose() {
scroll.removeListener(_listener);
super.onClose();
}
}
abstract class RxController extends DisposableInterface {}
abstract class SuperController<T> extends FullLifeCycleController
... ...