You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.8 KiB

3 years ago
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:teso/Classes/API%20Clasess/CouponDetails.dart';
import 'package:teso/Classes/API%20Clasess/CouponHead.dart';
import 'package:teso/Classes/Payload.dart';
import 'package:teso/Notifications/NotificationPlugin.dart';
import 'package:teso/Pages/PageWidgets/Coupons/personalizedDiscount.dart';
import 'package:teso/Pages/PageWidgets/Coupons/personalizedFreebie.dart';
import 'package:teso/util/consts.dart';
class PersonalCoupon extends StatefulWidget {
final CouponDetails details;
final CouponsHead head;
const PersonalCoupon({Key key, this.details, this.head}) : super(key: key);
@override
_PersonalCouponState createState() => _PersonalCouponState();
}
class _PersonalCouponState extends State<PersonalCoupon> {
bool acquiring = false;
void acceptCoupon() async {
setState(() {
acquiring = true;
});
try {
SharedPreferences prefs = await SharedPreferences.getInstance();
String token = prefs.getString("tokensTeso");
Map<String, String> requestheaders = {
'Content-type': 'application/json',
'Authorization': token
};
var register = serverLocation + 'coupons/acceptPersonalized';
var client = await http.post(Uri.parse(register),
body: json.encode(widget.head), headers: requestheaders);
if (client.statusCode == 200) {
Payload payload = new Payload();
payload.loadID = "TESN000";
payload.load1 = "CouponAcquisition";
await notificationPlugin.showNotification(
"Coupon Accepted",
"The personalied coupon has been added successfully!!",
payload.toString(),
);
Navigator.pop(context);
} else if (client.statusCode == 400) {
Payload payload = new Payload();
payload.loadID = "TESN000";
payload.load1 = "CouponAcquisition";
await notificationPlugin.showNotification(
"Offer Expired",
"Unable to acquire coupon as this offer has expired!!",
payload.toString(),
);
Navigator.pop(context);
} else {
Payload payload = new Payload();
payload.loadID = "TESN000";
payload.load1 = "CouponAcquisition";
await notificationPlugin.showNotification(
"Error Occurred",
"Unable to acquire coupon as this moment",
payload.toString(),
);
setState(() {
acquiring = false;
});
}
} catch (e) {
print(e);
Payload payload = new Payload();
payload.loadID = "TESN000";
payload.load1 = "CouponAcquisition";
await notificationPlugin.showNotification(
"Error Occurred",
"Unable to acquire coupon as this moment",
payload.toString(),
);
setState(() {
acquiring = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
color: Color.fromRGBO(0, 0, 0, 0.8),
child: Stack(
children: [
Align(
alignment: Alignment.topRight,
child: Container(
child: InkWell(
onTap: () => Navigator.pop(context),
child: Icon(
Icons.close_outlined,
color: Colors.white,
size: 35,
),
),
margin: EdgeInsets.symmetric(horizontal: 30, vertical: 30),
),
),
Align(
alignment: Alignment.center,
child: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.85,
padding: EdgeInsets.only(top: 25),
child: widget.head.type == "TESCP006"
? buildPersonalizedFreebieCoupon(
context, widget.details, acceptCoupon)
: buildPersonalizedDiscountCoupon(
context, widget.details, acceptCoupon)),
),
Visibility(
visible: acquiring,
child: Container(
color: Colors.white,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Center(
child: CupertinoActivityIndicator(
animating: true,
radius: 15,
),
),
),
),
],
),
),
);
}
}