add pagination for pages
This commit is contained in:
@@ -9,53 +9,47 @@ import 'package:fcs/helpers/firebase_helper.dart';
|
||||
import 'package:fcs/pages/main/model/base_model.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
import '../../../pagination/paginator_listener.dart';
|
||||
|
||||
class StaffModel extends BaseModel {
|
||||
final log = Logger('StaffModel');
|
||||
StreamSubscription<QuerySnapshot>? listener;
|
||||
StreamSubscription<QuerySnapshot>? privilegeListener;
|
||||
PaginatorListener<User>? getStaffs;
|
||||
|
||||
List<User> employees = [];
|
||||
StreamSubscription<QuerySnapshot>? privilegeListener;
|
||||
List<Privilege> privileges = [];
|
||||
|
||||
@override
|
||||
void privilegeChanged() {
|
||||
super.privilegeChanged();
|
||||
_loadPrivileges();
|
||||
_loadEmployees();
|
||||
}
|
||||
|
||||
@override
|
||||
logout() async {
|
||||
if (listener != null) listener!.cancel();
|
||||
getStaffs?.close();
|
||||
if (privilegeListener != null) privilegeListener!.cancel();
|
||||
employees = [];
|
||||
privileges = [];
|
||||
}
|
||||
|
||||
Future<void> _loadEmployees() async {
|
||||
loadPaginationStaffs() {
|
||||
if (user == null || !user!.hasStaffs()) return;
|
||||
|
||||
try {
|
||||
if (listener != null) listener!.cancel();
|
||||
String path = "/$user_collection";
|
||||
Query col = FirebaseFirestore.instance
|
||||
.collection(path)
|
||||
.where("is_employee", isEqualTo: true)
|
||||
.where("is_sys_admin", isEqualTo: false);
|
||||
|
||||
listener = FirebaseFirestore.instance
|
||||
.collection("/$user_collection")
|
||||
.where("is_employee", isEqualTo: true)
|
||||
.where("is_sys_admin", isEqualTo: false)
|
||||
.snapshots()
|
||||
.listen((QuerySnapshot snapshot) {
|
||||
employees.clear();
|
||||
employees = snapshot.docs.map((documentSnapshot) {
|
||||
var user = User.fromMap(
|
||||
documentSnapshot.data() as Map<String, dynamic>,
|
||||
documentSnapshot.id);
|
||||
return user;
|
||||
}).toList();
|
||||
notifyListeners();
|
||||
});
|
||||
} catch (e) {
|
||||
log.warning("Error!! $e");
|
||||
}
|
||||
Query pageQuery = FirebaseFirestore.instance
|
||||
.collection(path)
|
||||
.where("is_employee", isEqualTo: true)
|
||||
.where("is_sys_admin", isEqualTo: false)
|
||||
.orderBy("update_time", descending: true);
|
||||
|
||||
getStaffs?.close();
|
||||
getStaffs = PaginatorListener<User>(
|
||||
col, pageQuery, (data, id) => User.fromMap(data, id),
|
||||
rowPerLoad: 30);
|
||||
}
|
||||
|
||||
Future<void> _loadPrivileges() async {
|
||||
|
||||
@@ -9,6 +9,7 @@ import 'package:flutter_vector_icons/flutter_vector_icons.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../pagination/paginator_listview.dart';
|
||||
import 'staff_editor.dart';
|
||||
|
||||
class StaffList extends StatefulWidget {
|
||||
@@ -21,6 +22,12 @@ class _StaffListState extends State<StaffList> {
|
||||
final double dotSize = 15.0;
|
||||
bool _isLoading = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
context.read<StaffModel>().loadPaginationStaffs();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
StaffModel staffModel = Provider.of<StaffModel>(context);
|
||||
@@ -28,42 +35,33 @@ class _StaffListState extends State<StaffList> {
|
||||
return LocalProgress(
|
||||
inAsyncCall: _isLoading,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
leading: new IconButton(
|
||||
icon: new Icon(CupertinoIcons.back),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
leading: new IconButton(
|
||||
icon: new Icon(CupertinoIcons.back),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
),
|
||||
backgroundColor: primaryColor,
|
||||
title: LocalText(
|
||||
context,
|
||||
'staff.list.title',
|
||||
color: Colors.white,
|
||||
fontSize: 20,
|
||||
),
|
||||
),
|
||||
backgroundColor: primaryColor,
|
||||
title: LocalText(
|
||||
context,
|
||||
'staff.list.title',
|
||||
color: Colors.white,
|
||||
fontSize: 20,
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
onPressed: () {
|
||||
Navigator.of(context).push(
|
||||
CupertinoPageRoute(builder: (context) => StaffEditor()));
|
||||
},
|
||||
icon: Icon(Icons.add),
|
||||
label: LocalText(context, "staff.new", color: Colors.white),
|
||||
backgroundColor: primaryColor,
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
onPressed: () {
|
||||
Navigator.of(context)
|
||||
.push(CupertinoPageRoute(builder: (context) => StaffEditor()));
|
||||
},
|
||||
icon: Icon(Icons.add),
|
||||
label: LocalText(context, "staff.new", color: Colors.white),
|
||||
backgroundColor: primaryColor,
|
||||
),
|
||||
body: new ListView.separated(
|
||||
separatorBuilder: (context, index) => Divider(
|
||||
color: Colors.black,
|
||||
height: 1,
|
||||
),
|
||||
scrollDirection: Axis.vertical,
|
||||
shrinkWrap: true,
|
||||
itemCount: staffModel.employees.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
User user = staffModel.employees[index];
|
||||
return _item(user);
|
||||
}),
|
||||
),
|
||||
body: PaginatorListView<User>(
|
||||
paginatorListener: staffModel.getStaffs!,
|
||||
rowBuilder: (p) => _item(p),
|
||||
color: primaryColor)),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user