import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:provider/provider.dart'; import 'package:teso/Pages/codeQR.dart'; import 'package:teso/providers/user_provider.dart'; import 'package:teso/util/consts.dart'; import 'dart:convert'; import 'package:teso/Classes/API Clasess/CouponDetails.dart'; import 'package:teso/providers/pageAnimations.dart'; import 'package:teso/Pages/PageWidgets/Coupons/GiftFriend.dart'; import 'package:teso/Classes/TesoUser.dart'; import 'LoadGiftRecipient.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:teso/Classes/API Clasess/CouponHead.dart'; import 'package:flutter/cupertino.dart'; class AcquiredOptions extends StatefulWidget { final CouponDetails head; const AcquiredOptions({Key key, this.head}) : super(key: key); @override _AcquiredOptionsState createState() => _AcquiredOptionsState(head: this.head); } class _AcquiredOptionsState extends State { final CouponDetails head; _AcquiredOptionsState({this.head}); double _value; String result = ""; TextEditingController friendName; TesoUser selectedFriend = new TesoUser(); bool loading = false; double _n; int coinCost = 0; int _time = 0; bool freebie = false; changeValue(val) { if (mounted) setState(() { _value = val; }); } calcCost() { if (mounted) setState(() { _n = head.worth - (_value / 100) * head.worth; _n.roundToDouble(); }); } // ignore: unused_element Future _scanQRCode() async { result = ""; result = await Navigator.push( context, PageTransition( type: PageTransitionType.downToUp, child: QRCodeScanner(), ), ); if (result != null && result.isNotEmpty) { print("confirm purchase"); } } Future loadFriend(context) async { selectedFriend = await showModalBottomSheet( context: context, // isScrollControlled: true, // enableDrag: true, shape: RoundedRectangleBorder( borderRadius: BorderRadius.vertical(top: Radius.circular(20.0)), ), builder: (BuildContext bc) { return LoadGiftRecipient(); }); if (selectedFriend != null) { setState(() { selectedFriend = selectedFriend; }); friendName.text = selectedFriend.firstname + " " + selectedFriend.lastname; if (friendName.text.length > 15) friendName.text = friendName.text.substring(0, 15) + "..."; } else { friendName.text = ""; setState(() { selectedFriend = new TesoUser(); }); } } Future giftCoupon() async { setState(() { loading = true; }); if (head.worth > 0 && selectedFriend.userGUID != null) { CouponsHead gift = new CouponsHead(); gift.businessId = head.issuer.businessName; gift.targetProduct = head.targetProduct.productID; gift.couponId = head.couponId; if (freebie) { gift.lower = 100; } else { gift.lower = _value; } gift.state = head.countID; gift.upper = 0; gift.type = selectedFriend.userGUID; gift.expiration = head.expiration; SharedPreferences prefs = await SharedPreferences.getInstance(); Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': prefs.getString('tokensTeso') }; try { var register2 = serverLocation + 'coupons/giftCoupon'; var client1 = await http.post(Uri.parse(register2), body: json.encode(gift), headers: requestHeaders); if (client1.statusCode == 200) { setState(() { loading = false; }); Navigator.pop(context, true); Provider.of(context, listen: false).getCoupons(); } } catch (e) { setState(() { loading = false; }); print(e); } } } @override void initState() { _value = head.worth; friendName = new TextEditingController(); _n = head.worth; if (widget.head.type.toLowerCase().contains("freebie")) { setState(() { freebie = true; }); } super.initState(); } void add() { setState(() { _time++; coinCost = _time * 2; }); } void minus() { setState(() { if (_time != 0) { _time--; coinCost = (_time / 2).round(); } }); } @override Widget build(BuildContext context) { return Container( margin: EdgeInsets.symmetric( horizontal: (MediaQuery.of(context).size.width) * 0.030, vertical: (MediaQuery.of(context).size.width) * 0.040, ), child: Material( child: Stack( children: [ new Wrap( children: [ new Container( width: double.infinity, margin: EdgeInsets.only( top: 15.0, bottom: 12.0, ), child: Center( child: Text( "COUPON OPTIONS", style: TextStyle( fontSize: 18.0, ), ), )), Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0), child: Divider(), ), new ListTile( leading: new Text( "Gift To : ", style: TextStyle( fontWeight: FontWeight.bold, ), ), title: buildGiftRecipient( context: context, searchkey: friendName, loadFriend: () => loadFriend(context), friend: selectedFriend), ), if (!freebie) Row( children: [ Text( "Discount ", style: TextStyle( fontWeight: FontWeight.bold, ), ), Text("0 %"), Slider( value: _value, min: 0, max: 100, divisions: 20, activeColor: accentMain, inactiveColor: darkAccent, label: _value.toString() + "%", onChanged: (double newValue) { setState(() { _value = newValue; }); calcCost(); }, ), Text("100 %"), ], ), Center( child: ElevatedButton( style: ElevatedButton.styleFrom( primary: accentMain, shape: RoundedRectangleBorder( borderRadius: BorderRadius.all( Radius.circular(20.0), ), ), ), onPressed: () async => await giftCoupon(), child: Text("Gift"), ), ), Center( child: Wrap( direction: Axis.horizontal, children: [ Container( child: Text("Current Coupon Worth : "), ), Container( child: Text( _n.toString() + " % ", style: TextStyle( // fontSize: 20, fontWeight: FontWeight.bold, ), ), ), ], ), ), // Divider(), // Row( // mainAxisAlignment: MainAxisAlignment.spaceEvenly, // children: [ // Text( // "Additional Time (hours) ", // style: TextStyle( // fontWeight: FontWeight.bold, // ), // ), // Container( // width: 30, // child: new FloatingActionButton( // onPressed: minus, // child: new Text( // "-", // style: TextStyle( // fontSize: 30, // color: Colors.black, // ), // ), // backgroundColor: Colors.white, // ), // ), // new Text( // '$_time', // style: TextStyle( // fontSize: 20, // fontWeight: FontWeight.bold, // ), // ), // Container( // width: 30, // child: new FloatingActionButton( // onPressed: add, // child: new Icon( // Icons.add, // color: Colors.black, // size: 20, // ), // backgroundColor: Colors.white, // ), // ), // ], // ), // Center( // child: Row( // mainAxisAlignment: MainAxisAlignment.spaceEvenly, // children: [ // Container( // child: Text( // "Cost", // style: TextStyle( // fontWeight: FontWeight.bold, // ), // ), // ), // Wrap( // direction: Axis.horizontal, // children: [ // Container( // child: Container( // height: 30.0, // width: 30.0, // decoration: new BoxDecoration( // shape: BoxShape.circle, // ), // child: ClipRRect( // borderRadius: BorderRadius.only( // topLeft: Radius.circular(50.0), // topRight: Radius.circular(50.0), // bottomLeft: Radius.circular(50), // bottomRight: Radius.circular(50), // ), // child: Image( // height: 30, // width: 30, // fit: BoxFit.fill, // image: // AssetImage("assets/images/silver1.png"), // ), // ), // ), // ), // Container( // width: 30, // height: 30, // child: Center( // child: Text( // "$_time", // style: TextStyle( // fontSize: 16.5, // fontWeight: FontWeight.bold, // ), // ), // ), // ), // ], // ), // ], // ), // ), // Center( // child: ElevatedButton( // style: ElevatedButton.styleFrom( // shape: RoundedRectangleBorder( // borderRadius: BorderRadius.circular(20.0), // ), // primary: accentMain, // ), // onPressed: () async => await giftCoupon(), // child: Text("Extend Time"), // ), // ), ], ), Visibility( visible: loading, child: Container( color: Color.fromRGBO(0, 0, 0, 0.2), width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.width, child: Center( child: CupertinoActivityIndicator( animating: true, radius: 15, ), ), ), ), ], ), ), ); } }