import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:healthcare_pharmacy/models/offersview_model.dart'; import 'package:healthcare_pharmacy/offers.dart'; import 'package:healthcare_pharmacy/settings.dart'; import 'package:intl/intl.dart'; import 'package:flutter_cupertino_datetime_picker/flutter_cupertino_datetime_picker.dart'; class OffersView extends StatefulWidget { const OffersView({Key? key}) : super(key: key); @override State createState() => _OffersViewState(); } class _OffersViewState extends State with TickerProviderStateMixin { bool isSupplierDataLoading=false; bool isSereverIssue = false; bool isSereverIssueConnected = false; bool isSereverIssuePending = false; var startdate; var enddate; List offersviewList = []; TextEditingController offerNameController = TextEditingController(); TextEditingController offerCodeController = TextEditingController(); TextEditingController offerDescriptionController = TextEditingController(); TextEditingController offerDiscountController = TextEditingController(); TextEditingController offerStartDateController = TextEditingController(); TextEditingController offerEndDateController = TextEditingController(); TextEditingController updateOfferNameController = TextEditingController(); TextEditingController updateOfferCodeController = TextEditingController(); TextEditingController updateOfferDescriptionController = TextEditingController(); TextEditingController updateDiscountController = TextEditingController(); TextEditingController updateOfferStartDateController = TextEditingController(); TextEditingController updateOfferEndDateController = TextEditingController(); bool isLoading=false; Future getOffersViewData() async { isLoading = true; try { var response = await AppSettings.getOffers(); setState(() { offersviewList = ((jsonDecode(response)['data']) as List).map((dynamic model) { return GetOffersDetailsModel.fromJson(model); }).toList(); isLoading = false; }); } catch (e) { setState(() { isLoading = false; isSereverIssueConnected = true; }); } } @override void initState() { // TODO: implement initState isLoading=true; getOffersViewData(); super.initState(); } showUpdateOfferDialog(var object) async { updateOfferNameController.text = object.offer_name; updateOfferCodeController.text = object.offer_code; updateOfferDescriptionController.text = object.description; updateDiscountController.text = object.discount_percentage; updateOfferStartDateController.text=object.starting_date; updateOfferEndDateController.text=object.ending_date; return showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return AlertDialog( title: const Text('Update Offer'), content: SingleChildScrollView( child: ListBody( children: [ Container( padding: const EdgeInsets.fromLTRB(0, 0, 0, 0), child: TextFormField( cursorColor: greyColor, controller: updateOfferNameController, textCapitalization: TextCapitalization.characters, decoration: const InputDecoration( prefixIcon: Icon( Icons.person, color: primaryColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Enter offer name', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), //tanker name ), const SizedBox( height: 20, ), Container( //padding: const EdgeInsets.fromLTRB(10, 10, 10, 0), child: TextFormField( cursorColor: greyColor, controller: updateOfferCodeController, textCapitalization: TextCapitalization.characters, decoration: const InputDecoration( prefixIcon: Icon( Icons.numbers, color: primaryColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Enter Offer Code', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), //tanker name ), const SizedBox( height: 20, ), Container( //padding: const EdgeInsets.fromLTRB(10, 10, 10, 0), child: TextFormField( cursorColor: greyColor, controller: updateOfferDescriptionController, textCapitalization: TextCapitalization.characters, decoration: const InputDecoration( prefixIcon: Icon( Icons.description, color: primaryColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Enter Offer Description', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), //tanker name ), const SizedBox( height: 20, ), Container( //padding: const EdgeInsets.fromLTRB(10, 10, 10, 0), child: TextFormField( cursorColor: greyColor, maxLength: 2, controller: updateDiscountController, textCapitalization: TextCapitalization.characters, decoration: const InputDecoration( prefixIcon: Icon( Icons.percent_outlined, color: primaryColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: greyColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: greyColor), ), labelText: 'Enter Offer Discount', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), //tanker name ), const SizedBox( height: 20, ), Container( child: TextFormField( readOnly: true, cursorColor: greyColor, controller: updateOfferStartDateController, decoration: InputDecoration( labelText: 'Select Start Date', prefixIcon: IconButton( icon: Icon( Icons.date_range, color: primaryColor, ), onPressed: () async { DatePicker.showDatePicker( context, dateFormat: 'dd MMMM yyyy HH:mm', initialDateTime: DateTime.now(), minDateTime:DateTime.now(), maxDateTime: DateTime.now().add(Duration(days: 15)), onMonthChangeStartWithFirstDate: true, pickerMode: DateTimePickerMode.datetime, pickerTheme: DateTimePickerTheme( // backgroundColor: Colors.white, cancelTextStyle: labelTextStyle(), confirmTextStyle: labelTextStyle(), // showTitle: true, //title: Text('Pick date and time'), itemTextStyle: valuesTextStyle(), ), onConfirm: (dateTime, List index)async { DateTime selectdate = dateTime; setState(() { startdate = DateFormat('dd-MMM-yyyy - HH:mm').format(selectdate); }); if(startdate!=''){ setState(() { updateOfferStartDateController.text=startdate.toString(); }); } else { AppSettings.longFailedToast('please select date'); } }, ); }, ), labelStyle: const TextStyle( color: greyColor, //<-- SEE HERE ), border: const OutlineInputBorder( borderSide: BorderSide(color: primaryColor)), focusedBorder: const OutlineInputBorder( borderSide: BorderSide(color: primaryColor), ), enabledBorder: const OutlineInputBorder( borderSide: BorderSide(color: primaryColor), ), ), ), ), const SizedBox( height: 20, ), Container( child: TextFormField( readOnly: true, cursorColor: greyColor, controller: updateOfferEndDateController, decoration: InputDecoration( labelText: 'Select End Date', prefixIcon: IconButton( icon: Icon( Icons.date_range, color: primaryColor, ), onPressed: () async { DatePicker.showDatePicker( context, dateFormat: 'dd MMMM yyyy HH:mm', initialDateTime: DateTime.now(), minDateTime:DateTime.now(), maxDateTime: DateTime.now().add(Duration(days: 15)), onMonthChangeStartWithFirstDate: true, pickerMode: DateTimePickerMode.datetime, pickerTheme: DateTimePickerTheme( // backgroundColor: Colors.white, cancelTextStyle: labelTextStyle(), confirmTextStyle: labelTextStyle(), // showTitle: true, //title: Text('Pick date and time'), itemTextStyle: valuesTextStyle(), ), onConfirm: (dateTime, List index)async { DateTime selectdate = dateTime; setState(() { enddate = DateFormat('dd-MMM-yyyy - HH:mm').format(selectdate); }); if(enddate!=''){ setState(() { updateOfferEndDateController.text=enddate.toString(); }); } else { AppSettings.longFailedToast('please select date'); } }, ); }, ), labelStyle: const TextStyle( color: greyColor, //<-- SEE HERE ), border: const OutlineInputBorder( borderSide: BorderSide(color: primaryColor)), focusedBorder: const OutlineInputBorder( borderSide: BorderSide(color: primaryColor), ), enabledBorder: const OutlineInputBorder( borderSide: BorderSide(color: primaryColor), ), ), ), ),//address description const SizedBox( height: 30, ), ], ), ), actions: [ TextButton( child: Text('cancel', style: textButtonStyle()), onPressed: () { Navigator.of(context).pop(); }, ), TextButton( child: Text('Update', style: textButtonStyle()), onPressed: () async { if (updateOfferNameController.text != '' ) { AppSettings.preLoaderDialog(context); var payload = new Map(); payload["offer_name"] = updateOfferNameController.text.toString(); payload["offer_code"] = updateOfferCodeController.text.toString(); payload["description"] = updateOfferDescriptionController.text.toString(); payload["discount_percentage"] = updateDiscountController.text.toString(); payload["starting_date"] = updateOfferStartDateController.text.toString(); payload["ending_date"] = updateOfferEndDateController.text.toString(); payload["offer_status"] ="active"; bool offerStatus = await AppSettings.updateOffers(object.offer_code, payload); try { if (offerStatus) { Navigator.of(context, rootNavigator: true).pop(); AppSettings.longSuccessToast( "Offer Updated Successfully"); updateOfferNameController.text = ''; updateOfferCodeController.text = ''; updateOfferDescriptionController.text = ''; updateDiscountController.text = ''; updateOfferStartDateController.text = ''; updateOfferEndDateController.text = ''; Navigator.of(context).pop(); await getOffersViewData(); } else { Navigator.of(context, rootNavigator: true).pop(); AppSettings.longFailedStyledToast( "Offer upadtion failed", context); Navigator.of(context).pop(); } } catch (exception) { Navigator.of(context).pop(); print(exception); } } else { AppSettings.longFailedStyledToast("enter details", context); } }, ), ], ); }); }, ); } Widget renderzUi(){ if(offersviewList.length!=0){ return Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ Expanded(child:ListView.builder( padding: EdgeInsets.all(0), itemCount: offersviewList.length, itemBuilder: (BuildContext context, int index) { return Card( color: offersviewList[index].cardColor, child: Padding( padding:EdgeInsets.all(8) , child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, children: [ Container( //width: MediaQuery.of(context).size.width * .55, child: Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('OfferName :',style: labelTextStyle()), Text('OfferCode :',style: labelTextStyle()), Text('OfferDes :' ,style: labelTextStyle()), Text('OfferDis(%) :' ,style: labelTextStyle()), Text('StartDate :',style: labelTextStyle()), Text('EndDate :' ,style: labelTextStyle()), ], ), SizedBox(width: 5,), Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(offersviewList[index].offer_name,style: valuesTextStyle()), Text(offersviewList[index].offer_code,style: valuesTextStyle()), Text(offersviewList[index].description,style: valuesTextStyle()), Text(offersviewList[index].offer,style: valuesTextStyle()), Text(offersviewList[index].starting_date,style: valuesTextStyle()), Text(offersviewList[index].ending_date,style: valuesTextStyle()) ], ), ], ), ], ), ), Expanded(child:IconButton( icon: const Icon(Icons.edit,color: primaryColor,), onPressed: () { showUpdateOfferDialog(offersviewList[index]); }, ),), Expanded(child:IconButton( icon: const Icon(Icons.hide_source_rounded,color: primaryColor,), onPressed: () async{ showDialog( //if set to true allow to close popup by tapping out of the popup //barrierDismissible: false, context: context, builder: (BuildContext context) => AlertDialog( title: const Text('Do you want In_Active Offer??', style: TextStyle( color: primaryColor, fontSize: 16, )), actionsAlignment: MainAxisAlignment.spaceBetween, actions: [ TextButton( onPressed: ()async { bool deleteOfferStatus = await AppSettings.deleteOffers(offersviewList[index].offer_code); if(deleteOfferStatus){ getOffersViewData(); AppSettings.longSuccessToast('Offer In_Active Successfully!!'); Navigator.of(context).pop(true); } else{ AppSettings.longFailedToast('Offer In_Active failed'); } }, child: const Text('Yes', style: TextStyle( color: primaryColor, fontSize: 18, )), ), TextButton( onPressed: () { Navigator.of(context).pop(true); }, child: const Text('No', style: TextStyle( color: primaryColor, fontSize: 18, )), ), ], ), ); }, ),) ], ), ), ); }) ), Padding( padding: EdgeInsets.fromLTRB(8, 8, 8, 8), child: CircleAvatar( backgroundColor: primaryColor, radius: 40, child: Column( mainAxisSize: MainAxisSize.min, children: [ IconButton( iconSize: 40, icon: const Icon( Icons.add, color: Colors.white, ), onPressed: () async{ Navigator.pop(context); await Navigator.push( context, MaterialPageRoute( builder: (context) => offers()), ); //showBoreAddingDialog(); }, ), /* Padding( padding: EdgeInsets.fromLTRB(5, 0, 5, 5), child: Text( 'Add Tanks ', style: TextStyle(color: Colors.white), ), )*/ ], ), ), ), ]); } else{ return Center( child: Padding( padding: EdgeInsets.fromLTRB(0, 40, 0, 0), child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ SizedBox(height: MediaQuery.of(context).size.height * .25,), Text('Click below icon to add new Offer'), SizedBox( height: 20, ), CircleAvatar( backgroundColor: primaryColor, radius: 40, child: IconButton( iconSize: 40, icon: const Icon( Icons.add, color: Colors.white, ), onPressed: () async { Navigator.pop(context); await Navigator.push( context, MaterialPageRoute( builder: (context) => offers()), ); }, ), ) ], ), ) ); } } @override Widget build(BuildContext context) { return SafeArea( child: Scaffold( appBar: AppSettings.appBar('Active OfersView'), body: isLoading?Center( child: CircularProgressIndicator( color: primaryColor, strokeWidth: 5.0, ), ):renderzUi(), )); } }