Appwrite Pagination
Provides paginatation functionality on appwrite collections.
- Option to cache or paginate documents of a collection.
- Sort documents by
orderType
andorderAttributes
. - Provides collection start and end indicators.
- Comes with a dedicated
Widget
to allow for finer control over paginated collections.
Pagination
Provides the bare minimum for pagination functionality of an appwrite collection. This object can be used with any widget for less restrictive usage otherwise, using a PaginationWidget
can provide the typical functionality required for pagination such as start/end callbacks and its own scroll controller with managed state.
Usage
final pagination = Pagination(
cached: true,
documentsPerPage: 2,
collectionId: 'messages',
orderAttributes: ['text'],
orderTypes: ['DESC'],
database: Databases(client, databaseId: 'main'),
onStartCallback: (documents) => print('Start'),
onEndCallback: (documents) => print('End'),
);
PaginationWidget
A complimentary widget to be used with a Pagination
instance. Provides its child widget access to its internal scroll controller with options to specify callbacks when reaching the start/end of a collection.
Usage
PaginationWidget(
pagination: pagination,
child: (controller) => ListView.builder(
itemCount: pagination.documentList.length,
shrinkWrap: true,
itemBuilder: (context, index) =>
Text(pagination.documentList[index].data['text']),
),
),
Full Example
import 'package:appwrite/appwrite.dart';
import 'package:flutter/material.dart';
import 'package:appwrite_pagination_flutter/appwrite_pagination_flutter.dart';
main() => runApp(const App());
final client = Client(endPoint: 'http://localhost/v1')..setProject('main');
final pages = Pagination(
// cached: false,
itemsPerPage: 1,
collectionId: 'messages',
orderAttributes: ['text'],
orderTypes: ['ASC'],
database: Databases(client, databaseId: 'main'),
);
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(home: Home());
}
}
class Home extends StatefulWidget {
const Home({super.key});
@override
HomeState createState() => HomeState();
}
class HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Column(
children: [
ListView.builder(
shrinkWrap: true,
itemCount: pages.documentList.length,
itemBuilder: (context, index) =>
Text(pages.documentList[index].data['text']),
),
Text('atStart: ${pages.atStart}'),
Text('atEnd: ${pages.atEnd}'),
ElevatedButton(
onPressed: () async {
await pages.get('before');
setState(() {});
},
child: const Text('Before')),
ElevatedButton(
onPressed: () async {
await pages.get('after');
setState(() {});
},
child: const Text('After')),
ElevatedButton(
onPressed: () async {
pages.clear();
setState(() {});
},
child: const Text('Clear')),
ElevatedButton(
onPressed: () async {
pages.sort(orderAttributes: ['text'], orderTypes: ['DESC']);
setState(() {});
},
child: const Text('Sort DESC')),
ElevatedButton(
onPressed: () async {
pages.sort(orderAttributes: ['text'], orderTypes: ['ASC']);
setState(() {});
},
child: const Text('Sort ASC')),
],
),
);
}
}