|
| 1 | +## Examples / Usages |
| 2 | + |
| 3 | +> **Note**: This file only contains examples for Firestore Database. |
| 4 | +> But all of this can be implemented for Firebase Realtime Database using the |
| 5 | +> RealtimeDBPagination widget with only the `query` and `itemBuilder` params |
| 6 | +> differing from FirestorePagination. |
| 7 | +
|
| 8 | +#### For a simple list view pagination (with a custom bottom loader) |
| 9 | + |
| 10 | +```dart |
| 11 | +FirestorePagination( |
| 12 | + limit: 420, // Defaults to 10. |
| 13 | + viewType: ViewType.list, |
| 14 | + bottomLoader: const Center( |
| 15 | + child: CircularProgressIndicator( |
| 16 | + strokeWidth: 3, |
| 17 | + color: Colors.blue, |
| 18 | + ), |
| 19 | + ), |
| 20 | + query: FirebaseFirestore.instance |
| 21 | + .collection('scores') |
| 22 | + .orderBy('score', descending: true), |
| 23 | + itemBuilder: (context, documentSnapshot, index) { |
| 24 | + final data = documentSnapshot.data() as Map<String, dynamic>?; |
| 25 | + if (data == null) return Container(); |
| 26 | +
|
| 27 | + return Container( |
| 28 | + child: RecordTile( |
| 29 | + name: data['name'], |
| 30 | + score: data['score'], |
| 31 | + ), |
| 32 | + ); |
| 33 | + }, |
| 34 | +), |
| 35 | +``` |
| 36 | + |
| 37 | +<hr /> |
| 38 | + |
| 39 | +#### For live chat-like application with pagination (with separator between messages) |
| 40 | + |
| 41 | +```dart |
| 42 | +FirestorePagination( |
| 43 | + limit: 69, // Defaults to 10. |
| 44 | + isLive: true, // Defaults to false. |
| 45 | + viewType: ViewType.list, |
| 46 | + reverse: true, |
| 47 | + query: FirebaseFirestore.instance |
| 48 | + .collection('chats') |
| 49 | + .orderBy('createdAt', descending: true), |
| 50 | + itemBuilder: (context, documentSnapshot, index) { |
| 51 | + final data = documentSnapshot.data() as Map<String, dynamic>?; |
| 52 | + if (data == null) return Container(); |
| 53 | +
|
| 54 | + return MessageTile( |
| 55 | + senderName: data['senderName'], |
| 56 | + senderImageUrl: data['senderImageUrl'], |
| 57 | + message: data['message'], |
| 58 | + createdAt: data['createdAt'], |
| 59 | + ); |
| 60 | + }, |
| 61 | + separatorBuilder: (context, index) { |
| 62 | + return const Divider( |
| 63 | + height: 5, |
| 64 | + thickness: 1, |
| 65 | + ); |
| 66 | + }, |
| 67 | +), |
| 68 | +``` |
| 69 | + |
| 70 | +<hr /> |
| 71 | + |
| 72 | +#### For grid view with pagination (with custom no data view) |
| 73 | + |
| 74 | +```dart |
| 75 | +FirestorePagination( |
| 76 | + limit: 14, // Defaults to 10. |
| 77 | + viewType: ViewType.grid, |
| 78 | + onEmpty: const Center( |
| 79 | + child: Text('Cart is empty'), |
| 80 | + ), |
| 81 | + query: FirebaseFirestore.instance |
| 82 | + .collection('cart') |
| 83 | + .orderBy('price'), |
| 84 | + itemBuilder: (context, documentSnapshot, index) { |
| 85 | + final data = documentSnapshot.data() as Map<String, dynamic>?; |
| 86 | + if (data == null) return Container(); |
| 87 | +
|
| 88 | + return GridTile( |
| 89 | + itemName: data['itemName'], |
| 90 | + itemImageUrl: data['itemImageUrl'], |
| 91 | + price: data['price'], |
| 92 | + ); |
| 93 | + }, |
| 94 | + gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( |
| 95 | + crossAxisCount: 3, |
| 96 | + mainAxisSpacing: 8.0, |
| 97 | + crossAxisSpacing: 8.0, |
| 98 | + ), |
| 99 | +), |
| 100 | +``` |
| 101 | + |
| 102 | +<hr /> |
| 103 | + |
| 104 | +#### For Scrollable Wrap with pagination (with custom scrollDirection) |
| 105 | + |
| 106 | +```dart |
| 107 | +FirestorePagination( |
| 108 | + limit: 12, // Defaults to 10. |
| 109 | + viewType: ViewType.wrap, |
| 110 | + scrollDirection: Axis.horizontal, // Defaults to Axis.vertical. |
| 111 | + query: FirebaseFirestore.instance |
| 112 | + .collection('categories') |
| 113 | + .orderBy('popularity', descending: true), |
| 114 | + itemBuilder: (context, documentSnapshot, index) { |
| 115 | + final data = documentSnapshot.data() as Map<String, dynamic>?; |
| 116 | + if (data == null) return Container(); |
| 117 | +
|
| 118 | + return Container( |
| 119 | + constraints: const BoxConstraints(maxWidth: 169), |
| 120 | + child: CategoryTile( |
| 121 | + categoryName: data['categoryName'], |
| 122 | + categoryImageUrl: data['categoryImageUrl'], |
| 123 | + ), |
| 124 | + ); |
| 125 | + }, |
| 126 | + wrapOptions: const WrapOptions( |
| 127 | + alignment: WrapAlignment.start, |
| 128 | + direction: Axis.vertical, |
| 129 | + runSpacing: 10.0, |
| 130 | + ), |
| 131 | +), |
| 132 | +``` |
0 commit comments