import 'package:flutter/material.dart'; import 'package:flutter/scheduler.dart' show timeDilation; import 'package:page_transition/page_transition.dart'; import 'package:teso/Classes/CouponRateCalculator.dart'; import 'package:teso/Pages/Sub_Pages/CoinPurchase.dart'; import 'package:teso/providers/device_provider.dart'; import 'package:provider/provider.dart'; import 'package:teso/Classes/API Clasess/CouponHead.dart'; import 'package:teso/Classes/API Clasess/ProximityCoupon.dart'; import 'package:teso/Pages/PageWidgets/ProximityList/ActiveDiscount.dart'; import 'package:teso/Pages/PageWidgets/ProximityList/ActiveFreebie.dart'; import 'package:teso/Pages/PageWidgets/ProximityList/DummyDiscount.dart'; import 'package:teso/Pages/PageWidgets/ProximityList/DummyFreebie.dart'; import 'package:teso/providers/user_provider.dart'; class ProximityCoupons extends StatefulWidget { @override _ProximityCouponsState createState() => _ProximityCouponsState(); } class _ProximityCouponsState extends State with TickerProviderStateMixin { AnimationController _buttonController; Animation rotate; Animation right; Animation bottom; Animation width; int flag = 0; List data = []; List selectedData = []; double selectedDiscount; double price = 0; @override void initState() { super.initState(); _buttonController = new AnimationController( duration: new Duration(milliseconds: 1000), vsync: this); rotate = new Tween( begin: -0.0, end: -40.0, ).animate( new CurvedAnimation( parent: _buttonController, curve: Curves.ease, ), ); rotate.addListener(() { setState(() { if (rotate.isCompleted) { var i = data.removeLast(); data.insert(0, i); _buttonController.reset(); } }); }); right = new Tween( begin: 0.0, end: 400.0, ).animate( new CurvedAnimation( parent: _buttonController, curve: Curves.ease, ), ); bottom = new Tween( begin: 15.0, end: 100.0, ).animate( new CurvedAnimation( parent: _buttonController, curve: Curves.ease, ), ); width = new Tween( begin: 20.0, end: 25.0, ).animate( new CurvedAnimation( parent: _buttonController, curve: Curves.bounceOut, ), ); } @override void dispose() { _buttonController.dispose(); super.dispose(); } Future _swipeAnimation() async { try { await _buttonController.forward(); } on TickerCanceled {} } dismissImg(ProximityCoupon img) async { CouponsHead couponsHead = new CouponsHead(); couponsHead.businessId = img.business.businessId; couponsHead.expiration = img.expiration; couponsHead.couponId = img.couponId; couponsHead.quantity = img.quantity; couponsHead.state = "active"; couponsHead.lower = double.parse(img.lowerLimit.toString()); couponsHead.upper = double.parse(img.upperLimit.toString()); Provider.of(context, listen: false) .declineCoupon(couponsHead); setState(() { data.remove(img); }); } addImg(ProximityCoupon img) async { CouponsHead couponsHead = new CouponsHead(); couponsHead.businessId = img.business.businessId; couponsHead.expiration = img.expiration; couponsHead.couponId = img.couponId; couponsHead.quantity = 1; couponsHead.state = "active"; couponsHead.lower = double.parse(img.lowerLimit.toString()); couponsHead.upper = double.parse(img.upperLimit.toString()); couponsHead.targetProduct = img.targetID; double price = (img.targetCost * (img.lowerLimit / 100)); //Calculations int cost = CouponRateCalculator.getRate(price); Provider.of(context, listen: false) .acceptCoupon(couponsHead, cost.ceil(), context); setState(() { data.remove(img); selectedData.add(img); }); } swipeRight(img) { if (flag == 0) setState(() { flag = 1; }); _swipeAnimation(); addImg(img); } swipeLeft(img) { if (flag == 1) setState(() { flag = 0; }); _swipeAnimation(); dismissImg(img); } void calculateWorth(worth) { setState(() { price = worth; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Proximity Coupons"), actions: [ Consumer(builder: (BuildContext context, UserProvider value, Widget child) { return Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Container( height: 30, child: Row( children: [ InkWell( onTap: () => Navigator.push( context, PageTransition( type: PageTransitionType.bottomToTop, child: Coins(initalPage: 0), ), ), child: Image( image: AssetImage("assets/images/gold1.png"), ), ), InkWell( onTap: () => Navigator.push( context, PageTransition( type: PageTransitionType.bottomToTop, child: Coins(initalPage: 0), ), ), child: Container( margin: EdgeInsets.only(left: 2), child: Center( child: Text( value.currentUser.gold, style: TextStyle(fontSize: 13.5), ), ), ), ), InkWell( onTap: () => Navigator.push( context, PageTransition( type: PageTransitionType.bottomToTop, child: Coins(initalPage: 1), ), ), child: Image( image: AssetImage("assets/images/silver1.png"), ), ), InkWell( onTap: () => Navigator.push( context, PageTransition( type: PageTransitionType.bottomToTop, child: Coins(initalPage: 1), ), ), child: Container( margin: EdgeInsets.only( left: 2, right: 2, ), child: Center( child: Text( value.currentUser.silver, style: TextStyle(fontSize: 13.5), ), ), ), ), ], ), ), ], ); }), ], ), body: Consumer( builder: (context, value, child) { if (!value.serviceEnabled) { return Container( width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.height, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ Container( child: Text( "Proximity Coupon Alerts Disabled", textAlign: TextAlign.center, style: TextStyle( color: Color(0xFF003445), fontFamily: 'WickedGrit', fontSize: 30, ), ), ), new ElevatedButton( style: ElevatedButton.styleFrom( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(18.0), ), primary: Colors.green[200], padding: new EdgeInsets.all(10.0), ), onPressed: () => Provider.of(context, listen: false) .toggleBackgroundMode(context), child: new Container( height: 30.0, width: 100.0, alignment: Alignment.center, decoration: new BoxDecoration( color: Colors.green[200], borderRadius: new BorderRadius.circular(60.0), ), child: new Text( "Enable", style: new TextStyle( color: Colors.white, ), ), ), ), ], ), ), ); } else if (value.proximityCoupons == null || value.proximityCoupons.length == 0) { return Center( child: Text("No coupons available in your location"), ); } else { data = value.proximityCoupons; timeDilation = 0.4; double initialBottom = 15.0; var dataLength = data.length; double backCardPosition = initialBottom + (dataLength - 1) * 10 + 10; double backCardWidth = -10.0; return Container( alignment: Alignment.center, child: dataLength > 0 ? new Stack( alignment: AlignmentDirectional.center, children: data.map((item) { if (data.indexOf(item) == dataLength - 1 && item.type.toLowerCase().contains("discount")) { selectedDiscount = item.lowerLimit; return buildActiveDiscountCoupon( item, bottom.value, right.value, 0.0, backCardWidth + 10, rotate.value, rotate.value < -10 ? 0.1 : 0.0, context, dismissImg, flag, addImg, swipeRight, swipeLeft, price, calculateWorth); } else if (data.indexOf(item) == dataLength - 1 && item.type.toLowerCase().contains("freebie")) { return buildActiveFreebieCoupon( item, bottom.value, right.value, 0.0, backCardWidth + 10, rotate.value, rotate.value < -10 ? 0.1 : 0.0, context, dismissImg, flag, addImg, swipeRight, swipeLeft, ); } else if (data.indexOf(item) < dataLength - 1 && item.type.toLowerCase().contains("discount")) { backCardPosition = backCardPosition - 10; backCardWidth = backCardWidth + 10; double price = item.targetCost - (item.targetCost * item.lowerLimit / 100); return buildDummyDiscountCoupon( item, backCardPosition, 0.0, 0.0, backCardWidth, 0.0, 0.0, context, price.toString(), selectedDiscount); } else { backCardPosition = backCardPosition - 10; backCardWidth = backCardWidth + 10; double price = item.targetCost - (item.targetCost * item.lowerLimit / 100); return buildDummyFreebieCoupon( item, backCardPosition, 0.0, 0.0, backCardWidth, 0.0, 0.0, context, price.toString(), selectedDiscount); } }).toList()) : new Center( child: Text("No coupons available in your location")), ); } }, ), ); } }