|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/scheduler.dart' show timeDilation;
|
|
|
|
|
|
|
|
import 'package:teso/Classes/CouponRateCalculator.dart';
|
|
|
|
import 'package:teso/Pages/PageWidgets/HomeProximity/ActiveDiscount.dart';
|
|
|
|
import 'package:teso/Pages/PageWidgets/HomeProximity/ActiveFreebie.dart';
|
|
|
|
import 'package:teso/Pages/PageWidgets/HomeProximity/DummyDiscount.dart';
|
|
|
|
import 'package:teso/Pages/PageWidgets/HomeProximity/DummyFreebie.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';
|
|
|
|
|
|
|
|
class ProximityCoupons extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_ProximityCouponsState createState() => _ProximityCouponsState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ProximityCouponsState extends State<ProximityCoupons>
|
|
|
|
with TickerProviderStateMixin {
|
|
|
|
late AnimationController _buttonController;
|
|
|
|
late Animation<double> rotate;
|
|
|
|
late Animation<double> right;
|
|
|
|
late Animation<double> bottom;
|
|
|
|
Animation<double>? width;
|
|
|
|
int flag = 0;
|
|
|
|
List<ProximityCoupon> data = [];
|
|
|
|
List<ProximityCoupon> selectedData = [];
|
|
|
|
double? selectedDiscount;
|
|
|
|
double worth = 0;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_buttonController = new AnimationController(
|
|
|
|
duration: new Duration(milliseconds: 1000), vsync: this);
|
|
|
|
|
|
|
|
rotate = new Tween<double>(
|
|
|
|
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<double>(
|
|
|
|
begin: 0.0,
|
|
|
|
end: 400.0,
|
|
|
|
).animate(
|
|
|
|
new CurvedAnimation(
|
|
|
|
parent: _buttonController,
|
|
|
|
curve: Curves.ease,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
bottom = new Tween<double>(
|
|
|
|
begin: 15.0,
|
|
|
|
end: 90.0,
|
|
|
|
).animate(
|
|
|
|
new CurvedAnimation(
|
|
|
|
parent: _buttonController,
|
|
|
|
curve: Curves.ease,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
width = new Tween<double>(
|
|
|
|
begin: 20.0,
|
|
|
|
end: 25.0,
|
|
|
|
).animate(
|
|
|
|
new CurvedAnimation(
|
|
|
|
parent: _buttonController,
|
|
|
|
curve: Curves.bounceOut,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_buttonController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Null> _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<DeviceProvider>(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 = worth;
|
|
|
|
couponsHead.upper = double.parse(img.upperLimit.toString());
|
|
|
|
couponsHead.targetProduct = img.targetID;
|
|
|
|
double price = (img.targetCost! * (worth / 100));
|
|
|
|
|
|
|
|
//Calculations
|
|
|
|
int cost = CouponRateCalculator.getRate(price);
|
|
|
|
Provider.of<DeviceProvider>(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(val) {
|
|
|
|
setState(() {
|
|
|
|
this.worth = val;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
extendBody: false,
|
|
|
|
body: Consumer<DeviceProvider>(
|
|
|
|
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.spaceEvenly,
|
|
|
|
children: [
|
|
|
|
Container(
|
|
|
|
child: Icon(
|
|
|
|
Icons.location_off,
|
|
|
|
size: 120,
|
|
|
|
color: Color(0xFF003445),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
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<DeviceProvider>(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.isEmpty ||
|
|
|
|
value.proximityCoupons.length == 0) {
|
|
|
|
return Center(
|
|
|
|
child: Text("No coupons available in your location"),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
data = value.proximityCoupons;
|
|
|
|
timeDilation = 0.4;
|
|
|
|
double initialBottom = 20.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,
|
|
|
|
flag,
|
|
|
|
addImg,
|
|
|
|
dismissImg,
|
|
|
|
worth,
|
|
|
|
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,
|
|
|
|
flag,
|
|
|
|
addImg,
|
|
|
|
dismissImg,
|
|
|
|
);
|
|
|
|
} 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")),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|