Skip to content

Commit e3cb790

Browse files
authored
FEAT: added example code for both paginations (#17)
1 parent b09494a commit e3cb790

File tree

7 files changed

+328
-84
lines changed

7 files changed

+328
-84
lines changed

example/android/app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ android {
4747
applicationId "com.example.example"
4848
// You can update the following values to match your application needs.
4949
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
50-
minSdkVersion flutter.minSdkVersion
50+
minSdkVersion 19
5151
targetSdkVersion flutter.targetSdkVersion
5252
versionCode flutterVersionCode.toInteger()
5353
versionName flutterVersionName
54+
multiDexEnabled true
5455
}
5556

5657
buildTypes {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Flutter Packages
2+
import 'package:flutter/material.dart';
3+
4+
// Firebase Packages
5+
import 'package:cloud_firestore/cloud_firestore.dart';
6+
7+
// Third Party Packages
8+
import 'package:firebase_pagination/firebase_pagination.dart';
9+
10+
class FirestorePaginationExample extends StatefulWidget {
11+
const FirestorePaginationExample({super.key});
12+
13+
@override
14+
State<FirestorePaginationExample> createState() =>
15+
_FirestorePaginationExampleState();
16+
}
17+
18+
class _FirestorePaginationExampleState
19+
extends State<FirestorePaginationExample> {
20+
final _textController = TextEditingController();
21+
22+
@override
23+
Widget build(BuildContext context) {
24+
return Scaffold(
25+
appBar: AppBar(
26+
title: const Text('Firestore Pagination Example'),
27+
),
28+
body: Column(
29+
children: [
30+
Expanded(
31+
child: FirestorePagination(
32+
query: FirebaseFirestore.instance
33+
.collection('messages')
34+
.orderBy('createdAt', descending: true),
35+
isLive: true,
36+
limit: 6,
37+
reverse: true,
38+
padding: const EdgeInsets.all(8.0),
39+
separatorBuilder: (context, index) => const Divider(),
40+
itemBuilder: (context, snapshot, index) {
41+
String? msg =
42+
(snapshot.data() as Map<String, dynamic>?)?['text'];
43+
44+
return ListTile(
45+
shape: RoundedRectangleBorder(
46+
borderRadius: BorderRadius.circular(8.0),
47+
),
48+
tileColor: msg == null ? Colors.red : Colors.green,
49+
title: Text(
50+
msg ?? 'No Message',
51+
style: TextStyle(color: msg == null ? Colors.white : null),
52+
),
53+
);
54+
},
55+
),
56+
),
57+
Container(
58+
color: Colors.white,
59+
padding: const EdgeInsets.all(8.0),
60+
child: Row(
61+
children: [
62+
Expanded(
63+
child: TextField(
64+
controller: _textController,
65+
decoration: const InputDecoration(
66+
hintText: 'Enter a message',
67+
),
68+
),
69+
),
70+
const SizedBox(width: 8.0),
71+
IconButton(
72+
icon: const Icon(Icons.send),
73+
onPressed: () {
74+
FirebaseFirestore.instance.collection('messages').add({
75+
'text': _textController.text,
76+
'createdAt': FieldValue.serverTimestamp(),
77+
});
78+
_textController.clear();
79+
},
80+
),
81+
],
82+
),
83+
)
84+
],
85+
),
86+
);
87+
}
88+
}

example/lib/main.dart

Lines changed: 46 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,80 @@
1+
// Flutter Packages
12
import 'package:flutter/material.dart';
23

3-
void main() {
4+
// Firebase Packages
5+
import 'package:firebase_core/firebase_core.dart';
6+
7+
// Views
8+
import 'firestore_pagination_example.dart';
9+
import 'realtimedb_pagination_example.dart';
10+
11+
Future<void> main() async {
12+
WidgetsFlutterBinding.ensureInitialized();
13+
14+
await Firebase.initializeApp(
15+
options: const FirebaseOptions(
16+
// ...
17+
),
18+
);
19+
420
runApp(const MyApp());
521
}
622

723
class MyApp extends StatelessWidget {
8-
const MyApp({Key? key}) : super(key: key);
24+
const MyApp({super.key});
925

10-
// This widget is the root of your application.
1126
@override
1227
Widget build(BuildContext context) {
1328
return MaterialApp(
14-
title: 'Flutter Demo',
29+
title: 'Firebase Pagination Example',
1530
theme: ThemeData(
16-
// This is the theme of your application.
17-
//
18-
// Try running your application with "flutter run". You'll see the
19-
// application has a blue toolbar. Then, without quitting the app, try
20-
// changing the primarySwatch below to Colors.green and then invoke
21-
// "hot reload" (press "r" in the console where you ran "flutter run",
22-
// or simply save your changes to "hot reload" in a Flutter IDE).
23-
// Notice that the counter didn't reset back to zero; the application
24-
// is not restarted.
2531
primarySwatch: Colors.blue,
32+
useMaterial3: true,
2633
),
27-
home: const MyHomePage(title: 'Flutter Demo Home Page'),
34+
home: const MyHomePage(),
2835
);
2936
}
3037
}
3138

32-
class MyHomePage extends StatefulWidget {
33-
const MyHomePage({Key? key, required this.title}) : super(key: key);
34-
35-
// This widget is the home page of your application. It is stateful, meaning
36-
// that it has a State object (defined below) that contains fields that affect
37-
// how it looks.
38-
39-
// This class is the configuration for the state. It holds the values (in this
40-
// case the title) provided by the parent (in this case the App widget) and
41-
// used by the build method of the State. Fields in a Widget subclass are
42-
// always marked "final".
43-
44-
final String title;
45-
46-
@override
47-
State<MyHomePage> createState() => _MyHomePageState();
48-
}
49-
50-
class _MyHomePageState extends State<MyHomePage> {
51-
int _counter = 0;
52-
53-
void _incrementCounter() {
54-
setState(() {
55-
// This call to setState tells the Flutter framework that something has
56-
// changed in this State, which causes it to rerun the build method below
57-
// so that the display can reflect the updated values. If we changed
58-
// _counter without calling setState(), then the build method would not be
59-
// called again, and so nothing would appear to happen.
60-
_counter++;
61-
});
62-
}
39+
class MyHomePage extends StatelessWidget {
40+
const MyHomePage({super.key});
6341

6442
@override
6543
Widget build(BuildContext context) {
66-
// This method is rerun every time setState is called, for instance as done
67-
// by the _incrementCounter method above.
68-
//
69-
// The Flutter framework has been optimized to make rerunning build methods
70-
// fast, so that you can just rebuild anything that needs updating rather
71-
// than having to individually change instances of widgets.
7244
return Scaffold(
7345
appBar: AppBar(
74-
// Here we take the value from the MyHomePage object that was created by
75-
// the App.build method, and use it to set our appbar title.
76-
title: Text(widget.title),
46+
title: const Text('Firebase Pagination Example'),
7747
),
7848
body: Center(
79-
// Center is a layout widget. It takes a single child and positions it
80-
// in the middle of the parent.
8149
child: Column(
82-
// Column is also a layout widget. It takes a list of children and
83-
// arranges them vertically. By default, it sizes itself to fit its
84-
// children horizontally, and tries to be as tall as its parent.
85-
//
86-
// Invoke "debug painting" (press "p" in the console, choose the
87-
// "Toggle Debug Paint" action from the Flutter Inspector in Android
88-
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
89-
// to see the wireframe for each widget.
90-
//
91-
// Column has various properties to control how it sizes itself and
92-
// how it positions its children. Here we use mainAxisAlignment to
93-
// center the children vertically; the main axis here is the vertical
94-
// axis because Columns are vertical (the cross axis would be
95-
// horizontal).
9650
mainAxisAlignment: MainAxisAlignment.center,
97-
children: <Widget>[
98-
const Text(
99-
'You have pushed the button this many times:',
51+
children: [
52+
ElevatedButton(
53+
onPressed: () {
54+
Navigator.push(
55+
context,
56+
MaterialPageRoute(
57+
builder: (context) => const FirestorePaginationExample(),
58+
),
59+
);
60+
},
61+
child: const Text('Firestore Pagination Example'),
10062
),
101-
Text(
102-
'$_counter',
103-
style: Theme.of(context).textTheme.headline4,
63+
const SizedBox(height: 20),
64+
ElevatedButton(
65+
onPressed: () {
66+
Navigator.push(
67+
context,
68+
MaterialPageRoute(
69+
builder: (context) => const RealtimeDBPaginationExample(),
70+
),
71+
);
72+
},
73+
child: const Text('RealtimeDB Pagination Example'),
10474
),
10575
],
10676
),
10777
),
108-
floatingActionButton: FloatingActionButton(
109-
onPressed: _incrementCounter,
110-
tooltip: 'Increment',
111-
child: const Icon(Icons.add),
112-
), // This trailing comma makes auto-formatting nicer for build methods.
11378
);
11479
}
11580
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Flutter Packages
2+
import 'package:flutter/material.dart';
3+
4+
// Firebase Packages
5+
import 'package:firebase_database/firebase_database.dart';
6+
7+
// Third Party Packages
8+
import 'package:firebase_pagination/firebase_pagination.dart';
9+
10+
class RealtimeDBPaginationExample extends StatefulWidget {
11+
const RealtimeDBPaginationExample({super.key});
12+
13+
@override
14+
State<RealtimeDBPaginationExample> createState() =>
15+
_RealtimeDBPaginationExampleState();
16+
}
17+
18+
class _RealtimeDBPaginationExampleState
19+
extends State<RealtimeDBPaginationExample> {
20+
final _textController = TextEditingController();
21+
22+
@override
23+
Widget build(BuildContext context) {
24+
return Scaffold(
25+
appBar: AppBar(
26+
title: const Text('Realtime DB Pagination Example'),
27+
),
28+
body: Column(
29+
children: [
30+
Expanded(
31+
child: RealtimeDBPagination(
32+
query: FirebaseDatabase.instance
33+
.ref('messages')
34+
.orderByChild('createdAt'),
35+
orderBy: 'createdAt',
36+
isLive: true,
37+
limit: 6,
38+
reverse: true,
39+
padding: const EdgeInsets.all(8.0),
40+
separatorBuilder: (context, index) => const Divider(),
41+
itemBuilder: (context, snapshot, index) {
42+
String? msg =
43+
(snapshot.value as Map<String, dynamic>?)?['text'];
44+
45+
return ListTile(
46+
shape: RoundedRectangleBorder(
47+
borderRadius: BorderRadius.circular(8.0),
48+
),
49+
tileColor: msg == null ? Colors.red : Colors.green,
50+
title: Text(
51+
msg ?? 'No Message',
52+
style: TextStyle(color: msg == null ? Colors.white : null),
53+
),
54+
);
55+
},
56+
),
57+
),
58+
Container(
59+
color: Colors.white,
60+
padding: const EdgeInsets.all(8.0),
61+
child: Row(
62+
children: [
63+
Expanded(
64+
child: TextField(
65+
controller: _textController,
66+
decoration: const InputDecoration(
67+
hintText: 'Enter a message',
68+
),
69+
),
70+
),
71+
const SizedBox(width: 8.0),
72+
IconButton(
73+
icon: const Icon(Icons.send),
74+
onPressed: () async {
75+
FirebaseDatabase.instance.ref('messages').push().set({
76+
'text': _textController.text,
77+
'createdAt': -DateTime.now().millisecondsSinceEpoch,
78+
});
79+
_textController.clear();
80+
},
81+
),
82+
],
83+
),
84+
),
85+
],
86+
),
87+
);
88+
}
89+
}

example/macos/Flutter/GeneratedPluginRegistrant.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
import FlutterMacOS
66
import Foundation
77

8+
import cloud_firestore
9+
import firebase_core
10+
import firebase_database
811

912
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
13+
FLTFirebaseFirestorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseFirestorePlugin"))
14+
FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin"))
15+
FLTFirebaseDatabasePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseDatabasePlugin"))
1016
}

0 commit comments

Comments
 (0)