feat: Enhance ExpenseByStatusWidget with navigation and filter functionality; update ExpenseMainScreen to fetch expenses after UI initialization
This commit is contained in:
parent
1e39210a29
commit
d62f0d2c60
@ -4,6 +4,9 @@ import 'package:marco/controller/dashboard/dashboard_controller.dart';
|
|||||||
import 'package:marco/helpers/widgets/my_text.dart';
|
import 'package:marco/helpers/widgets/my_text.dart';
|
||||||
import 'package:marco/helpers/utils/utils.dart';
|
import 'package:marco/helpers/utils/utils.dart';
|
||||||
import 'package:marco/helpers/widgets/my_custom_skeleton.dart';
|
import 'package:marco/helpers/widgets/my_custom_skeleton.dart';
|
||||||
|
import 'package:marco/controller/expense/expense_screen_controller.dart';
|
||||||
|
import 'package:marco/view/expense/expense_screen.dart';
|
||||||
|
import 'package:collection/collection.dart';
|
||||||
|
|
||||||
class ExpenseByStatusWidget extends StatelessWidget {
|
class ExpenseByStatusWidget extends StatelessWidget {
|
||||||
final DashboardController controller;
|
final DashboardController controller;
|
||||||
@ -16,34 +19,106 @@ class ExpenseByStatusWidget extends StatelessWidget {
|
|||||||
required String title,
|
required String title,
|
||||||
required String amount,
|
required String amount,
|
||||||
required String count,
|
required String count,
|
||||||
|
required VoidCallback onTap,
|
||||||
}) {
|
}) {
|
||||||
return Padding(
|
return InkWell(
|
||||||
padding: const EdgeInsets.symmetric(vertical: 6),
|
onTap: onTap,
|
||||||
child: Row(
|
borderRadius: BorderRadius.circular(8),
|
||||||
children: [
|
child: Padding(
|
||||||
CircleAvatar(
|
padding: const EdgeInsets.symmetric(vertical: 6),
|
||||||
backgroundColor: color.withOpacity(0.15),
|
child: Row(
|
||||||
radius: 22,
|
children: [
|
||||||
child: Icon(icon, color: color, size: 24),
|
CircleAvatar(
|
||||||
),
|
backgroundColor: color.withOpacity(0.15),
|
||||||
const SizedBox(width: 12),
|
radius: 22,
|
||||||
Expanded(
|
child: Icon(icon, color: color, size: 24),
|
||||||
child: Column(
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
|
||||||
children: [
|
|
||||||
MyText.bodyMedium(title, fontWeight: 600),
|
|
||||||
const SizedBox(height: 2),
|
|
||||||
MyText.titleMedium(amount, color: Colors.blue, fontWeight: 700),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
),
|
const SizedBox(width: 12),
|
||||||
MyText.titleMedium(count, color: Colors.blue, fontWeight: 700),
|
Expanded(
|
||||||
const Icon(Icons.chevron_right, color: Colors.blue, size: 24),
|
child: Column(
|
||||||
],
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
MyText.bodyMedium(title, fontWeight: 600),
|
||||||
|
const SizedBox(height: 2),
|
||||||
|
MyText.titleMedium(amount,
|
||||||
|
color: Colors.blue, fontWeight: 700),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
MyText.titleMedium(count, color: Colors.blue, fontWeight: 700),
|
||||||
|
const Icon(Icons.chevron_right, color: Colors.blue, size: 24),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Navigate with status filter
|
||||||
|
Future<void> _navigateToExpenseWithFilter(
|
||||||
|
BuildContext context, String statusName) async {
|
||||||
|
final expenseController = Get.put(ExpenseController());
|
||||||
|
|
||||||
|
// 1️⃣ Ensure global projects and master data are loaded
|
||||||
|
if (expenseController.projectsMap.isEmpty) {
|
||||||
|
await expenseController.fetchGlobalProjects();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (expenseController.expenseStatuses.isEmpty) {
|
||||||
|
await expenseController.fetchMasterData();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2️⃣ Auto-select current project from DashboardController
|
||||||
|
final dashboardController = Get.find<DashboardController>();
|
||||||
|
final currentProjectId =
|
||||||
|
dashboardController.projectController.selectedProjectId.value;
|
||||||
|
|
||||||
|
final projectName = expenseController.projectsMap.entries
|
||||||
|
.firstWhereOrNull((entry) => entry.value == currentProjectId)
|
||||||
|
?.key;
|
||||||
|
|
||||||
|
expenseController.selectedProject.value = projectName ?? '';
|
||||||
|
|
||||||
|
// 3️⃣ Select status filter
|
||||||
|
final matchedStatus = expenseController.expenseStatuses.firstWhereOrNull(
|
||||||
|
(e) => e.name.toLowerCase() == statusName.toLowerCase(),
|
||||||
|
);
|
||||||
|
expenseController.selectedStatus.value = matchedStatus?.id ?? '';
|
||||||
|
|
||||||
|
// 4️⃣ Fetch expenses immediately with applied filters
|
||||||
|
await expenseController.fetchExpenses();
|
||||||
|
|
||||||
|
// 5️⃣ Navigate to Expense screen
|
||||||
|
Get.to(() => const ExpenseMainScreen());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Navigate without status filter
|
||||||
|
Future<void> _navigateToExpenseWithoutFilter() async {
|
||||||
|
final expenseController = Get.put(ExpenseController());
|
||||||
|
|
||||||
|
// Ensure global projects loaded
|
||||||
|
if (expenseController.projectsMap.isEmpty) {
|
||||||
|
await expenseController.fetchGlobalProjects();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auto-select current project
|
||||||
|
final dashboardController = Get.find<DashboardController>();
|
||||||
|
final currentProjectId =
|
||||||
|
dashboardController.projectController.selectedProjectId.value;
|
||||||
|
|
||||||
|
final projectName = expenseController.projectsMap.entries
|
||||||
|
.firstWhereOrNull((entry) => entry.value == currentProjectId)
|
||||||
|
?.key;
|
||||||
|
|
||||||
|
expenseController.selectedProject.value = projectName ?? '';
|
||||||
|
expenseController.selectedStatus.value = '';
|
||||||
|
|
||||||
|
// Fetch expenses with project filter (no status)
|
||||||
|
await expenseController.fetchExpenses();
|
||||||
|
|
||||||
|
// Navigate to Expense screen
|
||||||
|
Get.to(() => const ExpenseMainScreen());
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Obx(() {
|
return Obx(() {
|
||||||
@ -78,12 +153,17 @@ class ExpenseByStatusWidget extends StatelessWidget {
|
|||||||
children: [
|
children: [
|
||||||
MyText.titleMedium("Expense - By Status", fontWeight: 700),
|
MyText.titleMedium("Expense - By Status", fontWeight: 700),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
|
|
||||||
|
// ✅ Status tiles
|
||||||
_buildStatusTile(
|
_buildStatusTile(
|
||||||
icon: Icons.currency_rupee,
|
icon: Icons.currency_rupee,
|
||||||
color: Colors.blue,
|
color: Colors.blue,
|
||||||
title: "Pending Payment",
|
title: "Pending Payment",
|
||||||
amount: Utils.formatCurrency(data.processPending.totalAmount),
|
amount: Utils.formatCurrency(data.processPending.totalAmount),
|
||||||
count: data.processPending.count.toString(),
|
count: data.processPending.count.toString(),
|
||||||
|
onTap: () {
|
||||||
|
_navigateToExpenseWithFilter(context, 'Payment Pending');
|
||||||
|
},
|
||||||
),
|
),
|
||||||
_buildStatusTile(
|
_buildStatusTile(
|
||||||
icon: Icons.check_circle_outline,
|
icon: Icons.check_circle_outline,
|
||||||
@ -91,6 +171,9 @@ class ExpenseByStatusWidget extends StatelessWidget {
|
|||||||
title: "Pending Approve",
|
title: "Pending Approve",
|
||||||
amount: Utils.formatCurrency(data.approvePending.totalAmount),
|
amount: Utils.formatCurrency(data.approvePending.totalAmount),
|
||||||
count: data.approvePending.count.toString(),
|
count: data.approvePending.count.toString(),
|
||||||
|
onTap: () {
|
||||||
|
_navigateToExpenseWithFilter(context, 'Approval Pending');
|
||||||
|
},
|
||||||
),
|
),
|
||||||
_buildStatusTile(
|
_buildStatusTile(
|
||||||
icon: Icons.search,
|
icon: Icons.search,
|
||||||
@ -98,6 +181,9 @@ class ExpenseByStatusWidget extends StatelessWidget {
|
|||||||
title: "Pending Review",
|
title: "Pending Review",
|
||||||
amount: Utils.formatCurrency(data.reviewPending.totalAmount),
|
amount: Utils.formatCurrency(data.reviewPending.totalAmount),
|
||||||
count: data.reviewPending.count.toString(),
|
count: data.reviewPending.count.toString(),
|
||||||
|
onTap: () {
|
||||||
|
_navigateToExpenseWithFilter(context, 'Review Pending');
|
||||||
|
},
|
||||||
),
|
),
|
||||||
_buildStatusTile(
|
_buildStatusTile(
|
||||||
icon: Icons.insert_drive_file_outlined,
|
icon: Icons.insert_drive_file_outlined,
|
||||||
@ -105,27 +191,48 @@ class ExpenseByStatusWidget extends StatelessWidget {
|
|||||||
title: "Draft",
|
title: "Draft",
|
||||||
amount: Utils.formatCurrency(data.draft.totalAmount),
|
amount: Utils.formatCurrency(data.draft.totalAmount),
|
||||||
count: data.draft.count.toString(),
|
count: data.draft.count.toString(),
|
||||||
|
onTap: () {
|
||||||
|
_navigateToExpenseWithFilter(context, 'Draft');
|
||||||
|
},
|
||||||
),
|
),
|
||||||
|
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
Divider(color: Colors.grey.shade300),
|
Divider(color: Colors.grey.shade300),
|
||||||
const SizedBox(height: 12),
|
const SizedBox(height: 12),
|
||||||
Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
// ✅ Total row tap navigation (no filter)
|
||||||
children: [
|
InkWell(
|
||||||
Column(
|
onTap: _navigateToExpenseWithoutFilter,
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
borderRadius: BorderRadius.circular(8),
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 4),
|
||||||
|
child: Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
children: [
|
children: [
|
||||||
MyText.bodyMedium("Project Spendings:", fontWeight: 600),
|
Column(
|
||||||
MyText.bodySmall("(All Processed Payments)",
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
color: Colors.grey.shade600),
|
children: [
|
||||||
|
MyText.bodyMedium("Project Spendings:",
|
||||||
|
fontWeight: 600),
|
||||||
|
MyText.bodySmall("(All Processed Payments)",
|
||||||
|
color: Colors.grey.shade600),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
MyText.titleLarge(
|
||||||
|
Utils.formatCurrency(data.totalAmount),
|
||||||
|
color: Colors.blue,
|
||||||
|
fontWeight: 700,
|
||||||
|
),
|
||||||
|
const SizedBox(width: 6),
|
||||||
|
const Icon(Icons.chevron_right,
|
||||||
|
color: Colors.blue, size: 22),
|
||||||
|
],
|
||||||
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
MyText.titleLarge(
|
),
|
||||||
"${Utils.formatCurrency(data.totalAmount)} >",
|
|
||||||
color: Colors.blue,
|
|
||||||
fontWeight: 700,
|
|
||||||
)
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
@ -28,12 +28,17 @@ class _ExpenseMainScreenState extends State<ExpenseMainScreen>
|
|||||||
final projectController = Get.find<ProjectController>();
|
final projectController = Get.find<ProjectController>();
|
||||||
final permissionController = Get.find<PermissionController>();
|
final permissionController = Get.find<PermissionController>();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_tabController = TabController(length: 2, vsync: this);
|
_tabController = TabController(length: 2, vsync: this);
|
||||||
|
|
||||||
|
// ✅ Delay fetch until after UI & controller are ready
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
final expenseController = Get.find<ExpenseController>();
|
||||||
expenseController.fetchExpenses();
|
expenseController.fetchExpenses();
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user