Showing
3 changed files
with
55 additions
and
31 deletions
| @@ -193,8 +193,8 @@ class _MobileScannerState extends State<MobileScanner> | @@ -193,8 +193,8 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 193 | scanWindow = calculateScanWindowRelativeToTextureInPercentage( | 193 | scanWindow = calculateScanWindowRelativeToTextureInPercentage( |
| 194 | widget.fit, | 194 | widget.fit, |
| 195 | widget.scanWindow!, | 195 | widget.scanWindow!, |
| 196 | - value.size, | ||
| 197 | - constraints.biggest, | 196 | + textureSize: value.size, |
| 197 | + widgetSize: constraints.biggest, | ||
| 198 | ); | 198 | ); |
| 199 | 199 | ||
| 200 | _controller.updateScanWindow(scanWindow); | 200 | _controller.updateScanWindow(scanWindow); |
| @@ -18,11 +18,11 @@ import 'package:flutter/rendering.dart'; | @@ -18,11 +18,11 @@ import 'package:flutter/rendering.dart'; | ||
| 18 | /// Returns a [Rect] that represents the position and size of the scan window in the texture. | 18 | /// Returns a [Rect] that represents the position and size of the scan window in the texture. |
| 19 | Rect calculateScanWindowRelativeToTextureInPercentage( | 19 | Rect calculateScanWindowRelativeToTextureInPercentage( |
| 20 | BoxFit fit, | 20 | BoxFit fit, |
| 21 | - Rect scanWindow, | ||
| 22 | - Size textureSize, | ||
| 23 | - Size widgetSize, | ||
| 24 | -) { | ||
| 25 | - /// map the texture size to get its new size after fitted to screen | 21 | + Rect scanWindow, { |
| 22 | + required Size textureSize, | ||
| 23 | + required Size widgetSize, | ||
| 24 | +}) { | ||
| 25 | + // Convert the texture size to a size in widget-space, with the box fit applied. | ||
| 26 | final fittedTextureSize = applyBoxFit(fit, textureSize, widgetSize); | 26 | final fittedTextureSize = applyBoxFit(fit, textureSize, widgetSize); |
| 27 | 27 | ||
| 28 | // Get the correct scaling values depending on the given BoxFit mode | 28 | // Get the correct scaling values depending on the given BoxFit mode |
| @@ -76,8 +76,9 @@ Rect calculateScanWindowRelativeToTextureInPercentage( | @@ -76,8 +76,9 @@ Rect calculateScanWindowRelativeToTextureInPercentage( | ||
| 76 | 76 | ||
| 77 | // Clip the scan window in texture coordinates with the texture bounds. | 77 | // Clip the scan window in texture coordinates with the texture bounds. |
| 78 | // This prevents percentages outside the range [0; 1]. | 78 | // This prevents percentages outside the range [0; 1]. |
| 79 | - final clippedScanWndInTexSpace = scanWindowInTexSpace | ||
| 80 | - .intersect(Rect.fromLTWH(0, 0, textureSize.width, textureSize.height)); | 79 | + final clippedScanWndInTexSpace = scanWindowInTexSpace.intersect( |
| 80 | + Rect.fromLTWH(0, 0, textureSize.width, textureSize.height), | ||
| 81 | + ); | ||
| 81 | 82 | ||
| 82 | // Compute relative rectangle coordinates, | 83 | // Compute relative rectangle coordinates, |
| 83 | // with respect to the texture size, i.e. scan image. | 84 | // with respect to the texture size, i.e. scan image. |
| @@ -20,49 +20,58 @@ void main() { | @@ -20,49 +20,58 @@ void main() { | ||
| 20 | 20 | ||
| 21 | test('wl tp: BoxFit.none', () { | 21 | test('wl tp: BoxFit.none', () { |
| 22 | ctx.testScanWindow( | 22 | ctx.testScanWindow( |
| 23 | - BoxFit.none, const Rect.fromLTRB(0.275, 0.4, 0.725, 0.6)); | 23 | + BoxFit.none, |
| 24 | + const Rect.fromLTRB(0.275, 0.4, 0.725, 0.6), | ||
| 25 | + ); | ||
| 24 | }); | 26 | }); |
| 25 | 27 | ||
| 26 | test('wl tp: BoxFit.fill', () { | 28 | test('wl tp: BoxFit.fill', () { |
| 27 | ctx.testScanWindow( | 29 | ctx.testScanWindow( |
| 28 | - BoxFit.fill, const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75)); | 30 | + BoxFit.fill, |
| 31 | + const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75), | ||
| 32 | + ); | ||
| 29 | }); | 33 | }); |
| 30 | 34 | ||
| 31 | test('wl tp: BoxFit.fitHeight', () { | 35 | test('wl tp: BoxFit.fitHeight', () { |
| 32 | ctx.testScanWindow( | 36 | ctx.testScanWindow( |
| 33 | - BoxFit.fitHeight, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75)); | 37 | + BoxFit.fitHeight, |
| 38 | + const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75), | ||
| 39 | + ); | ||
| 34 | }); | 40 | }); |
| 35 | 41 | ||
| 36 | test('wl tp: BoxFit.fitWidth', () { | 42 | test('wl tp: BoxFit.fitWidth', () { |
| 37 | ctx.testScanWindow( | 43 | ctx.testScanWindow( |
| 38 | - BoxFit.fitWidth, | ||
| 39 | - const Rect.fromLTRB( | ||
| 40 | - 0.25, 0.38888888888888895, 0.75, 0.6111111111111112)); | 44 | + BoxFit.fitWidth, |
| 45 | + const Rect.fromLTRB(0.25, 0.38888888888888895, 0.75, 0.6111111111111112), | ||
| 46 | + ); | ||
| 41 | }); | 47 | }); |
| 42 | 48 | ||
| 43 | test('wl tp: BoxFit.cover', () { | 49 | test('wl tp: BoxFit.cover', () { |
| 44 | // equal to fitWidth | 50 | // equal to fitWidth |
| 45 | ctx.testScanWindow( | 51 | ctx.testScanWindow( |
| 46 | - BoxFit.cover, | ||
| 47 | - const Rect.fromLTRB( | ||
| 48 | - 0.25, 0.38888888888888895, 0.75, 0.6111111111111112)); | 52 | + BoxFit.cover, |
| 53 | + const Rect.fromLTRB(0.25, 0.38888888888888895, 0.75, 0.6111111111111112), | ||
| 54 | + ); | ||
| 49 | }); | 55 | }); |
| 50 | 56 | ||
| 51 | test('wl tp: BoxFit.contain', () { | 57 | test('wl tp: BoxFit.contain', () { |
| 52 | // equal to fitHeigth | 58 | // equal to fitHeigth |
| 53 | ctx.testScanWindow( | 59 | ctx.testScanWindow( |
| 54 | - BoxFit.contain, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75)); | 60 | + BoxFit.contain, |
| 61 | + const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75), | ||
| 62 | + ); | ||
| 55 | }); | 63 | }); |
| 56 | 64 | ||
| 57 | test('wl tp: BoxFit.scaleDown', () { | 65 | test('wl tp: BoxFit.scaleDown', () { |
| 58 | // equal to fitHeigth, contain | 66 | // equal to fitHeigth, contain |
| 59 | ctx.testScanWindow( | 67 | ctx.testScanWindow( |
| 60 | - BoxFit.scaleDown, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75)); | 68 | + BoxFit.scaleDown, |
| 69 | + const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75), | ||
| 70 | + ); | ||
| 61 | }); | 71 | }); |
| 62 | }); | 72 | }); |
| 63 | 73 | ||
| 64 | - group('Widget (landscape) smaller than texture and texture (landscape)', | ||
| 65 | - () { | 74 | + group('Widget (landscape) smaller than texture and texture (landscape)', () { |
| 66 | const textureSize = Size(640.0, 480.0); | 75 | const textureSize = Size(640.0, 480.0); |
| 67 | const widgetSize = Size(320.0, 120.0); | 76 | const widgetSize = Size(320.0, 120.0); |
| 68 | final ctx = ScanWindowTestContext( | 77 | final ctx = ScanWindowTestContext( |
| @@ -78,40 +87,54 @@ void main() { | @@ -78,40 +87,54 @@ void main() { | ||
| 78 | 87 | ||
| 79 | test('wl tl: BoxFit.none', () { | 88 | test('wl tl: BoxFit.none', () { |
| 80 | ctx.testScanWindow( | 89 | ctx.testScanWindow( |
| 81 | - BoxFit.none, const Rect.fromLTRB(0.375, 0.4375, 0.625, 0.5625)); | 90 | + BoxFit.none, |
| 91 | + const Rect.fromLTRB(0.375, 0.4375, 0.625, 0.5625), | ||
| 92 | + ); | ||
| 82 | }); | 93 | }); |
| 83 | 94 | ||
| 84 | test('wl tl: BoxFit.fill', () { | 95 | test('wl tl: BoxFit.fill', () { |
| 85 | ctx.testScanWindow( | 96 | ctx.testScanWindow( |
| 86 | - BoxFit.fill, const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75)); | 97 | + BoxFit.fill, |
| 98 | + const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75), | ||
| 99 | + ); | ||
| 87 | }); | 100 | }); |
| 88 | 101 | ||
| 89 | test('wl tl: BoxFit.fitHeight', () { | 102 | test('wl tl: BoxFit.fitHeight', () { |
| 90 | ctx.testScanWindow( | 103 | ctx.testScanWindow( |
| 91 | - BoxFit.fitHeight, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75)); | 104 | + BoxFit.fitHeight, |
| 105 | + const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75), | ||
| 106 | + ); | ||
| 92 | }); | 107 | }); |
| 93 | 108 | ||
| 94 | test('wl tl: BoxFit.fitWidth', () { | 109 | test('wl tl: BoxFit.fitWidth', () { |
| 95 | ctx.testScanWindow( | 110 | ctx.testScanWindow( |
| 96 | - BoxFit.fitWidth, const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625)); | 111 | + BoxFit.fitWidth, |
| 112 | + const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625), | ||
| 113 | + ); | ||
| 97 | }); | 114 | }); |
| 98 | 115 | ||
| 99 | test('wl tl: BoxFit.cover', () { | 116 | test('wl tl: BoxFit.cover', () { |
| 100 | // equal to fitWidth | 117 | // equal to fitWidth |
| 101 | ctx.testScanWindow( | 118 | ctx.testScanWindow( |
| 102 | - BoxFit.cover, const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625)); | 119 | + BoxFit.cover, |
| 120 | + const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625), | ||
| 121 | + ); | ||
| 103 | }); | 122 | }); |
| 104 | 123 | ||
| 105 | test('wl tl: BoxFit.contain', () { | 124 | test('wl tl: BoxFit.contain', () { |
| 106 | // equal to fitHeigth | 125 | // equal to fitHeigth |
| 107 | ctx.testScanWindow( | 126 | ctx.testScanWindow( |
| 108 | - BoxFit.contain, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75)); | 127 | + BoxFit.contain, |
| 128 | + const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75), | ||
| 129 | + ); | ||
| 109 | }); | 130 | }); |
| 110 | 131 | ||
| 111 | test('wl tl: BoxFit.scaleDown', () { | 132 | test('wl tl: BoxFit.scaleDown', () { |
| 112 | // equal to fitHeigth, contain | 133 | // equal to fitHeigth, contain |
| 113 | ctx.testScanWindow( | 134 | ctx.testScanWindow( |
| 114 | - BoxFit.scaleDown, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75)); | 135 | + BoxFit.scaleDown, |
| 136 | + const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75), | ||
| 137 | + ); | ||
| 115 | }); | 138 | }); |
| 116 | }); | 139 | }); |
| 117 | }); | 140 | }); |
| @@ -132,8 +155,8 @@ class ScanWindowTestContext { | @@ -132,8 +155,8 @@ class ScanWindowTestContext { | ||
| 132 | final actual = calculateScanWindowRelativeToTextureInPercentage( | 155 | final actual = calculateScanWindowRelativeToTextureInPercentage( |
| 133 | fit, | 156 | fit, |
| 134 | scanWindow, | 157 | scanWindow, |
| 135 | - textureSize, | ||
| 136 | - widgetSize, | 158 | + textureSize: textureSize, |
| 159 | + widgetSize: widgetSize, | ||
| 137 | ); | 160 | ); |
| 138 | 161 | ||
| 139 | // don't use expect(actual, expected) because Rect.toString() only shows one digit after the comma which can be confusing | 162 | // don't use expect(actual, expected) because Rect.toString() only shows one digit after the comma which can be confusing |
-
Please register or login to post a comment