• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

icapps / flutter-custom-image-crop / 6545506866

17 Oct 2023 09:53AM UTC coverage: 0.0%. Remained the same
6545506866

Pull #45

github

web-flow
Merge bff8ba3a1 into 75def6bbd
Pull Request #45: optimize fillcropscpace imagefit

130 of 130 new or added lines in 3 files covered. (100.0%)

0 of 485 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lib/src/calculators/calculate_on_crop_params.dart
1
import 'dart:math';
2

3
import 'package:custom_image_crop/src/models/params_model.dart';
4
import 'package:custom_image_crop/src/widgets/custom_image_crop_widget.dart';
5

6
/// Returns params to use for cropping image.
7
OnCropParams caclulateOnCropParams({
×
8
  required double screenWidth,
9
  required double screenHeight,
10
  required double cropPercentage,
11
  required double dataScale,
12
  required double aspectRatio,
13
  required int imageWidth,
14
  required int imageHeight,
15
  required CustomImageFit imageFit,
16
}) {
17
  /// the size of the area to crop (width and/or height depending on the aspect ratio)
18
  final double cropSizeMax;
19

20
  /// the scale used to adjust x and y coodinates of the crop area
21
  final double translateScale;
22

23
  /// used to adjust image scale
24
  final double scale;
25

26
  /// Temp variable used to calculate the translateScale
27
  final double uiSize;
28

29
  switch (imageFit) {
30
    case CustomImageFit.fillCropSpace:
×
31
      double cropScale;
32
      if (screenWidth > screenHeight * aspectRatio) {
×
33
        uiSize = screenHeight;
34
        cropSizeMax = imageHeight.toDouble();
×
35
        cropScale = max(cropSizeMax / imageWidth, 1.0);
×
36
      } else {
37
        uiSize = screenWidth;
38
        cropSizeMax = imageWidth.toDouble();
×
39
        cropScale = max(cropSizeMax / imageHeight, 1.0);
×
40
      }
41
      translateScale = cropSizeMax / (uiSize * cropPercentage);
×
42
      scale = dataScale * cropScale;
×
43
      break;
44

45
    case CustomImageFit.fitCropSpace:
×
46
      if (screenWidth > screenHeight * aspectRatio) {
×
47
        uiSize = screenHeight;
48
        cropSizeMax = imageWidth.toDouble() / aspectRatio;
×
49
      } else {
50
        uiSize = screenWidth;
51
        cropSizeMax = imageHeight.toDouble() * aspectRatio;
×
52
      }
53
      translateScale = cropSizeMax / (uiSize * cropPercentage);
×
54
      scale = dataScale;
55
      break;
56

57
    case CustomImageFit.fillCropWidth:
×
58
      uiSize = screenWidth;
59
      cropSizeMax = imageWidth / min(1, aspectRatio);
×
60
      translateScale = cropSizeMax / (uiSize * cropPercentage);
×
61
      scale = dataScale;
62
      break;
63

64
    case CustomImageFit.fillCropHeight:
×
65
      uiSize = screenHeight;
66
      cropSizeMax = imageHeight * max(1, aspectRatio);
×
67
      translateScale = cropSizeMax / (uiSize * cropPercentage);
×
68
      scale = dataScale;
69
      break;
70

71
    case CustomImageFit.fitVisibleSpace:
×
72
      final double uiSize;
73
      if (screenHeight * aspectRatio < screenWidth) {
×
74
        uiSize = screenHeight;
75
        cropSizeMax = imageHeight.toDouble();
×
76
      } else {
77
        uiSize = screenWidth;
78
        cropSizeMax = imageWidth.toDouble();
×
79
      }
80
      scale = dataScale / cropPercentage;
×
81
      translateScale = cropSizeMax / uiSize / cropPercentage;
×
82
      break;
83

84
    case CustomImageFit.fillVisibleSpace:
×
85
      final heightToWidthRatio = (screenHeight / screenWidth);
×
86

87
      if (screenHeight * aspectRatio > screenWidth) {
×
88
        uiSize = screenHeight;
89
        cropSizeMax = imageHeight.toDouble();
×
90
        translateScale =
91
            cropSizeMax / uiSize / cropPercentage * heightToWidthRatio;
×
92
        scale = dataScale / cropPercentage * heightToWidthRatio;
×
93
      } else {
94
        uiSize = screenWidth;
95
        cropSizeMax = imageWidth.toDouble();
×
96
        translateScale =
97
            cropSizeMax / uiSize / cropPercentage / heightToWidthRatio;
×
98
        scale = dataScale / cropPercentage / heightToWidthRatio;
×
99
      }
100
      break;
101

102
    case CustomImageFit.fillVisibleHeight:
×
103
      final heightToWidthRatio = (screenHeight / screenWidth);
×
104
      uiSize = screenHeight;
105
      cropSizeMax = imageHeight.toDouble();
×
106
      if (screenWidth > screenHeight * aspectRatio) {
×
107
        translateScale = cropSizeMax / uiSize / cropPercentage;
×
108
        scale = dataScale / cropPercentage;
×
109
      } else {
110
        translateScale =
111
            cropSizeMax / uiSize / cropPercentage * heightToWidthRatio;
×
112
        scale = dataScale / cropPercentage * heightToWidthRatio;
×
113
      }
114
      break;
115

116
    case CustomImageFit.fillVisibleWidth:
×
117
      final heightToWidthRatio = (screenHeight / screenWidth);
×
118
      uiSize = screenWidth;
119
      cropSizeMax = imageWidth.toDouble();
×
120
      if (screenWidth > screenHeight * aspectRatio) {
×
121
        translateScale =
122
            cropSizeMax / uiSize / cropPercentage / heightToWidthRatio;
×
123
        scale = dataScale / cropPercentage / heightToWidthRatio;
×
124
      } else {
125
        translateScale = cropSizeMax / uiSize / cropPercentage;
×
126
        scale = dataScale / cropPercentage;
×
127
      }
128
      break;
129
  }
130

131
  final double cropSizeWidth;
132
  final double cropSizeHeight;
133
  if (aspectRatio > 1) {
×
134
    cropSizeWidth = cropSizeMax;
135
    cropSizeHeight = cropSizeWidth / aspectRatio;
×
136
  } else {
137
    cropSizeHeight = cropSizeMax;
138
    cropSizeWidth = cropSizeHeight * aspectRatio;
×
139
  }
140
  return OnCropParams(
×
141
    cropSizeHeight: cropSizeHeight,
142
    cropSizeWidth: cropSizeWidth,
143
    translateScale: translateScale,
144
    scale: scale,
145
  );
146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc