rluv_client/lib/features/budget/screens/transactions_listview.dart

289 lines
10 KiB
Dart
Raw Normal View History

2023-07-22 21:29:32 -06:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:rluv/features/budget/widgets/transaction_list_item.dart';
import 'package:rluv/global/styles.dart';
2023-08-17 13:34:30 -06:00
import 'package:rluv/models/transaction_model.dart';
2023-07-22 21:29:32 -06:00
import '../../../global/store.dart';
2023-08-17 13:34:30 -06:00
import '../../../models/budget_category_model.dart';
2023-07-22 21:29:32 -06:00
class TransactionsListview extends ConsumerStatefulWidget {
const TransactionsListview({super.key});
@override
2024-09-10 15:03:32 -06:00
ConsumerState<TransactionsListview> createState() => _TransactionsListviewState();
2023-07-22 21:29:32 -06:00
}
class _TransactionsListviewState extends ConsumerState<TransactionsListview> {
2023-08-17 13:34:30 -06:00
final scaffoldKey = GlobalKey<ScaffoldState>();
@override
void initState() {
// TODO: implement initState
WidgetsBinding.instance.addPostFrameCallback((_) {
2024-09-10 15:03:32 -06:00
ref.read(selectedTransactionSortProvider.notifier).state = TransactionSort.all;
2023-08-17 13:34:30 -06:00
ref.read(selectedSortDateProvider.notifier).state = SortDate.decending;
});
super.initState();
}
2023-07-22 21:29:32 -06:00
@override
Widget build(BuildContext context) {
2023-08-17 13:34:30 -06:00
final budgetCategories = ref.watch(budgetCategoriesProvider);
if (ref.read(selectedCategory) == null && budgetCategories.isNotEmpty) {
2024-09-10 15:03:32 -06:00
WidgetsBinding.instance
.addPostFrameCallback((_) => ref.read(selectedCategory.notifier).state = budgetCategories.first);
2023-08-17 13:34:30 -06:00
}
final sortType = ref.watch(selectedTransactionSortProvider);
final sortDate = ref.watch(selectedSortDateProvider);
List<Transaction> transactions = [...ref.watch(transactionsProvider)];
// Removes transactions by category
switch (sortType) {
case TransactionSort.income:
transactions = transactions
.where(
(element) => element.type == TransactionType.income,
)
.toList();
break;
case TransactionSort.expense:
transactions = transactions
.where(
(element) => element.type == TransactionType.expense,
)
.toList();
break;
case TransactionSort.category:
if (ref.read(selectedCategory) != null) {
transactions = transactions
.where(
2024-09-10 15:03:32 -06:00
(element) => element.budgetCategoryId == ref.read(selectedCategory)!.id,
2023-08-17 13:34:30 -06:00
)
.toList();
}
break;
case TransactionSort.all:
break;
}
// Sorts transactions by date
switch (sortDate) {
case SortDate.ascending:
transactions.sort((a, b) => a.date.compareTo(b.date));
break;
case SortDate.decending:
transactions.sort((a, b) => b.date.compareTo(a.date));
break;
}
2024-09-10 15:03:32 -06:00
WidgetsBinding.instance
.addPostFrameCallback((_) => ref.read(transactionHistoryListProvider.notifier).state = transactions);
2023-07-22 21:29:32 -06:00
return Scaffold(
2023-08-17 13:34:30 -06:00
endDrawer: Drawer(
backgroundColor: Styles.purpleNurple,
child: SafeArea(
child: Column(
children: [
2024-09-10 15:03:32 -06:00
SizedBox(height: MediaQuery.of(context).size.height * 0.15),
2023-08-17 13:34:30 -06:00
DropdownButton<TransactionSort>(
dropdownColor: Styles.lavender,
value: ref.watch(selectedTransactionSortProvider),
items: TransactionSort.values
.map(
(e) => DropdownMenuItem(
value: e,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
sortTypeToIcon(e),
const SizedBox(width: 12),
Text(e.name),
],
),
),
)
.toList(),
onChanged: (TransactionSort? value) {
if (value != null) {
2024-09-10 15:03:32 -06:00
ref.read(selectedTransactionSortProvider.notifier).state = value;
2023-08-17 13:34:30 -06:00
}
}),
const SizedBox(height: 20),
DropdownButton<SortDate>(
dropdownColor: Styles.lavender,
value: ref.watch(selectedSortDateProvider),
items: SortDate.values
.map(
(e) => DropdownMenuItem(
value: e,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
sortDateToIcon(e),
const SizedBox(width: 12),
Text(e.name),
],
),
),
)
.toList(),
onChanged: (SortDate? value) {
if (value != null) {
ref.read(selectedSortDateProvider.notifier).state = value;
}
}),
const SizedBox(height: 20),
2024-09-10 15:03:32 -06:00
if (ref.read(selectedTransactionSortProvider) == TransactionSort.category)
2023-08-17 13:34:30 -06:00
DropdownButton<BudgetCategory>(
dropdownColor: Styles.lavender,
value: ref.watch(selectedCategory),
items: budgetCategories
.map(
(e) => DropdownMenuItem(
value: e,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 18,
width: 18,
decoration: BoxDecoration(
shape: BoxShape.circle,
2024-09-10 15:03:32 -06:00
border: Border.all(color: Colors.black, width: 1.5),
2023-08-17 13:34:30 -06:00
color: e.color,
),
),
const SizedBox(width: 12),
Text(e.name),
],
),
),
)
.toList(),
onChanged: (BudgetCategory? value) {
if (value != null) {
ref.read(selectedCategory.notifier).state = value;
}
},
),
],
),
),
),
key: scaffoldKey,
2023-07-22 21:29:32 -06:00
backgroundColor: Styles.purpleNurple,
body: SafeArea(
child: Stack(
children: [
if (transactions.isEmpty) ...[
2023-08-17 13:34:30 -06:00
Align(
alignment: Alignment.topLeft,
child: Padding(
padding: const EdgeInsets.only(left: 8.0, top: 16.0),
2024-09-10 15:03:32 -06:00
child: IconButton(icon: const Icon(Icons.chevron_left), onPressed: () => Navigator.pop(context)),
2023-08-17 13:34:30 -06:00
),
),
2023-07-22 21:29:32 -06:00
const Center(
child: Text('No transactions'),
),
],
if (transactions.isNotEmpty)
Column(
children: [
2023-08-17 13:34:30 -06:00
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Padding(
padding: const EdgeInsets.only(left: 8.0, top: 16.0),
2024-09-10 15:03:32 -06:00
child: IconButton(icon: const Icon(Icons.chevron_left), onPressed: () => Navigator.pop(context)),
2023-08-17 13:34:30 -06:00
),
const Spacer(),
const Padding(
2024-09-10 15:03:32 -06:00
padding: EdgeInsets.symmetric(vertical: 18.0, horizontal: 0.0),
2023-08-17 13:34:30 -06:00
child: Text(
'Transaction History',
style: TextStyle(fontSize: 28),
textAlign: TextAlign.center,
),
),
const Spacer(),
Padding(
padding: const EdgeInsets.only(left: 8.0, top: 16.0),
child: IconButton(
icon: const Icon(Icons.sort),
onPressed: () {
toggleDrawer();
}),
),
]),
2023-07-22 21:29:32 -06:00
Expanded(
child: ListView.builder(
physics: const BouncingScrollPhysics(),
itemCount: transactions.length,
itemBuilder: (BuildContext context, int index) {
return TransactionListItem(
index: index,
);
},
),
),
],
),
],
),
),
);
}
2023-08-17 13:34:30 -06:00
void toggleDrawer() {
2024-09-10 15:03:32 -06:00
if (scaffoldKey.currentState != null && scaffoldKey.currentState!.isDrawerOpen) {
2023-08-17 13:34:30 -06:00
scaffoldKey.currentState!.openDrawer();
} else if (scaffoldKey.currentState != null) {
scaffoldKey.currentState!.openEndDrawer();
}
}
}
enum TransactionSort {
all,
income,
expense,
category,
}
enum SortDate {
decending,
ascending,
}
Icon sortTypeToIcon(TransactionSort s) {
switch (s) {
case TransactionSort.all:
return const Icon(Icons.donut_large);
case TransactionSort.income:
return const Icon(Icons.attach_money);
case TransactionSort.expense:
return const Icon(Icons.shopping_bag);
case TransactionSort.category:
return const Icon(Icons.sort);
}
2023-07-22 21:29:32 -06:00
}
2023-08-17 13:34:30 -06:00
Icon sortDateToIcon(SortDate s) {
switch (s) {
case SortDate.ascending:
return const Icon(Icons.arrow_circle_up);
case SortDate.decending:
return const Icon(Icons.arrow_circle_down);
}
}
final selectedTransactionSortProvider = StateProvider<TransactionSort>(
(ref) => TransactionSort.all,
);
2024-09-10 15:03:32 -06:00
final selectedSortDateProvider = StateProvider<SortDate>((ref) => SortDate.decending);
2023-08-17 13:34:30 -06:00
final transactionHistoryListProvider = StateProvider<List<Transaction>>(
(ref) => [],
);
final selectedCategory = StateProvider<BudgetCategory?>((ref) => null);