Line data Source code
1 : // Copyright 2023 The terCAD team. All rights reserved.
2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
3 :
4 : import 'package:app_finance/_classes/herald/app_sorting.dart';
5 : import 'package:app_finance/_classes/storage/app_data.dart';
6 : import 'package:app_finance/_classes/herald/app_locale.dart';
7 : import 'package:app_finance/_classes/structure/budget_app_data.dart';
8 : import 'package:app_finance/_classes/structure/currency/exchange.dart';
9 : import 'package:app_finance/_configs/theme_helper.dart';
10 : import 'package:app_finance/_classes/structure/navigation/app_route.dart';
11 : import 'package:app_finance/design/generic/base_header_widget.dart';
12 : import 'package:app_finance/design/generic/base_swipe_widget.dart';
13 : import 'package:app_finance/design/wrapper/background_wrapper.dart';
14 : import 'package:app_finance/design/wrapper/tap_widget.dart';
15 : import 'package:app_finance/pages/_interfaces/abstract_page_state.dart';
16 : import 'package:app_finance/pages/budget/widgets/budget_line_widget.dart';
17 : import 'package:app_finance/pages/budget/widgets/header_widget.dart';
18 : import 'package:material_ui/material_ui.dart';
19 : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
20 : import 'package:provider/provider.dart';
21 :
22 : class BudgetPage extends StatefulWidget {
23 : final String? search;
24 2 : const BudgetPage({
25 : super.key,
26 : this.search,
27 : });
28 :
29 0 : @override
30 0 : BudgetPageState createState() => BudgetPageState();
31 : }
32 :
33 : class BudgetPageState extends AbstractPageState<BudgetPage> {
34 0 : AppDataGetter _getItems() {
35 0 : if (widget.search != null) {
36 : final scope =
37 0 : state.getList(AppDataType.budgets).where((e) => e.title.toString().startsWith(widget.search!)).toList();
38 0 : final ex = Exchange(store: super.state);
39 : return (
40 0 : total: scope.fold(0.0, (v, e) => v + ex.reform(e.details, e.currency, ex.getDefaultCurrency())),
41 : list: scope,
42 0 : stream: state.getStream(AppDataType.budgets, filter: (e) => !e.title.toString().startsWith(widget.search!)),
43 : );
44 : }
45 0 : return state.get(AppDataType.budgets);
46 : }
47 :
48 0 : @override
49 : String getTitle() {
50 0 : if (widget.search != null) {
51 0 : return AppLocale.labels.search(widget.search!);
52 : }
53 0 : return AppLocale.labels.budgetHeadline;
54 : }
55 :
56 0 : @override
57 0 : String getButtonName() => AppLocale.labels.addBudgetTooltip;
58 :
59 0 : @override
60 : Widget buildButton(BuildContext context, BoxConstraints constraints) {
61 0 : NavigatorState nav = Navigator.of(context);
62 0 : return FloatingActionButton(
63 : heroTag: 'budget_page',
64 0 : onPressed: () => nav.pushNamed(AppRoute.budgetAddRoute),
65 0 : tooltip: getButtonName(),
66 : child: const Icon(Icons.add),
67 : );
68 : }
69 :
70 0 : @override
71 : Widget buildContent(BuildContext context, BoxConstraints constraints) {
72 0 : final items = _getItems();
73 0 : final sorting = Provider.of<AppSorting>(context, listen: true);
74 0 : items.list.sort(sorting.getSortFunction(context));
75 0 : final width = ThemeHelper.getWidth(context, 4, constraints);
76 0 : final indent = ThemeHelper.getIndent();
77 0 : final widthCount = ThemeHelper.getWidthCount(constraints, context);
78 0 : return Padding(
79 0 : padding: EdgeInsets.all(indent),
80 0 : child: Column(
81 0 : children: [
82 0 : BaseHeaderWidget(
83 : route: AppRoute.homeRoute,
84 0 : tooltip: AppLocale.labels.homeTooltip,
85 : width: width,
86 : total: items.total,
87 0 : title: '${AppLocale.labels.budgetHeadline}, ${AppLocale.labels.left}',
88 : sortRoute: AppRoute.sortingRoute,
89 : ),
90 : ThemeHelper.hIndent,
91 0 : if (widthCount > 2) HeaderWidget(count: widthCount, width: width),
92 0 : Expanded(
93 0 : child: ListView.builder(
94 : scrollDirection: Axis.vertical,
95 : shrinkWrap: true,
96 0 : itemCount: items.list.length + 1,
97 0 : itemBuilder: (BuildContext context, index) {
98 0 : if (index >= items.list.length) {
99 : return ThemeHelper.formEndBox;
100 : }
101 0 : BudgetAppData item = items.list[index];
102 0 : return BackgroundWrapper(
103 : index: index,
104 0 : child: BaseSwipeWidget(
105 : routePath: AppRoute.budgetEditRoute,
106 0 : uuid: item.uuid!,
107 0 : child: TapWidget(
108 : tooltip: '',
109 0 : route: RouteSettings(name: AppRoute.budgetViewRoute, arguments: {routeArguments.uuid: item.uuid}),
110 0 : child: BudgetLineWidget(
111 : width: width,
112 : count: widthCount,
113 0 : uuid: item.uuid ?? '',
114 0 : title: item.title,
115 0 : amount: item.amount.toCurrency(currency: item.currency, withPattern: false),
116 0 : details: item.detailsFormatted,
117 0 : description: item.description,
118 0 : color: item.color ?? Colors.transparent,
119 0 : icon: item.icon ?? Icons.question_mark,
120 0 : skip: item.skip,
121 : ),
122 : ),
123 : ),
124 : );
125 : },
126 : ),
127 : ),
128 : ],
129 : ),
130 : );
131 : }
132 : }
|