implementation of payment feature
This commit is contained in:
parent
ff9c712a7e
commit
6c0f325a44
@ -3,7 +3,7 @@ import 'package:provider/provider.dart';
|
|||||||
import 'package:marco/controller/payment/payment_controller.dart';
|
import 'package:marco/controller/payment/payment_controller.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
|
|
||||||
class PaymentScreen extends StatelessWidget {
|
class PaymentScreen extends StatefulWidget {
|
||||||
final double amount;
|
final double amount;
|
||||||
final String description;
|
final String description;
|
||||||
|
|
||||||
@ -13,14 +13,57 @@ class PaymentScreen extends StatelessWidget {
|
|||||||
this.description = "No description",
|
this.description = "No description",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<PaymentScreen> createState() => _PaymentScreenState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _PaymentScreenState extends State<PaymentScreen> {
|
||||||
|
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<PaymentController>(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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
// Accept arguments from Get.arguments fallback to widget fields
|
||||||
final args = (Get.arguments ?? {}) as Map<String, dynamic>;
|
final args = (Get.arguments ?? {}) as Map<String, dynamic>;
|
||||||
final double finalAmount = args['amount'] ?? amount;
|
final double finalAmount = (args['amount'] ?? widget.amount).toDouble();
|
||||||
final String finalDescription = args['description'] ?? description;
|
final String finalDescription = args['description'] ?? widget.description;
|
||||||
|
final String orderId = args['orderId'] ?? '';
|
||||||
final paymentController = Provider.of<PaymentController>(context);
|
|
||||||
|
|
||||||
|
final controller = _controller!;
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text("Payment"),
|
title: const Text("Payment"),
|
||||||
@ -32,45 +75,40 @@ class PaymentScreen extends StatelessWidget {
|
|||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
if (orderId.isNotEmpty)
|
||||||
"Description: $finalDescription",
|
Text("Order ID: $orderId", style: const TextStyle(fontSize: 18)),
|
||||||
style: const TextStyle(fontSize: 18),
|
if (orderId.isNotEmpty) const SizedBox(height: 8),
|
||||||
),
|
Text("Description: $finalDescription",
|
||||||
|
style: const TextStyle(fontSize: 18)),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
Text(
|
Text("Amount: ₹${finalAmount.toStringAsFixed(2)}",
|
||||||
"Amount: ₹${finalAmount.toStringAsFixed(2)}",
|
style: const TextStyle(
|
||||||
style: const TextStyle(
|
fontSize: 22,
|
||||||
fontSize: 22,
|
fontWeight: FontWeight.bold,
|
||||||
fontWeight: FontWeight.bold,
|
)),
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 40),
|
const SizedBox(height: 40),
|
||||||
|
|
||||||
// ✅ Show loader when payment processing
|
|
||||||
Center(
|
Center(
|
||||||
child: paymentController.isProcessing
|
child: ElevatedButton(
|
||||||
? const CircularProgressIndicator()
|
onPressed: () async {
|
||||||
: ElevatedButton(
|
await controller.startPayment(
|
||||||
onPressed: () async {
|
amount: finalAmount,
|
||||||
await paymentController.startPayment(
|
description: finalDescription,
|
||||||
amount: finalAmount,
|
context: context,
|
||||||
description: finalDescription,
|
);
|
||||||
context: context,
|
},
|
||||||
);
|
style: ElevatedButton.styleFrom(
|
||||||
},
|
padding:
|
||||||
style: ElevatedButton.styleFrom(
|
const EdgeInsets.symmetric(horizontal: 40, vertical: 15),
|
||||||
padding: const EdgeInsets.symmetric(
|
shape: RoundedRectangleBorder(
|
||||||
horizontal: 40, vertical: 15),
|
borderRadius: BorderRadius.circular(12),
|
||||||
shape: RoundedRectangleBorder(
|
),
|
||||||
borderRadius: BorderRadius.circular(12),
|
backgroundColor: Colors.green,
|
||||||
),
|
),
|
||||||
backgroundColor: Colors.green,
|
child: const Text(
|
||||||
),
|
"Pay Now",
|
||||||
child: const Text(
|
style: TextStyle(fontSize: 18, color: Colors.white),
|
||||||
"Pay Now",
|
),
|
||||||
style: TextStyle(fontSize: 18, color: Colors.white),
|
),
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user