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

icapps / flutter-custom-image-crop / 6183199936

14 Sep 2023 08:57AM UTC coverage: 0.0%. Remained the same
6183199936

push

github

web-flow
Merge pull request #44 from icapps/feature/keep-ratio-while-cropping

Feature/keep ratio while cropping

128 of 128 new or added lines in 4 files covered. (100.0%)

0 of 362 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
      if (screenWidth > screenHeight * aspectRatio) {
×
32
        uiSize = screenHeight;
33
        cropSizeMax = imageHeight.toDouble();
×
34
      } else {
35
        uiSize = screenWidth;
36
        cropSizeMax = imageWidth.toDouble();
×
37
      }
38
      translateScale = cropSizeMax / (uiSize * cropPercentage);
×
39
      scale = dataScale;
40
      break;
41

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

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

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

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

81
    case CustomImageFit.fillVisibleSpace:
×
82
      final heightToWidthRatio = (screenHeight / screenWidth);
×
83

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

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

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

128
  final double cropSizeWidth;
129
  final double cropSizeHeight;
130
  if (aspectRatio > 1) {
×
131
    cropSizeWidth = cropSizeMax;
132
    cropSizeHeight = cropSizeWidth / aspectRatio;
×
133
  } else {
134
    cropSizeHeight = cropSizeMax;
135
    cropSizeWidth = cropSizeHeight * aspectRatio;
×
136
  }
137
  return OnCropParams(
×
138
    cropSizeHeight: cropSizeHeight,
139
    cropSizeWidth: cropSizeWidth,
140
    translateScale: translateScale,
141
    scale: scale,
142
  );
143
}
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