add pagination for pages
This commit is contained in:
@@ -107,9 +107,9 @@ class _DeliveryInfoState extends State<DeliveryInfo> {
|
||||
}
|
||||
|
||||
_calShipmentWeight() {
|
||||
double l = double.parse(_lengthController.text);
|
||||
double w = double.parse(_widthController.text);
|
||||
double h = double.parse(_heightController.text);
|
||||
double l = double.tryParse(_lengthController.text) ?? 0;
|
||||
double w = double.tryParse(_widthController.text) ?? 0;
|
||||
double h = double.tryParse(_heightController.text) ?? 0;
|
||||
setState(() {
|
||||
shipmentWeight = l * w * h / volumetricRatio;
|
||||
});
|
||||
|
||||
@@ -7,6 +7,8 @@ import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../domain/entities/carton.dart';
|
||||
import '../../pagination/paginator_listview.dart';
|
||||
import 'delivery_list_row.dart';
|
||||
import 'model/delivery_model.dart';
|
||||
|
||||
@@ -17,97 +19,62 @@ class DeliverList extends StatefulWidget {
|
||||
|
||||
class _DeliverListState extends State<DeliverList> {
|
||||
bool _isLoading = false;
|
||||
var _controller = ScrollController();
|
||||
int _selectedIndex = 1;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller.addListener(() async {
|
||||
if (_controller.position.pixels == _controller.position.maxScrollExtent) {
|
||||
Provider.of<DeliveryModel>(context, listen: false).loadMore();
|
||||
}
|
||||
});
|
||||
Provider.of<DeliveryModel>(context, listen: false).initData();
|
||||
_init();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
_init() {
|
||||
var model = context.read<DeliveryModel>();
|
||||
_selectedIndex = model.selectedIndex;
|
||||
model.loadPaginationCartons(_selectedIndex);
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var deliveryModel = Provider.of<DeliveryModel>(context);
|
||||
|
||||
final popupMenu = LocalPopupMenuButton(
|
||||
popmenus: [
|
||||
LocalPopupMenu(
|
||||
id: 1,
|
||||
textKey: "delivery.popupmenu.active",
|
||||
selected: deliveryModel.selectedIndex == 1),
|
||||
LocalPopupMenu(
|
||||
id: 2,
|
||||
textKey: "delivery.popupmenu.delivered",
|
||||
selected: deliveryModel.selectedIndex == 2)
|
||||
],
|
||||
popupMenuCallback: (p) => this.setState(() {
|
||||
deliveryModel.selectedIndex = p.id;
|
||||
}),
|
||||
);
|
||||
popmenus: [
|
||||
LocalPopupMenu(
|
||||
id: 1,
|
||||
textKey: "delivery.popupmenu.active",
|
||||
selected: deliveryModel.selectedIndex == 1),
|
||||
LocalPopupMenu(
|
||||
id: 2,
|
||||
textKey: "delivery.popupmenu.delivered",
|
||||
selected: deliveryModel.selectedIndex == 2)
|
||||
],
|
||||
popupMenuCallback: (p) {
|
||||
setState(() {
|
||||
_selectedIndex = p.id;
|
||||
});
|
||||
context.read<DeliveryModel>().onChanged(_selectedIndex);
|
||||
});
|
||||
|
||||
return LocalProgress(
|
||||
inAsyncCall: _isLoading,
|
||||
child: DefaultTabController(
|
||||
length: 2,
|
||||
inAsyncCall: _isLoading,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
leading: new IconButton(
|
||||
icon: new Icon(CupertinoIcons.back),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
),
|
||||
backgroundColor: primaryColor,
|
||||
title: LocalText(context, "delivery",
|
||||
color: Colors.white, fontSize: 20),
|
||||
actions: <Widget>[popupMenu],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: RefreshIndicator(
|
||||
child: new ListView.separated(
|
||||
physics: AlwaysScrollableScrollPhysics(),
|
||||
controller: _controller,
|
||||
separatorBuilder: (context, index) => Divider(
|
||||
color: Colors.black,
|
||||
height: 1,
|
||||
),
|
||||
scrollDirection: Axis.vertical,
|
||||
shrinkWrap: true,
|
||||
itemCount: deliveryModel.cartons.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return DeliveryListRow(
|
||||
key: ValueKey(deliveryModel.cartons[index].id),
|
||||
box: deliveryModel.cartons[index]);
|
||||
}),
|
||||
onRefresh: () => deliveryModel.refresh(),
|
||||
),
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
leading: new IconButton(
|
||||
icon: new Icon(CupertinoIcons.back),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
),
|
||||
deliveryModel.isLoading
|
||||
? Container(
|
||||
padding: EdgeInsets.all(8),
|
||||
color: primaryColor,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text("Loading...",
|
||||
style: TextStyle(color: Colors.white)),
|
||||
],
|
||||
),
|
||||
)
|
||||
: Container(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
backgroundColor: primaryColor,
|
||||
title: LocalText(context, "delivery",
|
||||
color: Colors.white, fontSize: 20),
|
||||
actions: <Widget>[popupMenu],
|
||||
),
|
||||
body: PaginatorListView<Carton>(
|
||||
paginatorListener: deliveryModel.getCartons!,
|
||||
rowBuilder: (p) => DeliveryListRow(box: p),
|
||||
color: primaryColor)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,100 +4,61 @@ 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/carton.dart';
|
||||
import 'package:fcs/helpers/paginator.dart';
|
||||
import 'package:fcs/pages/main/model/base_model.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
import '../../../pagination/paginator_listener.dart';
|
||||
|
||||
class DeliveryModel extends BaseModel {
|
||||
final log = Logger('DeliveryModel');
|
||||
List<Carton> get cartons => _selectedIndex == 1
|
||||
? _cartons
|
||||
: List<Carton>.from(_delivered?.values ?? []);
|
||||
PaginatorListener<Carton>? getCartons;
|
||||
int selectedIndex = 1;
|
||||
|
||||
Paginator? _delivered;
|
||||
int _selectedIndex = 1;
|
||||
bool isLoading = false;
|
||||
List<Carton> _cartons = [];
|
||||
StreamSubscription<QuerySnapshot>? listener;
|
||||
|
||||
set selectedIndex(int index) {
|
||||
_selectedIndex = index;
|
||||
onChanged(int index) {
|
||||
selectedIndex = index;
|
||||
loadPaginationCartons(selectedIndex);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
int get selectedIndex => _selectedIndex;
|
||||
|
||||
initData() {
|
||||
_selectedIndex = 1;
|
||||
_loadCartons();
|
||||
|
||||
if (_delivered != null) _delivered!.close();
|
||||
_delivered = _getDelivered();
|
||||
_delivered!.load();
|
||||
}
|
||||
|
||||
Future<void> _loadCartons() async {
|
||||
loadPaginationCartons(int index) {
|
||||
if (user == null || !user!.hasDeliveries()) return;
|
||||
String path = "/$cartons_collection/";
|
||||
if (listener != null) listener!.cancel();
|
||||
_cartons = [];
|
||||
try {
|
||||
listener = FirebaseFirestore.instance
|
||||
.collection("$path")
|
||||
String path = "/$cartons_collection";
|
||||
|
||||
Query col = FirebaseFirestore.instance.collection(path);
|
||||
Query pageQuery = FirebaseFirestore.instance.collection(path);
|
||||
|
||||
if (index == 1) {
|
||||
col = col
|
||||
.where("status", isEqualTo: carton_shipped_status)
|
||||
.where("carton_type", whereIn: [
|
||||
carton_from_packages,
|
||||
carton_from_shipments,
|
||||
carton_small_bag
|
||||
])
|
||||
.where("is_deleted", isEqualTo: false)
|
||||
.orderBy("carton_number", descending: false)
|
||||
.snapshots()
|
||||
.listen((QuerySnapshot snapshot) {
|
||||
_cartons.clear();
|
||||
_cartons = snapshot.docs.map((documentSnapshot) {
|
||||
var s = Carton.fromMap(
|
||||
documentSnapshot.data as Map<String, dynamic>,
|
||||
documentSnapshot.id);
|
||||
return s;
|
||||
}).toList();
|
||||
notifyListeners();
|
||||
});
|
||||
} catch (e) {
|
||||
log.warning("Error!! $e");
|
||||
carton_from_packages,
|
||||
carton_from_shipments,
|
||||
carton_small_bag
|
||||
]);
|
||||
pageQuery = pageQuery
|
||||
.where("status", isEqualTo: carton_shipped_status)
|
||||
.where("carton_type", whereIn: [
|
||||
carton_from_packages,
|
||||
carton_from_shipments,
|
||||
carton_small_bag
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Paginator _getDelivered() {
|
||||
if (user == null || !user!.hasDeliveries()) throw "No Privilege";
|
||||
if (index == 2) {
|
||||
col = col
|
||||
.where("is_delivered", isEqualTo: true)
|
||||
.where("status", whereIn: [carton_delivered_status]);
|
||||
pageQuery = pageQuery
|
||||
.where("is_delivered", isEqualTo: true)
|
||||
.where("status", whereIn: [carton_delivered_status]);
|
||||
}
|
||||
|
||||
var pageQuery = FirebaseFirestore.instance
|
||||
.collection("/$cartons_collection")
|
||||
.where("is_delivered", isEqualTo: true)
|
||||
.where("status", whereIn: [carton_delivered_status]).where("is_deleted",
|
||||
isEqualTo: false);
|
||||
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
|
||||
return Carton.fromMap(data, id);
|
||||
});
|
||||
return paginator;
|
||||
}
|
||||
pageQuery = pageQuery.orderBy("carton_number");
|
||||
|
||||
Future<void> loadMore() async {
|
||||
if (_delivered!.ended || _selectedIndex == 1) return;
|
||||
isLoading = true;
|
||||
notifyListeners();
|
||||
await _delivered!.load(onFinished: () {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> refresh() async {
|
||||
if (_selectedIndex == 1) return;
|
||||
|
||||
await _delivered!.refresh(onFinished: () {
|
||||
notifyListeners();
|
||||
});
|
||||
getCartons?.close();
|
||||
getCartons = PaginatorListener<Carton>(
|
||||
col, pageQuery, (data, id) => Carton.fromMap(data, id),
|
||||
rowPerLoad: 30);
|
||||
}
|
||||
|
||||
void initUser(user) {
|
||||
@@ -106,9 +67,7 @@ class DeliveryModel extends BaseModel {
|
||||
|
||||
@override
|
||||
logout() async {
|
||||
if (listener != null) await listener!.cancel();
|
||||
if (_delivered != null) _delivered!.close();
|
||||
_cartons = [];
|
||||
getCartons?.close();
|
||||
}
|
||||
|
||||
Future<void> deliver(Carton carton) {
|
||||
|
||||
Reference in New Issue
Block a user