marco.pms.mobileapp/lib/view/payment/payment_screen.dart

130 lines
4.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:marco/controller/payment/payment_controller.dart';
import 'package:get/get.dart';
class PaymentScreen extends StatefulWidget {
final double amount;
final String description;
const PaymentScreen({
super.key,
this.amount = 0.0,
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
Widget build(BuildContext context) {
// Accept arguments from Get.arguments fallback to widget fields
final args = (Get.arguments ?? {}) as Map<String, dynamic>;
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",
style: TextStyle(color: Colors.black),
),
backgroundColor: Colors.white,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
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,
)),
const SizedBox(height: 40),
Center(
child: ElevatedButton(
onPressed: () async {
// Extract optional IDs passed via Get.arguments
final String tenantEnquireId =
(args['tenantEnquireId'] ?? '') as String;
final String planId = (args['planId'] ?? '') as String;
await controller.startPayment(
amount: finalAmount,
description: finalDescription,
context: context,
tenantEnquireId:
tenantEnquireId.isNotEmpty ? tenantEnquireId : null,
planId: planId.isNotEmpty ? planId : null,
);
},
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),
),
),
),
],
),
),
);
}
}