From 6c0f325a44783939f68440335ff0e0082e1eb25b Mon Sep 17 00:00:00 2001 From: Manish Zure Date: Mon, 27 Oct 2025 15:40:44 +0530 Subject: [PATCH] implementation of payment feature --- lib/view/payment/payment_screen.dart | 120 ++++++++++++++++++--------- 1 file changed, 79 insertions(+), 41 deletions(-) diff --git a/lib/view/payment/payment_screen.dart b/lib/view/payment/payment_screen.dart index bd45e43..785df19 100644 --- a/lib/view/payment/payment_screen.dart +++ b/lib/view/payment/payment_screen.dart @@ -3,7 +3,7 @@ import 'package:provider/provider.dart'; import 'package:marco/controller/payment/payment_controller.dart'; import 'package:get/get.dart'; -class PaymentScreen extends StatelessWidget { +class PaymentScreen extends StatefulWidget { final double amount; final String description; @@ -13,14 +13,57 @@ class PaymentScreen extends StatelessWidget { this.description = "No description", }); + @override + State createState() => _PaymentScreenState(); +} + +class _PaymentScreenState extends State { + PaymentController? _controller; + bool _controllerOwned = false; // true if we created it locally + + @override + void initState() { + super.initState(); + // don't initialize controller here — we need context to try Provider.of() + } + + @override + void didChangeDependencies() { + super.didChangeDependencies(); + if (_controller == null) { + // Try to get from Provider (if app provides one) + try { + // If a provider exists, this returns it; if not, it throws. + _controller = Provider.of(context, listen: false); + _controllerOwned = false; + } catch (e) { + // No provider: create our own controller and mark ownership + _controller = PaymentController(); + _controllerOwned = true; + } + } + } + + @override + void dispose() { + // If we created the controller, dispose its resources (Razorpay listeners) + if (_controllerOwned) { + try { + _controller?.disposeController(); + } catch (_) {} + } + super.dispose(); + } + @override Widget build(BuildContext context) { + // Accept arguments from Get.arguments fallback to widget fields final args = (Get.arguments ?? {}) as Map; - final double finalAmount = args['amount'] ?? amount; - final String finalDescription = args['description'] ?? description; - - final paymentController = Provider.of(context); + final double finalAmount = (args['amount'] ?? widget.amount).toDouble(); + final String finalDescription = args['description'] ?? widget.description; + final String orderId = args['orderId'] ?? ''; + final controller = _controller!; return Scaffold( appBar: AppBar( title: const Text("Payment"), @@ -32,45 +75,40 @@ class PaymentScreen extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: [ - Text( - "Description: $finalDescription", - style: const TextStyle(fontSize: 18), - ), + if (orderId.isNotEmpty) + Text("Order ID: $orderId", style: const TextStyle(fontSize: 18)), + if (orderId.isNotEmpty) const SizedBox(height: 8), + Text("Description: $finalDescription", + style: const TextStyle(fontSize: 18)), const SizedBox(height: 8), - Text( - "Amount: ₹${finalAmount.toStringAsFixed(2)}", - style: const TextStyle( - fontSize: 22, - fontWeight: FontWeight.bold, - ), - ), + Text("Amount: ₹${finalAmount.toStringAsFixed(2)}", + style: const TextStyle( + fontSize: 22, + fontWeight: FontWeight.bold, + )), const SizedBox(height: 40), - - // ✅ Show loader when payment processing Center( - child: paymentController.isProcessing - ? const CircularProgressIndicator() - : ElevatedButton( - onPressed: () async { - await paymentController.startPayment( - amount: finalAmount, - description: finalDescription, - context: context, - ); - }, - style: ElevatedButton.styleFrom( - padding: const EdgeInsets.symmetric( - horizontal: 40, vertical: 15), - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(12), - ), - backgroundColor: Colors.green, - ), - child: const Text( - "Pay Now", - style: TextStyle(fontSize: 18, color: Colors.white), - ), - ), + child: ElevatedButton( + onPressed: () async { + await controller.startPayment( + amount: finalAmount, + description: finalDescription, + context: context, + ); + }, + style: ElevatedButton.styleFrom( + padding: + const EdgeInsets.symmetric(horizontal: 40, vertical: 15), + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(12), + ), + backgroundColor: Colors.green, + ), + child: const Text( + "Pay Now", + style: TextStyle(fontSize: 18, color: Colors.white), + ), + ), ), ], ),