Skip to content

Instantly share code, notes, and snippets.

@TobiCrackIT
Last active August 30, 2021 12:16
Show Gist options
  • Save TobiCrackIT/be906622a97d836b0d55a31744a25628 to your computer and use it in GitHub Desktop.
Save TobiCrackIT/be906622a97d836b0d55a31744a25628 to your computer and use it in GitHub Desktop.
Pagination in Flutter App without using a package
import 'package:flutter/material.dart';
class PaginatedScreen extends StatefulWidget {
@override
_PaginatedScreenState createState() => _PaginatedScreenState();
}
class _PaginatedScreenState extends State<PaginatedScreen> {
int page = 1;
List<String> items = ['item 1', 'item 2','item 3', 'item 4','item 5', 'item 6','item 7', 'item 8','item 9', 'item 10', ];
bool isLoading = false;
Future _loadData() async {
// perform fetching data delay
await new Future.delayed(new Duration(seconds: 1));
print("loading more data");
setState(() {
items.addAll( ['item 11', 'item 12','item 13', 'item 14','item 15', 'item 16','item 17', 'item 18','item 19', 'item 20', ]);
isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Sample',style: TextStyle(color: Colors.black),),
),
body: Column(
children: <Widget>[
Expanded(
child: NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification scrollInfo) {
if (!isLoading && scrollInfo.metrics.pixels ==
scrollInfo.metrics.maxScrollExtent) {
_loadData();
// start loading data
setState(() {
isLoading = true;
});
}
return true;
},
child: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${items[index]}',style: TextStyle(color: Colors.black,fontSize: 14),),
);
},
),
),
),
Visibility(
visible: isLoading,
child: Card(
shape: CircleBorder(),
child: Container(
padding: EdgeInsets.all(5),
child: Center(
child: SizedBox(
child: CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation<Color>(
Colors.lightGreen,
),
strokeWidth: 2.0,
backgroundColor:
Colors.transparent,
),
height: 20,
width: 20,
),
),
),
),
)
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment