Files
grocery_user_app-frontend/lib/src/ui/coupons/coupons_screen.dart
2025-05-10 13:09:19 +05:30

351 lines
17 KiB
Dart

import 'dart:math';
import 'dart:ui';
import 'package:blur/blur.dart';
import 'package:flutter/material.dart';
import 'package:grocery_app/src/logic/provider/addTocart_provider.dart';
import 'package:grocery_app/src/logic/provider/home_provider.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
class CouponsScreen extends StatelessWidget {
String cartId;
CouponsScreen({super.key, required this.cartId});
TextEditingController inpucode = TextEditingController();
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
Provider.of<AddtocartProvider>(context, listen: false).offerCoupon(context);
print("kldfjgdfkljgdf ${cartId}");
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
title: Text("Coupons"),
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: () => Navigator.pop(context),
),
),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
children: [
Form(
key: _formKey,
child: Consumer<AddtocartProvider>(
builder: (context, provider, child) {
return TextFormField(
controller: inpucode,
cursorHeight: 20,
readOnly: false,
decoration: InputDecoration(
hintText: "Enter Coupon Code",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
suffixIcon: Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () async {
if (_formKey.currentState!.validate()) {
var status = await provider.applyCoupon(
context, cartId, inpucode.text, '');
if (status) {
Navigator.pop(context);
}
}
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text("Apply"),
),
),
),
validator: (value) {
if (value == null || value.trim().isEmpty) {
return "Please enter a coupon code";
}
return null;
},
);
},
),
),
SizedBox(height: 16),
Consumer<AddtocartProvider>(builder: (context, provider, child) {
if (provider.iscouponLoading) {
return Center(child: CircularProgressIndicator());
} else if (provider.couponDataModel.data!.isEmpty) {
return SizedBox.shrink();
} else {
return Expanded(
child: ListView.builder(
itemCount: provider.couponDataModel.data!.length,
itemBuilder: (context, index) {
final coupon = provider.couponDataModel.data![index];
DateTime couponEndDate =
DateTime.parse(coupon.endDate.toString()).toUtc();
DateTime now = DateTime.now().toUtc();
bool isCouponValid = now.isBefore(couponEndDate);
print(
"Coupon Expiry: ${coupon.endDate} ${coupon.imageUrl}| Current UTC Time: $now | Valid: $isCouponValid");
return Card(
color: Colors.transparent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
elevation: 4,
child: ClipRRect(
borderRadius: BorderRadius.circular(12),
child: Stack(
children: [
Positioned.fill(
child: Image.network(
coupon.imageUrl,
fit: BoxFit.cover,
),
),
Positioned.fill(
child: BackdropFilter(
filter:
ImageFilter.blur(sigmaX: 5, sigmaY: 5),
child: Container(
color: Colors.black.withOpacity(0.3),
),
),
),
Padding(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
coupon.description ?? '',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
SizedBox(height: 5),
Row(
children: [
Text(
"Expired On : ",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
Text(
DateFormat("dd-MM-yyyy").format(
DateTime.parse(
coupon.endDate.toString())),
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
SizedBox(height: 5),
Text(
coupon.type == "PERCENTAGE"
? "${coupon.discountValue ?? ''}%"
: "${coupon.discountValue ?? ''}",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.amber,
),
),
SizedBox(height: 5),
Text(
coupon.terms ?? "",
style: TextStyle(color: Colors.white70),
),
SizedBox(height: 10),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Container(
padding: EdgeInsets.symmetric(
horizontal: 10, vertical: 5),
decoration: BoxDecoration(
color:
Colors.white.withOpacity(0.2),
border:
Border.all(color: Colors.green),
borderRadius:
BorderRadius.circular(8),
),
child: Text(
coupon.code!,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
ElevatedButton(
onPressed: isCouponValid
? () async
{
var status = await provider
.applyCoupon(
context,
cartId,
coupon.code,
coupon.id);
if (status)
{
Navigator.pop(context);
}
}
: null, // Disable button if expired
style: ElevatedButton.styleFrom(
backgroundColor: isCouponValid
? Colors.green
: Colors.grey,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(8),
),
),
child: Text("Apply"),
),
],
),
],
),
),
],
),
),
);
// Card(
// color: Colors.transparent.withOpacity(0.4),
// // color: Color((Random().nextDouble() * 0xFFFFFF).toInt())
// // .withOpacity(0.9),
// shape: RoundedRectangleBorder(
// borderRadius: BorderRadius.circular(12),
// ),
// elevation: 2,
// child: Blur(
// blur: 2.5,
// // blurColor: Theme.of(context).primaryColor,
// child: Container(
// decoration: BoxDecoration(
// image: DecorationImage(
// image: NetworkImage(
// coupon.imageUrl), // Your background image
// fit: BoxFit.cover, // Adjust as needed
// ),
// borderRadius: BorderRadius.circular(
// 12), // Match Card's border radius
// ),
// child: Padding(
// padding: EdgeInsets.all(16.0),
// child: Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// Text(
// coupon.description ?? '',
// style: TextStyle(
// fontSize: 16,
// fontWeight: FontWeight.bold,
// ),
// ),
// Text(
// "₹" + coupon.discountValue ?? '',
// style: TextStyle(
// fontSize: 16,
// fontWeight: FontWeight.bold,
// ),
// ),
// SizedBox(height: 5),
// Text(
// coupon.terms ?? "",
// style: TextStyle(color: Colors.grey[600]),
// ),
// SizedBox(height: 10),
// Row(
// mainAxisAlignment:
// MainAxisAlignment.spaceBetween,
// children: [
// Container(
// padding: EdgeInsets.symmetric(
// horizontal: 10, vertical: 5),
// decoration: BoxDecoration(
// border:
// Border.all(color: Colors.green),
// borderRadius:
// BorderRadius.circular(8),
// ),
// child: Text(
// coupon.code!,
// style: TextStyle(
// fontWeight: FontWeight.bold,
// color: Colors.green,
// ),
// ),
// ),
// ElevatedButton(
// onPressed: isCouponValid
// ? () async {
// var status =
// await provider.applyCoupon(
// context,
// cartId,
// coupon.code,
// coupon.id);
// if (status) {
// Navigator.pop(context);
// }
// }
// : null, // Disables button if expired
// style: ElevatedButton.styleFrom(
// backgroundColor: isCouponValid
// ? Colors.green
// : Colors.grey,
// shape: RoundedRectangleBorder(
// borderRadius:
// BorderRadius.circular(8),
// ),
// ),
// child: Text("Apply"),
// )
// ],
// )
// ],
// ),
// ),
// ),
// ),
// );
},
),
);
}
}),
],
),
),
);
}
}