Skip to content

Instantly share code, notes, and snippets.

@shaon2016
Created June 19, 2021 16:15
Show Gist options
  • Save shaon2016/f41a8b41797cc5b8b47938c5ccd33651 to your computer and use it in GitHub Desktop.
Save shaon2016/f41a8b41797cc5b8b47938c5ccd33651 to your computer and use it in GitHub Desktop.
Example to show pagination using GetX
class HomeController extends BaseController with StateMixin, ScrollMixin {
final donors = <BloodDonors>[].obs;
var page = 1;
var isToLoadMore = true;
@override
onInit() {
super.onInit();
loadData();
}
loadData() async {
final map = Map<String, dynamic>();
map['page'] = page;
try {
final result = await restClient.request("blood-finder", Method.GET, map);
if (result != null) {
if (result is d.Response) {
var data = BloodDonor.fromJson(result.data).data;
if (data != null) {
donors.addAll(data.data);
isToLoadMore = true;
change(donors, status: RxStatus.success());
} else {
isToLoadMore = false;
}
}
} else {
isToLoadMore = false;
}
} on Exception catch (e) {
Get.showSnackbar(GetBar(
message: "$e",
duration: Duration(milliseconds: 3000),
));
}
}
@override
Future<void> onEndScroll() async {
if (isToLoadMore) {
page++;
await loadData();
}
print("onEngScroll: Called");
}
@override
Future<void> onTopScroll() async {
print("onTopScroll: Called");
}
}
class HomePage extends GetView<HomeController> {
static final routeName = "/home";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Clean Architecture"),
),
body: controller.obx((state) => ListView.builder(
controller: controller.scroll,
itemBuilder: (ctx, index) {
if (controller.isToLoadMore &&
controller.donors.length - 1 == index)
return Center(
child: CircularProgressIndicator(),
);
return ListTile(
title: Text(controller.donors[index].name),
subtitle: Text(controller.donors[index].designation),
);
},
itemCount: controller.donors.length,
)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment