add paginator
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import 'package:fcs/helpers/paginator.dart';
|
||||
import 'package:fcs/helpers/theme.dart';
|
||||
import 'package:fcs/localization/app_translations.dart';
|
||||
import 'package:fcs/pages/fcs_shipment/model/fcs_shipment_model.dart';
|
||||
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
|
||||
import 'package:fcs/pages/widgets/local_popup_menu_button.dart';
|
||||
import 'package:fcs/pages/widgets/local_popupmenu.dart';
|
||||
import 'package:fcs/pages/widgets/local_text.dart';
|
||||
import 'package:fcs/pages/widgets/progress.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'fcs_shipment_editor.dart';
|
||||
import 'fcs_shipment_list_row.dart';
|
||||
@@ -19,21 +19,39 @@ class FcsShipmentList extends StatefulWidget {
|
||||
|
||||
class _FcsShipmentListState extends State<FcsShipmentList> {
|
||||
bool _isLoading = false;
|
||||
var _controller = ScrollController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
_controller.addListener(() async {
|
||||
if (_controller.position.pixels == _controller.position.maxScrollExtent) {
|
||||
Provider.of<FcsShipmentModel>(context, listen: false).loadMore();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var shipmentModel = Provider.of<FcsShipmentModel>(context);
|
||||
|
||||
final popupMenu = LocalPopupMenuButton(
|
||||
popmenus: [
|
||||
LocalPopupMenu(
|
||||
id: 1,
|
||||
textKey: "package.popupmenu.active",
|
||||
selected: shipmentModel.selectedIndex == 1),
|
||||
LocalPopupMenu(
|
||||
id: 2,
|
||||
textKey: "package.popupmenu.delivered",
|
||||
selected: shipmentModel.selectedIndex == 2)
|
||||
],
|
||||
popupMenuCallback: (p) => this.setState(() {
|
||||
shipmentModel.selectedIndex = p.id;
|
||||
}),
|
||||
);
|
||||
|
||||
return LocalProgress(
|
||||
inAsyncCall: _isLoading,
|
||||
child: Scaffold(
|
||||
@@ -46,6 +64,7 @@ class _FcsShipmentListState extends State<FcsShipmentList> {
|
||||
backgroundColor: primaryColor,
|
||||
title: LocalText(context, 'FCSshipment.list.title',
|
||||
color: Colors.white, fontSize: 20),
|
||||
actions: [popupMenu],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
onPressed: () {
|
||||
@@ -58,20 +77,39 @@ class _FcsShipmentListState extends State<FcsShipmentList> {
|
||||
body: Column(
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: new ListView.separated(
|
||||
separatorBuilder: (context, index) => Divider(
|
||||
color: Colors.black,
|
||||
),
|
||||
scrollDirection: Axis.vertical,
|
||||
padding: EdgeInsets.only(top: 15),
|
||||
shrinkWrap: true,
|
||||
itemCount: shipmentModel.fcsShipments.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return FcsShipmentListRow(
|
||||
key: ValueKey(shipmentModel.fcsShipments[index].id),
|
||||
shipment: shipmentModel.fcsShipments[index]);
|
||||
}),
|
||||
child: RefreshIndicator(
|
||||
child: ListView.separated(
|
||||
physics: AlwaysScrollableScrollPhysics(),
|
||||
controller: _controller,
|
||||
separatorBuilder: (context, index) => Divider(
|
||||
color: Colors.black,
|
||||
),
|
||||
scrollDirection: Axis.vertical,
|
||||
padding: EdgeInsets.only(top: 15),
|
||||
shrinkWrap: true,
|
||||
itemCount: shipmentModel.fcsShipments.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return FcsShipmentListRow(
|
||||
key: ValueKey(
|
||||
shipmentModel.fcsShipments[index].id),
|
||||
shipment: shipmentModel.fcsShipments[index]);
|
||||
}),
|
||||
onRefresh: () => shipmentModel.refresh(),
|
||||
),
|
||||
),
|
||||
shipmentModel.isLoading
|
||||
? Container(
|
||||
padding: EdgeInsets.all(8),
|
||||
color: primaryColor,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text("Loading...",
|
||||
style: TextStyle(color: Colors.white)),
|
||||
],
|
||||
),
|
||||
)
|
||||
: Container(),
|
||||
],
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:fcs/data/services/services.dart';
|
||||
import 'package:fcs/domain/constants.dart';
|
||||
import 'package:fcs/domain/entities/fcs_shipment.dart';
|
||||
import 'package:fcs/helpers/paginator.dart';
|
||||
import 'package:fcs/pages/main/model/base_model.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
@@ -11,27 +12,43 @@ class FcsShipmentModel extends BaseModel {
|
||||
final log = Logger('FcsShipmentModel');
|
||||
|
||||
StreamSubscription<QuerySnapshot> listener;
|
||||
List<FcsShipment> fcsShipments = [];
|
||||
List<FcsShipment> _fcsShipments = [];
|
||||
List<FcsShipment> get fcsShipments => _selectedIndex == 1
|
||||
? _fcsShipments
|
||||
: List<FcsShipment>.from(_delivered.values);
|
||||
|
||||
Paginator _delivered;
|
||||
bool isLoading = false;
|
||||
int _selectedIndex = 1;
|
||||
set selectedIndex(int index) {
|
||||
_selectedIndex = index;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
get selectedIndex => _selectedIndex;
|
||||
|
||||
@override
|
||||
void privilegeChanged() {
|
||||
super.privilegeChanged();
|
||||
_loadFcsShipments();
|
||||
|
||||
if (_delivered != null) _delivered.close();
|
||||
_delivered = _getDelivered();
|
||||
}
|
||||
|
||||
Future<void> _loadFcsShipments() async {
|
||||
if (user == null || !user.hasFcsShipments()) return;
|
||||
String path = "/$fcs_shipment_collection/";
|
||||
if (listener != null) listener.cancel();
|
||||
fcsShipments = [];
|
||||
_fcsShipments = [];
|
||||
try {
|
||||
listener = Firestore.instance
|
||||
.collection("$path")
|
||||
.orderBy("shipment_number", descending: true)
|
||||
.snapshots()
|
||||
.listen((QuerySnapshot snapshot) {
|
||||
fcsShipments.clear();
|
||||
fcsShipments = snapshot.documents.map((documentSnapshot) {
|
||||
_fcsShipments.clear();
|
||||
_fcsShipments = snapshot.documents.map((documentSnapshot) {
|
||||
var s = FcsShipment.fromMap(
|
||||
documentSnapshot.data, documentSnapshot.documentID);
|
||||
return s;
|
||||
@@ -43,6 +60,36 @@ class FcsShipmentModel extends BaseModel {
|
||||
}
|
||||
}
|
||||
|
||||
Paginator _getDelivered() {
|
||||
if (user == null || !user.hasFcsShipments()) return null;
|
||||
|
||||
var pageQuery = Firestore.instance
|
||||
.collection("/$fcs_shipment_collection")
|
||||
.where("is_delivered", isEqualTo: true)
|
||||
.where("is_deleted", isEqualTo: false)
|
||||
.orderBy("current_status_date", descending: true);
|
||||
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
|
||||
return FcsShipment.fromMap(data, id);
|
||||
});
|
||||
return paginator;
|
||||
}
|
||||
|
||||
Future<void> loadMore() async {
|
||||
if (_delivered.ended) return;
|
||||
isLoading = true;
|
||||
notifyListeners();
|
||||
await _delivered.load(onFinished: () {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> refresh() async {
|
||||
await _delivered.refresh(onFinished: () {
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
void initUser(user) {
|
||||
super.initUser(user);
|
||||
}
|
||||
@@ -50,7 +97,8 @@ class FcsShipmentModel extends BaseModel {
|
||||
@override
|
||||
logout() async {
|
||||
if (listener != null) await listener.cancel();
|
||||
fcsShipments = [];
|
||||
if (_delivered != null) _delivered.close();
|
||||
_fcsShipments = [];
|
||||
}
|
||||
|
||||
Future<void> create(FcsShipment fcsShipment) {
|
||||
|
||||
Reference in New Issue
Block a user