1
- import " dart:math" as math;
1
+ import ' dart:math' as math;
2
2
import 'package:collection/collection.dart' ;
3
3
import 'package:flutter/material.dart' ;
4
4
@@ -41,31 +41,20 @@ class BarcodePainter extends CustomPainter {
41
41
return ;
42
42
}
43
43
44
- ScalingRatios ratio = calculateBoxFitRatio (boxFit, cameraPreviewSize, size);
44
+ final ratios = calculateBoxFitRatio (boxFit, cameraPreviewSize, size);
45
45
// final adjustedSize = applyBoxFit(boxFit, cameraPreviewSize, size);
46
46
47
- double horizontalPadding =
48
- (( cameraPreviewSize.width * ratio .widthRatio - size.width) / 2 ) ;
49
- double verticalPadding =
50
- (( cameraPreviewSize.height * ratio .heightRatio - size.height) / 2 ) ;
47
+ final double horizontalPadding =
48
+ (cameraPreviewSize.width * ratios .widthRatio - size.width) / 2 ;
49
+ final double verticalPadding =
50
+ (cameraPreviewSize.height * ratios .heightRatio - size.height) / 2 ;
51
51
52
52
final List <Offset > adjustedOffset = [
53
- Offset (
54
- (barcodeCorners[0 ].dx * ratio.widthRatio - horizontalPadding),
55
- (barcodeCorners[0 ].dy * ratio.heightRatio - verticalPadding),
56
- ),
57
- Offset (
58
- (barcodeCorners[1 ].dx * ratio.widthRatio - horizontalPadding),
59
- (barcodeCorners[1 ].dy * ratio.heightRatio - verticalPadding),
60
- ),
61
- Offset (
62
- (barcodeCorners[2 ].dx * ratio.widthRatio - horizontalPadding),
63
- (barcodeCorners[2 ].dy * ratio.heightRatio - verticalPadding),
64
- ),
65
- Offset (
66
- (barcodeCorners[3 ].dx * ratio.widthRatio - horizontalPadding),
67
- (barcodeCorners[3 ].dy * ratio.heightRatio - verticalPadding),
68
- ),
53
+ for (final offset in barcodeCorners)
54
+ Offset (
55
+ offset.dx * ratios.widthRatio - horizontalPadding,
56
+ offset.dy * ratios.heightRatio - verticalPadding,
57
+ ),
69
58
];
70
59
71
60
final cutoutPath = Path ()..addPolygon (adjustedOffset, true );
@@ -90,28 +79,17 @@ class BarcodePainter extends CustomPainter {
90
79
}
91
80
}
92
81
93
- class ScalingRatios {
94
- final double widthRatio;
95
- final double heightRatio;
96
-
97
- ScalingRatios (this .widthRatio, this .heightRatio);
98
-
99
- @override
100
- String toString () =>
101
- 'ScalingRatios(widthRatio: $widthRatio , heightRatio: $heightRatio )' ;
102
- }
103
-
104
82
/// Calculate the scaling ratios for width and height to fit the small box (cameraPreviewSize)
105
83
/// into the large box (size) based on the specified BoxFit mode.
106
- /// Returns a ScalingRatios object containing the width and height scaling ratios.
107
- ScalingRatios calculateBoxFitRatio (
84
+ /// Returns a record containing the width and height scaling ratios.
85
+ ({ double widthRatio, double heightRatio}) calculateBoxFitRatio (
108
86
BoxFit boxFit, Size cameraPreviewSize, Size size) {
109
87
// If the width or height of cameraPreviewSize or size is 0, return (1.0, 1.0) (no scaling)
110
88
if (cameraPreviewSize.width <= 0 ||
111
89
cameraPreviewSize.height <= 0 ||
112
90
size.width <= 0 ||
113
91
size.height <= 0 ) {
114
- return ScalingRatios ( 1.0 , 1.0 );
92
+ return (widthRatio : 1.0 , heightRatio : 1.0 );
115
93
}
116
94
117
95
// Calculate the scaling ratios for width and height
@@ -121,33 +99,33 @@ ScalingRatios calculateBoxFitRatio(
121
99
switch (boxFit) {
122
100
case BoxFit .fill:
123
101
// Stretch to fill the large box without maintaining aspect ratio
124
- return ScalingRatios (widthRatio, heightRatio);
102
+ return (widthRatio: widthRatio, heightRatio : heightRatio);
125
103
126
104
case BoxFit .contain:
127
105
// Maintain aspect ratio, ensure the content fits entirely within the large box
128
106
final ratio = math.min (widthRatio, heightRatio);
129
- return ScalingRatios ( ratio, ratio);
107
+ return (widthRatio : ratio, heightRatio : ratio);
130
108
131
109
case BoxFit .cover:
132
110
// Maintain aspect ratio, ensure the content fully covers the large box
133
111
final ratio = math.max (widthRatio, heightRatio);
134
- return ScalingRatios ( ratio, ratio);
112
+ return (widthRatio : ratio, heightRatio : ratio);
135
113
136
114
case BoxFit .fitWidth:
137
115
// Maintain aspect ratio, ensure the width matches the large box
138
- return ScalingRatios (widthRatio, widthRatio);
116
+ return (widthRatio: widthRatio, heightRatio : widthRatio);
139
117
140
118
case BoxFit .fitHeight:
141
119
// Maintain aspect ratio, ensure the height matches the large box
142
- return ScalingRatios ( heightRatio, heightRatio);
120
+ return (widthRatio : heightRatio, heightRatio : heightRatio);
143
121
144
122
case BoxFit .none:
145
123
// No scaling
146
- return ScalingRatios ( 1.0 , 1.0 );
124
+ return (widthRatio : 1.0 , heightRatio : 1.0 );
147
125
148
126
case BoxFit .scaleDown:
149
127
// If the content is larger than the large box, scale down to fit; otherwise, no scaling
150
128
final ratio = math.min (1.0 , math.min (widthRatio, heightRatio));
151
- return ScalingRatios ( ratio, ratio);
129
+ return (widthRatio : ratio, heightRatio : ratio);
152
130
}
153
131
}
0 commit comments