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 1 : const BudgetPage({
25 : super.key,
26 : this.search,
27 : });
28 :
29 1 : @override
30 1 : BudgetPageState createState() => BudgetPageState();
31 : }
32 :
33 : class BudgetPageState extends AbstractPageState<BudgetPage> {
34 1 : AppDataGetter _getItems() {
35 2 : 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 2 : return state.get(AppDataType.budgets);
46 : }
47 :
48 1 : @override
49 : String getTitle() {
50 2 : if (widget.search != null) {
51 0 : return AppLocale.labels.search(widget.search!);
52 : }
53 2 : return AppLocale.labels.budgetHeadline;
54 : }
55 :
56 1 : @override
57 2 : String getButtonName() => AppLocale.labels.addBudgetTooltip;
58 :
59 1 : @override
60 : Widget buildButton(BuildContext context, BoxConstraints constraints) {
61 1 : NavigatorState nav = Navigator.of(context);
62 1 : return FloatingActionButton(
63 : heroTag: 'budget_page',
64 2 : onPressed: () => nav.pushNamed(AppRoute.budgetAddRoute),
65 1 : tooltip: getButtonName(),
66 : child: const Icon(Icons.add),
67 : );
68 : }
69 :
70 1 : @override
71 : Widget buildContent(BuildContext context, BoxConstraints constraints) {
72 1 : final items = _getItems();
73 1 : final sorting = Provider.of<AppSorting>(context, listen: true);
74 2 : items.list.sort(sorting.getSortFunction(context));
75 1 : final width = ThemeHelper.getWidth(context, 4, constraints);
76 1 : final indent = ThemeHelper.getIndent();
77 1 : final widthCount = ThemeHelper.getWidthCount(constraints, context);
78 1 : return Padding(
79 1 : padding: EdgeInsets.all(indent),
80 1 : child: Column(
81 1 : children: [
82 1 : BaseHeaderWidget(
83 : route: AppRoute.homeRoute,
84 2 : tooltip: AppLocale.labels.homeTooltip,
85 : width: width,
86 : total: items.total,
87 5 : title: '${AppLocale.labels.budgetHeadline}, ${AppLocale.labels.left}',
88 : sortRoute: AppRoute.sortingRoute,
89 : ),
90 : ThemeHelper.hIndent,
91 1 : if (widthCount > 2) HeaderWidget(count: widthCount, width: width),
92 1 : Expanded(
93 1 : child: ListView.builder(
94 : scrollDirection: Axis.vertical,
95 : shrinkWrap: true,
96 2 : itemCount: items.list.length + 1,
97 1 : itemBuilder: (BuildContext context, index) {
98 2 : if (index >= items.list.length) {
99 : return ThemeHelper.formEndBox;
100 : }
101 1 : BudgetAppData item = items.list[index];
102 1 : return BackgroundWrapper(
103 : index: index,
104 1 : child: BaseSwipeWidget(
105 : routePath: AppRoute.budgetEditRoute,
106 1 : uuid: item.uuid!,
107 1 : child: TapWidget(
108 : tooltip: '',
109 3 : route: RouteSettings(name: AppRoute.budgetViewRoute, arguments: {routeArguments.uuid: item.uuid}),
110 1 : child: BudgetLineWidget(
111 : width: width,
112 : count: widthCount,
113 1 : uuid: item.uuid ?? '',
114 1 : title: item.title,
115 3 : amount: item.amount.toCurrency(currency: item.currency, withPattern: false),
116 1 : details: item.detailsFormatted,
117 1 : description: item.description,
118 1 : color: item.color ?? Colors.transparent,
119 1 : icon: item.icon ?? Icons.question_mark,
120 1 : skip: item.skip,
121 : ),
122 : ),
123 : ),
124 : );
125 : },
126 : ),
127 : ),
128 : ],
129 : ),
130 : );
131 : }
132 : }
|