LCOV - code coverage report
Current view: top level - lib/_configs - theme_helper.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 63 74 85.1 %
Date: 2024-10-04 11:12:13 Functions: 0 0 -

          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           1 :   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           4 :   static double getIndent([double multiply = 1]) => _Sizes.normal / AppZoom.state * multiply;
      36             : 
      37           1 :   static double _env(BuildContext context, BoxConstraints? constraints) =>
      38             :       (constraints != null &&
      39           2 :               (isNavRight(context, constraints) || ScreenHelper.state().isRight) &&
      40           0 :               !(isWearable || ScreenHelper.state().isWearable)
      41             :           ? barHeight
      42           1 :           : 0) +
      43           2 :       (constraints != null && isWideScreen(constraints) || ScreenHelper.state().isWide ? menuWidth : 0);
      44             : 
      45           1 :   static double getMaxWidth(BuildContext context, BoxConstraints constraints) =>
      46           3 :       constraints.maxWidth - _env(context, constraints);
      47             : 
      48           1 :   static double getWidth(BuildContext context,
      49             :           [double multiply = 4, BoxConstraints? constraints, bool withZoom = true]) =>
      50           5 :       MediaQuery.sizeOf(context).width / (withZoom ? AppZoom.state : 1) -
      51           2 :       getIndent(multiply) -
      52           1 :       _env(context, constraints);
      53             : 
      54           1 :   static double getHeight(BuildContext context, [double multiply = 2]) =>
      55           6 :       MediaQuery.sizeOf(context).height / AppZoom.state - getIndent(multiply);
      56             : 
      57           1 :   static double getMinHeight(BuildContext context, [BoxConstraints? constraints]) =>
      58           4 :       [ThemeHelper.getHeight(context), constraints?.maxHeight ?? double.infinity].reduce(min);
      59             : 
      60           1 :   static bool isKeyboardVisible(BuildContext context, [BoxConstraints? constraints]) =>
      61           4 :       MediaQuery.of(context).viewInsets.bottom > 0 ||
      62           4 :       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           1 :   static double getTextHeight(Text txt) {
      81           2 :     return _getPainter(txt).height;
      82             :   }
      83             : 
      84           1 :   static double getTextWidth(Text txt) {
      85           2 :     return _getPainter(txt).width;
      86             :   }
      87             : 
      88           1 :   static TextPainter _getPainter(Text txt) {
      89           1 :     final painter = TextPainter(
      90           3 :       text: TextSpan(text: txt.data, style: txt.style),
      91             :       textDirection: TextDirection.ltr,
      92           1 :       maxLines: txt.maxLines,
      93             :     );
      94           1 :     painter.layout();
      95             :     return painter;
      96             :   }
      97             : 
      98           1 :   static bool isTextExceedWidth(String txt, TextStyle? style, double maxWidth) {
      99           1 :     final textPainter = TextPainter(
     100           1 :       text: TextSpan(
     101             :         text: txt,
     102             :         style: style,
     103             :       ),
     104             :       maxLines: 1,
     105             :       textDirection: TextDirection.ltr,
     106           1 :     )..layout(maxWidth: maxWidth);
     107           1 :     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           3 :   static bool isNavBottom(BoxConstraints constraints) => getWidthCount(constraints) <= 2;
     115             : 
     116           1 :   static bool isNavRight(BuildContext context, BoxConstraints constraints) =>
     117           3 :       isNavBottom(constraints) && getHeightCount(context, constraints) <= 2;
     118             : 
     119           1 :   static int getWidthCount(BoxConstraints? constraints, [BuildContext? context]) {
     120           2 :     double width = context != null ? getWidth(context, 0, constraints) : constraints?.maxWidth ?? 0;
     121           1 :     final matrix = {
     122           1 :       AdaptiveWindowType.xlarge: width >= 1440, // AdaptiveWindowType.xlarge.widthRangeValues.start,
     123           1 :       AdaptiveWindowType.large: width >= 1024, // AdaptiveWindowType.large.widthRangeValues.start,
     124           1 :       AdaptiveWindowType.medium: width >= 640, // AdaptiveWindowType.medium.widthRangeValues.start,
     125           1 :       AdaptiveWindowType.small: width >= 320, // AdaptiveWindowType.small.widthRangeValues.start,
     126             :       AdaptiveWindowType.xsmall: true,
     127             :     };
     128           3 :     matrix.removeWhere((_, value) => value == false);
     129           2 :     return switch (matrix.keys.first) {
     130           1 :       AdaptiveWindowType.xlarge => 4,
     131           1 :       AdaptiveWindowType.large => 3,
     132           1 :       AdaptiveWindowType.medium => 2,
     133             :       _ => 1,
     134             :     };
     135             :   }
     136             : 
     137           1 :   static int getHeightCount(BuildContext context, [BoxConstraints? constraints]) {
     138           1 :     final height = getMinHeight(context, constraints);
     139           1 :     final matrix = {
     140           1 :       AdaptiveWindowType.xlarge: height >= 1440,
     141           1 :       AdaptiveWindowType.large: height >= 800,
     142           1 :       AdaptiveWindowType.medium: height >= 480,
     143           1 :       AdaptiveWindowType.small: height >= 240,
     144             :       AdaptiveWindowType.xsmall: true,
     145             :     };
     146           3 :     matrix.removeWhere((_, value) => value == false);
     147           2 :     return switch (matrix.keys.first) {
     148           1 :       AdaptiveWindowType.xlarge => 7,
     149           1 :       AdaptiveWindowType.large => 5,
     150           1 :       AdaptiveWindowType.medium => 4,
     151           0 :       AdaptiveWindowType.small => 2,
     152             :       _ => 1,
     153             :     };
     154             :   }
     155             : 
     156           3 :   static bool isWideScreen(BoxConstraints constraints) => ThemeHelper.getWidthCount(constraints) >= 4;
     157             : 
     158           1 :   static bool isWearableMode(BuildContext context, BoxConstraints constraints) =>
     159           4 :       isWearable = getWidthCount(constraints) * getHeightCount(context, constraints) == 1;
     160             : }

Generated by: LCOV version 1.14