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