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_preferences.dart';
6 : import 'package:app_finance/_configs/theme_helper.dart';
7 : import 'package:app_finance/_ext/build_context_ext.dart';
8 : import 'package:app_finance/_ext/string_ext.dart';
9 : import 'package:app_finance/components/_core/components_builder_item.dart';
10 : import 'package:app_finance/components/_core/component_data.dart';
11 : import 'package:app_finance/components/_core/widgets/draggable_frame.dart';
12 : import 'package:flutter/material.dart';
13 : import 'package:flutter_grid_layout/flutter_grid_layout.dart';
14 :
15 : class ComponentsBuilder extends StatelessWidget {
16 : final List<ComponentData> data;
17 : final bool editMode;
18 : final Function? adjust;
19 : final Function? delete;
20 : final _shift = componentData.shift;
21 : final _order = componentData.order;
22 : final _start = componentData.start;
23 : final _end = componentData.end;
24 :
25 0 : ComponentsBuilder(
26 : this.data, {
27 : super.key,
28 : this.editMode = false,
29 : this.adjust,
30 : this.delete,
31 : });
32 :
33 1 : static String getKey(BuildContext context) =>
34 5 : 'cmp${ThemeHelper.getWidth(context).toInt()}x${ThemeHelper.getHeight(context).toInt()}';
35 :
36 1 : static List<ComponentData>? getData(BuildContext context) =>
37 3 : AppPreferences.get(getKey(context))?.toList<ComponentData>();
38 :
39 0 : void resize(ComponentData change, Size start) {
40 0 : final scope = data[change[_order]];
41 0 : scope[_order] = change[_order];
42 0 : if (change[_shift] != null) {
43 0 : scope[componentData.endX] += start.width - scope[componentData.startX];
44 0 : scope[componentData.endY] += start.height - scope[componentData.startY];
45 0 : scope[componentData.startX] = start.width;
46 0 : scope[componentData.startY] = start.height;
47 0 : } else if (change[_start] != null) {
48 0 : scope[componentData.startX] = start.width;
49 0 : scope[componentData.startY] = start.height;
50 0 : } else if (change[_end] != null) {
51 0 : scope[componentData.endX] = start.width + 1.0;
52 0 : scope[componentData.endY] = start.height + 1.0;
53 : }
54 0 : adjust!(scope[_order], scope);
55 : }
56 :
57 0 : @override
58 : Widget build(BuildContext context) {
59 0 : final isVertical = ThemeHelper.getWidth(context) < ThemeHelper.getHeight(context);
60 : final rowsCount = (isVertical ? 6 : 12);
61 : final columnsCount = (isVertical ? 12 : 6);
62 0 : return GridContainer(
63 0 : alignment: AppDesign.getAlignment<MainAxisAlignment>(),
64 0 : rows: List.filled(rowsCount, null),
65 0 : columns: List.filled(columnsCount, null),
66 0 : children: editMode
67 0 : ? [
68 0 : ...List.generate(72, (i) {
69 0 : final start = Size(i % rowsCount.toDouble(), (i ~/ rowsCount).toDouble());
70 0 : return GridItem(
71 : start: start,
72 0 : end: Size(start.width + 1, start.height + 1),
73 : order: 0,
74 0 : child: DragTarget<ComponentData>(
75 0 : onWillAccept: (_) => true,
76 0 : onAccept: (change) => resize(change, start),
77 0 : builder: (context, candidateData, rejectedData) => Container(
78 0 : decoration: BoxDecoration(
79 0 : border: Border.all(color: context.colorScheme.secondary.withOpacity(0.1)),
80 0 : color: candidateData.isEmpty ? null : Colors.green.shade200,
81 : ),
82 : ),
83 : ),
84 : );
85 : }),
86 0 : ...List.generate(
87 0 : data.length,
88 0 : (i) => GridItem(
89 0 : start: Size(data[i][componentData.startX] + .0, data[i][componentData.startY] + .0),
90 0 : end: Size(data[i][componentData.endX] + .0, data[i][componentData.endY] + .0),
91 0 : order: i + 1,
92 0 : child: DraggableFrame({...data[i], _order: i}, delete: delete!, adjust: adjust!),
93 : ),
94 : ),
95 : ]
96 0 : : List.generate(
97 0 : data.length,
98 0 : (i) => GridItem(
99 0 : start: Size(data[i][componentData.startX] + .0, data[i][componentData.startY] + .0),
100 0 : end: Size(data[i][componentData.endX] + .0, data[i][componentData.endY] + .0),
101 0 : child: ComponentsBuilderItem({...data[i], _order: i}),
102 : )),
103 : );
104 : }
105 : }
|