import 'dart:convert'; import 'package:teso/util/consts.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:teso/Classes/API Clasess/CouponDetails.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:flutter/cupertino.dart'; import 'package:teso/Pages/PageWidgets/WalkIn Coupons/ActiveDiscount.dart'; import 'package:teso/Pages/PageWidgets/WalkIn Coupons/ActiveFreebie.dart'; class ScanQR extends StatefulWidget { final String shopID; const ScanQR({Key key, this.shopID}) : super(key: key); @override _ScanQRState createState() => _ScanQRState(shopID: this.shopID); } class _ScanQRState extends State with TickerProviderStateMixin { String shopID; _ScanQRState({this.shopID}); int flag = 0; var _future; List data; List selectedData = []; double selectedDiscount = 0; void initState() { super.initState(); _future = getShopsCoupons(); } @override void dispose() { super.dispose(); } Future> getShopsCoupons() async { SharedPreferences prefs = await SharedPreferences.getInstance(); Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': prefs.getString('tokensTeso') }; var register2 = serverLocation + 'coupons/qrCoupon'; var client1 = await http.post(Uri.parse(register2), body: json.encode(shopID), headers: requestHeaders); if (client1.statusCode == 200) { try { var details = jsonDecode(client1.body); data = List.from( details.map((model) => CouponDetails.fromJSON(model)).toList()); return data; } catch (e) { print(e); return null; } } else { return null; } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Redeem Coupon"), ), body: FutureBuilder( future: _future, builder: (context, snapshot) { if (snapshot.data == null && snapshot.connectionState == ConnectionState.waiting) { return Center( child: CupertinoActivityIndicator( animating: true, radius: 15, ), ); } else if ((data == null || data.length == 0) && snapshot.connectionState == ConnectionState.done) { return Center( child: Text( "Sorry you do not have any coupon from this shop to redeem"), ); } else { return ListView.builder( scrollDirection: Axis.horizontal, itemCount: data.length, itemBuilder: (context, index) { if (data.elementAt(index).type.contains("FREEBIE")) { return buildActiveFreebieCoupon( data.elementAt(index), 0, context); } else { return buildActiveDiscountCoupon( data.elementAt(index), 0, context); } }, ); } }, ), ); } }