import 'package:teso/Classes/TesoUser.dart'; import 'package:flutter/material.dart'; import 'package:teso/Pages/PageWidgets/Recently%20Viewed/viewedItem.dart'; import 'package:teso/Classes/API Clasess/CouponDetails.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:flutter/cupertino.dart'; import 'package:provider/provider.dart'; import 'package:teso/providers/user_provider.dart'; import 'package:teso/Classes/API Clasess/Product.dart'; import 'package:teso/Classes/API Clasess/TesoBusinessDetail.dart'; import 'package:teso/Classes/API Clasess/CouponHead.dart'; import 'package:teso/Pages/Sub_Pages/Coupons/Acquire.dart'; class Recently extends StatefulWidget { @override _RecentlyState createState() => _RecentlyState(); } class _RecentlyState extends State { TesoUser? currentUser; int _limit = 20; final int _limitIncrement = 20; List? coupons = new List.from([]); final ScrollController listScrollController = ScrollController(); @override void initState() { listScrollController.addListener(_scrollListener); super.initState(); } _scrollListener() { if (listScrollController.offset >= listScrollController.position.maxScrollExtent && !listScrollController.position.outOfRange) { setState(() { _limit += _limitIncrement; }); } if (listScrollController.offset <= listScrollController.position.minScrollExtent && !listScrollController.position.outOfRange) { setState(() {}); } } tesoCouponDialog(context, CouponsHead head, double price) { showModalBottomSheet( context: context, isScrollControlled: true, enableDrag: true, shape: RoundedRectangleBorder( borderRadius: BorderRadius.vertical(top: Radius.circular(20.0)), ), builder: (BuildContext bc) { return AcquireCoupon( head: head, price: price, ); }); } @override Widget build(BuildContext context) { return Consumer( builder: (BuildContext context, UserProvider user, Widget? child) { // ignore: unnecessary_null_comparison if (user == null) { return Center( child: CupertinoActivityIndicator( animating: true, radius: 15, ), ); } else { currentUser = user.currentUser; return StreamBuilder( stream: FirebaseFirestore.instance .collection('recentlyViewed') .doc("users") .collection(currentUser!.userGUID!) .orderBy('dateViewed', descending: true) .limit(_limit) .snapshots(), builder: (BuildContext context, AsyncSnapshot snapshot) { if (!snapshot.hasData) { return Center( child: CircularProgressIndicator( valueColor: AlwaysStoppedAnimation( Theme.of(context).primaryColor))); } else if (snapshot.data == null || coupons == null) { return Center( child: CircularProgressIndicator( valueColor: AlwaysStoppedAnimation( Theme.of(context).primaryColor))); } else { coupons = snapshot.data!.docs; return ListView.builder( itemCount: coupons!.length, controller: listScrollController, itemBuilder: (context, int index) { if (coupons!.length <= 0) { return Container(); } else { CouponDetails couponDetails = new CouponDetails(); couponDetails.issuer = new TesoBusinessDetail(); couponDetails.targetProduct = new Product(); couponDetails.businessId = snapshot.data!.docs[index]['businessId']; couponDetails.couponId = snapshot.data!.docs[index]['couponId']; couponDetails.expiration = DateTime.parse(snapshot .data!.docs[index] ['expiration'] .toString()); couponDetails.type = snapshot.data!.docs[index]['type']; couponDetails.quantity = int.parse(snapshot .data!.docs[index] ['quantity'] .toString()); couponDetails.worth = double.parse(snapshot .data!.docs[index] ['worth'] .toString()); couponDetails.state = snapshot.data!.docs[index]['state']; couponDetails.productCost = double.parse(snapshot .data!.docs[index] ['unitPrice'] .toString()); couponDetails.issuer!.businessAddress = snapshot.data!.docs[index]['businessAddress']; couponDetails.issuer!.businessContact = snapshot.data!.docs[index]['businessContact']; couponDetails.issuer!.businessDescription = snapshot .data!.docs[index] ['businessDescription']; couponDetails.issuer!.businessLat = snapshot.data!.docs[index]['businessLat']; couponDetails.issuer!.businessLng = snapshot.data!.docs[index]['businessLng']; couponDetails.issuer!.businessLogo = snapshot.data!.docs[index]['businessLogo']; couponDetails.issuer!.businessName = snapshot.data!.docs[index]['businessName']; couponDetails.targetProduct!.productID = snapshot.data!.docs[index]['productId']; couponDetails.targetProduct!.productName = snapshot.data!.docs[index]['name']; couponDetails.targetProduct!.productImage = snapshot.data!.docs[index]['productImage']; couponDetails.targetProduct!.unitPrice = double.parse( snapshot.data!.docs[index] ['unitPrice'] .toString()); couponDetails.lowerLimit = double.parse(snapshot .data!.docs[index] ['lowerLimit'] .toString()); couponDetails.upperLimit = double.parse(snapshot .data!.docs[index] ['upperLimit'] .toString()); return buildRecentItem( context, couponDetails, tesoCouponDialog); } }, ); } }); } }, ); } }