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/_configs/theme_helper.dart';
6 : import 'package:material_ui/material_ui.dart';
7 :
8 : class RowWidget extends StatelessWidget {
9 : late final List<double> chunk;
10 : final List<List<Widget>> children;
11 : final double indent;
12 : final double maxWidth;
13 : final MainAxisAlignment alignment;
14 :
15 6 : RowWidget({
16 : required List<double?> chunk,
17 : required this.children,
18 : required this.indent,
19 : required this.maxWidth,
20 : this.alignment = MainAxisAlignment.spaceBetween,
21 : super.key,
22 : }) {
23 6 : List<double?> scope = [...chunk];
24 18 : int restCount = scope.where((e) => e == null).length;
25 : double takenWidth = 0;
26 36 : double width = maxWidth - indent * (scope.length - 1);
27 18 : for (int i = 0; i < scope.length; i++) {
28 6 : if (scope[i] == null) {
29 : continue;
30 : }
31 6 : double value = scope[i]!;
32 12 : if (value > 0 && value < 1) {
33 4 : scope[i] = value * width;
34 4 : takenWidth += scope[i]!;
35 : } else {
36 6 : takenWidth += value;
37 : }
38 : }
39 6 : if (takenWidth > width) {
40 0 : double cut = (width - takenWidth) / (scope.length - restCount);
41 0 : scope = scope.map((value) => value != null ? value + cut : null).toList();
42 : takenWidth = width;
43 : }
44 6 : if (restCount > 0) {
45 12 : double rest = (width - takenWidth) / restCount;
46 18 : scope = scope.map((value) => value ?? rest).toList();
47 : }
48 12 : this.chunk = scope.cast<double>();
49 : }
50 :
51 6 : @override
52 : Widget build(BuildContext context) {
53 24 : final size = 2 * chunk.length - 1;
54 6 : return SizedBox(
55 6 : width: maxWidth,
56 6 : child: Row(
57 6 : crossAxisAlignment: AppDesign.getAlignment(),
58 : verticalDirection: VerticalDirection.down,
59 6 : mainAxisAlignment: alignment,
60 : // mainAxisAlignment: AppDesign.getAlignment<MainAxisAlignment>(),
61 12 : children: List<Widget>.generate(size, (index) {
62 18 : final idx = AppDesign.getAlignment() == CrossAxisAlignment.start ? index ~/ 2 : (size - index) ~/ 2;
63 12 : if (index % 2 == 1) {
64 12 : return SizedBox(width: indent);
65 18 : } else if (chunk[idx] > 0) {
66 6 : return SizedBox(
67 12 : width: chunk[idx],
68 6 : child: Column(
69 6 : crossAxisAlignment: AppDesign.getAlignment(),
70 6 : mainAxisAlignment: AppDesign.getAlignment<MainAxisAlignment>(),
71 : verticalDirection: VerticalDirection.down,
72 12 : children: children[idx],
73 : ),
74 : );
75 : } else {
76 : return ThemeHelper.emptyBox;
77 : }
78 : }),
79 : ),
80 : );
81 : }
82 : }
|