marco.pms.mobileapp/lib/view/expense/expense_screen.dart
Vaibhav Surve af83d66390 feat: Add expense models and update expense detail screen
- Created ExpenseModel, Project, ExpenseType, PaymentMode, PaidBy, CreatedBy, and Status classes for expense management.
- Implemented JSON serialization and deserialization for expense models.
- Added ExpenseStatusModel and ExpenseTypeModel for handling status and type of expenses.
- Introduced PaymentModeModel for managing payment modes.
- Refactored ExpenseDetailScreen to utilize the new ExpenseModel structure.
- Enhanced UI components for better display of expense details.
- Added search and filter functionality in ExpenseMainScreen.
- Updated dependencies in pubspec.yaml to include geocoding package.
2025-07-25 10:45:21 +05:30

464 lines
15 KiB
Dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:marco/controller/project_controller.dart';
import 'package:marco/controller/expense/expense_screen_controller.dart';
import 'package:marco/helpers/utils/date_time_utils.dart';
import 'package:marco/helpers/widgets/my_spacing.dart';
import 'package:marco/helpers/widgets/my_text.dart';
import 'package:marco/model/expense/expense_list_model.dart';
import 'package:marco/view/expense/expense_detail_screen.dart';
import 'package:marco/model/expense/add_expense_bottom_sheet.dart';
class ExpenseMainScreen extends StatefulWidget {
const ExpenseMainScreen({super.key});
@override
State<ExpenseMainScreen> createState() => _ExpenseMainScreenState();
}
class _ExpenseMainScreenState extends State<ExpenseMainScreen> {
final RxBool isHistoryView = false.obs;
final TextEditingController searchController = TextEditingController();
final RxString searchQuery = ''.obs;
final ProjectController projectController = Get.find<ProjectController>();
final ExpenseController expenseController = Get.put(ExpenseController());
@override
void initState() {
super.initState();
expenseController.fetchExpenses(); // Load expenses from API
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: _ExpenseAppBar(projectController: projectController),
body: SafeArea(
child: Column(
children: [
_SearchAndFilter(
searchController: searchController,
onChanged: (value) => searchQuery.value = value,
onFilterTap: _openFilterBottomSheet,
),
_ToggleButtons(isHistoryView: isHistoryView),
Expanded(
child: Obx(() {
if (expenseController.isLoading.value) {
return const Center(child: CircularProgressIndicator());
}
if (expenseController.errorMessage.isNotEmpty) {
return Center(
child: Text(
expenseController.errorMessage.value,
style: const TextStyle(color: Colors.red),
),
);
}
if (expenseController.expenses.isEmpty) {
return const Center(child: Text("No expenses found."));
}
// Apply search filter
final filteredList = expenseController.expenses.where((expense) {
final query = searchQuery.value.toLowerCase();
return query.isEmpty ||
expense.expensesType.name.toLowerCase().contains(query) ||
expense.supplerName.toLowerCase().contains(query) ||
expense.paymentMode.name.toLowerCase().contains(query);
}).toList();
// Split into current month and history
final now = DateTime.now();
final currentMonthList = filteredList.where((e) =>
e.transactionDate.month == now.month &&
e.transactionDate.year == now.year).toList();
final historyList = filteredList.where((e) =>
e.transactionDate.isBefore(
DateTime(now.year, now.month, 1))).toList();
final listToShow =
isHistoryView.value ? historyList : currentMonthList;
return _ExpenseList(expenseList: listToShow);
}),
),
],
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.red,
onPressed: showAddExpenseBottomSheet,
child: const Icon(Icons.add, color: Colors.white),
),
);
}
void _openFilterBottomSheet() {
Get.bottomSheet(
Container(
padding: const EdgeInsets.all(16),
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
),
child: Wrap(
runSpacing: 10,
children: [
const Text(
'Filter Expenses',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
ListTile(
leading: const Icon(Icons.date_range),
title: const Text('Date Range'),
onTap: () {},
),
ListTile(
leading: const Icon(Icons.work_outline),
title: const Text('Project'),
onTap: () {},
),
ListTile(
leading: const Icon(Icons.check_circle_outline),
title: const Text('Status'),
onTap: () {},
),
],
),
),
);
}
}
// AppBar Widget
class _ExpenseAppBar extends StatelessWidget implements PreferredSizeWidget {
final ProjectController projectController;
const _ExpenseAppBar({required this.projectController});
@override
Size get preferredSize => const Size.fromHeight(72);
@override
Widget build(BuildContext context) {
return PreferredSize(
preferredSize: preferredSize,
child: AppBar(
backgroundColor: const Color(0xFFF5F5F5),
elevation: 0.5,
automaticallyImplyLeading: false,
titleSpacing: 0,
title: Padding(
padding: MySpacing.xy(16, 0),
child: Row(
children: [
IconButton(
icon: const Icon(Icons.arrow_back_ios_new,
color: Colors.black, size: 20),
onPressed: () => Get.offNamed('/dashboard'),
),
MySpacing.width(8),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
MyText.titleLarge(
'Expenses',
fontWeight: 700,
color: Colors.black,
),
MySpacing.height(2),
GetBuilder<ProjectController>(
builder: (_) {
final projectName =
projectController.selectedProject?.name ??
'Select Project';
return InkWell(
onTap: () => Get.toNamed('/project-selector'),
child: Row(
children: [
const Icon(Icons.work_outline,
size: 14, color: Colors.grey),
MySpacing.width(4),
Expanded(
child: MyText.bodySmall(
projectName,
fontWeight: 600,
overflow: TextOverflow.ellipsis,
color: Colors.grey[700],
),
),
],
),
);
},
)
],
),
),
],
),
),
),
);
}
}
// Search and Filter Widget
class _SearchAndFilter extends StatelessWidget {
final TextEditingController searchController;
final ValueChanged<String> onChanged;
final VoidCallback onFilterTap;
const _SearchAndFilter({
required this.searchController,
required this.onChanged,
required this.onFilterTap,
});
@override
Widget build(BuildContext context) {
return Padding(
padding: MySpacing.fromLTRB(12, 10, 12, 0),
child: Row(
children: [
Expanded(
child: SizedBox(
height: 35,
child: TextField(
controller: searchController,
onChanged: onChanged,
decoration: InputDecoration(
contentPadding:
const EdgeInsets.symmetric(horizontal: 12),
prefixIcon:
const Icon(Icons.search, size: 20, color: Colors.grey),
hintText: 'Search expenses...',
filled: true,
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(color: Colors.grey.shade300),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(color: Colors.grey.shade300),
),
),
),
),
),
MySpacing.width(8),
IconButton(
icon: const Icon(Icons.tune, color: Colors.black),
onPressed: onFilterTap,
),
],
),
);
}
}
// Toggle Buttons Widget
class _ToggleButtons extends StatelessWidget {
final RxBool isHistoryView;
const _ToggleButtons({required this.isHistoryView});
@override
Widget build(BuildContext context) {
return Padding(
padding: MySpacing.fromLTRB(8, 12, 8, 5),
child: Obx(() {
return Container(
padding: const EdgeInsets.all(2),
decoration: BoxDecoration(
color: const Color(0xFFF0F0F0),
borderRadius: BorderRadius.circular(10),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
blurRadius: 4,
offset: const Offset(0, 2),
),
],
),
child: Row(
children: [
_ToggleButton(
label: 'Expenses',
icon: Icons.receipt_long,
selected: !isHistoryView.value,
onTap: () => isHistoryView.value = false,
),
_ToggleButton(
label: 'History',
icon: Icons.history,
selected: isHistoryView.value,
onTap: () => isHistoryView.value = true,
),
],
),
);
}),
);
}
}
class _ToggleButton extends StatelessWidget {
final String label;
final IconData icon;
final bool selected;
final VoidCallback onTap;
const _ToggleButton({
required this.label,
required this.icon,
required this.selected,
required this.onTap,
});
@override
Widget build(BuildContext context) {
return Expanded(
child: GestureDetector(
onTap: onTap,
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 10),
decoration: BoxDecoration(
color: selected ? Colors.red : Colors.transparent,
borderRadius: BorderRadius.circular(8),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(icon, size: 16, color: selected ? Colors.white : Colors.grey),
const SizedBox(width: 6),
Text(
label,
style: TextStyle(
color: selected ? Colors.white : Colors.grey,
fontWeight: FontWeight.w600,
fontSize: 13,
),
),
],
),
),
),
);
}
}
// Expense List Widget (Dynamic)
class _ExpenseList extends StatelessWidget {
final List<ExpenseModel> expenseList;
const _ExpenseList({required this.expenseList});
static Color _getStatusColor(String status) {
switch (status) {
case 'Requested': return Colors.blue;
case 'Review': return Colors.orange;
case 'Approved': return Colors.green;
case 'Paid': return Colors.purple;
case 'Closed': return Colors.grey;
default: return Colors.black;
}
}
@override
Widget build(BuildContext context) {
if (expenseList.isEmpty) {
return const Center(child: Text('No expenses found.'));
}
return ListView.separated(
padding: const EdgeInsets.all(12),
itemCount: expenseList.length,
separatorBuilder: (_, __) =>
Divider(color: Colors.grey.shade300, height: 20),
itemBuilder: (context, index) {
final expense = expenseList[index];
final statusColor = _getStatusColor(expense.status.name);
// Convert UTC date to local formatted string
final formattedDate = DateTimeUtils.convertUtcToLocal(
expense.transactionDate.toIso8601String(),
format: 'dd MMM yyyy, hh:mm a',
);
return GestureDetector(
onTap: () => Get.to(
() => const ExpenseDetailScreen(),
arguments: {'expense': expense},
),
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Title + Amount row
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
const Icon(Icons.receipt_long,
size: 20, color: Colors.red),
const SizedBox(width: 8),
Text(
expense.expensesType.name,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
],
),
Text(
'${expense.amount.toStringAsFixed(2)}',
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.red,
),
),
],
),
const SizedBox(height: 6),
// Date + Status
Row(
children: [
Text(
formattedDate,
style: TextStyle(fontSize: 12, color: Colors.grey[600]),
),
const Spacer(),
Text(
expense.status.name,
style: TextStyle(
color: statusColor,
fontWeight: FontWeight.bold,
fontSize: 12,
),
),
],
),
],
),
),
);
},
);
}
}