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 'dart:math';
5 :
6 : import 'package:adaptive_breakpoints/adaptive_breakpoints.dart';
7 : import 'package:app_finance/_classes/herald/app_zoom.dart';
8 : import 'package:app_finance/_configs/screen_helper.dart';
9 : import 'package:flutter/material.dart';
10 :
11 : class _Sizes {
12 : static const half = 4.0;
13 : static const normal = 8.0;
14 : static const double = 16.0;
15 : static const triple = 24.0;
16 : static const quatre = 32.0;
17 : static const sixfold = 48.0;
18 : }
19 :
20 : class ThemeHelper {
21 : static const barHeight = 48.0;
22 : static const menuWidth = 200.0;
23 3 : static late bool isWearable;
24 : static const emptyBox = SizedBox();
25 : static const formEndBox = SizedBox(height: 110);
26 : static const hIndent = SizedBox(height: _Sizes.normal);
27 : static const hIndent2x = SizedBox(height: _Sizes.double);
28 : static const hIndent3x = SizedBox(height: _Sizes.triple);
29 : static const hIndent4x = SizedBox(height: _Sizes.quatre);
30 : static const hIndent6x = SizedBox(height: _Sizes.sixfold);
31 : static const hIndent05 = SizedBox(height: _Sizes.half);
32 : static const wIndent = SizedBox(width: _Sizes.normal);
33 : static const wIndent2x = SizedBox(width: _Sizes.double);
34 :
35 12 : static double getIndent([double multiply = 1]) => _Sizes.normal / AppZoom.state * multiply;
36 :
37 3 : static double _env(BuildContext context, BoxConstraints? constraints) =>
38 : (constraints != null &&
39 6 : (isNavRight(context, constraints) || ScreenHelper.state().isRight) &&
40 0 : !(isWearable || ScreenHelper.state().isWearable)
41 : ? barHeight
42 3 : : 0) +
43 6 : (constraints != null && isWideScreen(constraints) || ScreenHelper.state().isWide ? menuWidth : 0);
44 :
45 0 : static double getMaxWidth(BuildContext context, BoxConstraints constraints) =>
46 0 : constraints.maxWidth - _env(context, constraints);
47 :
48 3 : static double getWidth(BuildContext context,
49 : [double multiply = 4, BoxConstraints? constraints, bool withZoom = true]) =>
50 15 : MediaQuery.sizeOf(context).width / (withZoom ? AppZoom.state : 1) -
51 6 : getIndent(multiply) -
52 3 : _env(context, constraints);
53 :
54 3 : static double getHeight(BuildContext context, [double multiply = 2]) =>
55 18 : MediaQuery.sizeOf(context).height / AppZoom.state - getIndent(multiply);
56 :
57 3 : static double getMinHeight(BuildContext context, [BoxConstraints? constraints]) =>
58 12 : [ThemeHelper.getHeight(context), constraints?.maxHeight ?? double.infinity].reduce(min);
59 :
60 3 : static bool isKeyboardVisible(BuildContext context, [BoxConstraints? constraints]) =>
61 12 : MediaQuery.of(context).viewInsets.bottom > 0 ||
62 12 : constraints != null && ScreenHelper.state().height - 100 > constraints.maxHeight;
63 :
64 0 : static double getMaxHeight(List<dynamic> scope) {
65 : double height = 0;
66 0 : for (int i = 0; i < scope.length; i++) {
67 : double tmpHeight = 0;
68 0 : if (scope[i] is Text) {
69 0 : tmpHeight = ThemeHelper.getTextHeight(scope[i]);
70 0 : } else if (scope[i] is RenderBox) {
71 0 : tmpHeight = (scope[i] as RenderBox).getMaxIntrinsicHeight(double.infinity);
72 : }
73 0 : if (tmpHeight > height) {
74 : height = tmpHeight;
75 : }
76 : }
77 : return height;
78 : }
79 :
80 3 : static double getTextHeight(Text txt) {
81 6 : return _getPainter(txt).height;
82 : }
83 :
84 0 : static double getTextWidth(Text txt) {
85 0 : return _getPainter(txt).width;
86 : }
87 :
88 3 : static TextPainter _getPainter(Text txt) {
89 3 : final painter = TextPainter(
90 9 : text: TextSpan(text: txt.data, style: txt.style),
91 : textDirection: TextDirection.ltr,
92 3 : maxLines: txt.maxLines,
93 : );
94 3 : painter.layout();
95 : return painter;
96 : }
97 :
98 3 : static bool isTextExceedWidth(String txt, TextStyle? style, double maxWidth) {
99 3 : final textPainter = TextPainter(
100 3 : text: TextSpan(
101 : text: txt,
102 : style: style,
103 : ),
104 : maxLines: 1,
105 : textDirection: TextDirection.ltr,
106 3 : )..layout(maxWidth: maxWidth);
107 3 : return textPainter.didExceedMaxLines;
108 : }
109 :
110 0 : static bool isVertical(BoxConstraints constraints) => constraints.maxWidth < constraints.maxHeight;
111 :
112 0 : static bool isLower(AdaptiveWindowType size, AdaptiveWindowType windowType) => windowType <= size;
113 :
114 9 : static bool isNavBottom(BoxConstraints constraints) => getWidthCount(constraints) <= 2;
115 :
116 3 : static bool isNavRight(BuildContext context, BoxConstraints constraints) =>
117 9 : isNavBottom(constraints) && getHeightCount(context, constraints) <= 2;
118 :
119 3 : static int getWidthCount(BoxConstraints? constraints, [BuildContext? context]) {
120 5 : double width = context != null ? getWidth(context, 0, constraints) : constraints?.maxWidth ?? 0;
121 3 : final matrix = {
122 3 : AdaptiveWindowType.xlarge: width >= 1440, // AdaptiveWindowType.xlarge.widthRangeValues.start,
123 3 : AdaptiveWindowType.large: width >= 1024, // AdaptiveWindowType.large.widthRangeValues.start,
124 3 : AdaptiveWindowType.medium: width >= 640, // AdaptiveWindowType.medium.widthRangeValues.start,
125 3 : AdaptiveWindowType.small: width >= 320, // AdaptiveWindowType.small.widthRangeValues.start,
126 : AdaptiveWindowType.xsmall: true,
127 : };
128 9 : matrix.removeWhere((_, value) => value == false);
129 6 : return switch (matrix.keys.first) {
130 3 : AdaptiveWindowType.xlarge => 4,
131 3 : AdaptiveWindowType.large => 3,
132 3 : AdaptiveWindowType.medium => 2,
133 : _ => 1,
134 : };
135 : }
136 :
137 3 : static int getHeightCount(BuildContext context, [BoxConstraints? constraints]) {
138 3 : final height = getMinHeight(context, constraints);
139 3 : final matrix = {
140 3 : AdaptiveWindowType.xlarge: height >= 1440,
141 3 : AdaptiveWindowType.large: height >= 800,
142 3 : AdaptiveWindowType.medium: height >= 480,
143 3 : AdaptiveWindowType.small: height >= 240,
144 : AdaptiveWindowType.xsmall: true,
145 : };
146 9 : matrix.removeWhere((_, value) => value == false);
147 6 : return switch (matrix.keys.first) {
148 3 : AdaptiveWindowType.xlarge => 7,
149 3 : AdaptiveWindowType.large => 5,
150 3 : AdaptiveWindowType.medium => 4,
151 0 : AdaptiveWindowType.small => 2,
152 : _ => 1,
153 : };
154 : }
155 :
156 9 : static bool isWideScreen(BoxConstraints constraints) => ThemeHelper.getWidthCount(constraints) >= 4;
157 :
158 3 : static bool isWearableMode(BuildContext context, BoxConstraints constraints) =>
159 12 : isWearable = getWidthCount(constraints) * getHeightCount(context, constraints) == 1;
160 : }
|