Navaron Bracke

use named parameters; add commas for formatting

... ... @@ -193,8 +193,8 @@ class _MobileScannerState extends State<MobileScanner>
scanWindow = calculateScanWindowRelativeToTextureInPercentage(
widget.fit,
widget.scanWindow!,
value.size,
constraints.biggest,
textureSize: value.size,
widgetSize: constraints.biggest,
);
_controller.updateScanWindow(scanWindow);
... ...
... ... @@ -18,11 +18,11 @@ import 'package:flutter/rendering.dart';
/// Returns a [Rect] that represents the position and size of the scan window in the texture.
Rect calculateScanWindowRelativeToTextureInPercentage(
BoxFit fit,
Rect scanWindow,
Size textureSize,
Size widgetSize,
) {
/// map the texture size to get its new size after fitted to screen
Rect scanWindow, {
required Size textureSize,
required Size widgetSize,
}) {
// Convert the texture size to a size in widget-space, with the box fit applied.
final fittedTextureSize = applyBoxFit(fit, textureSize, widgetSize);
// Get the correct scaling values depending on the given BoxFit mode
... ... @@ -76,8 +76,9 @@ Rect calculateScanWindowRelativeToTextureInPercentage(
// Clip the scan window in texture coordinates with the texture bounds.
// This prevents percentages outside the range [0; 1].
final clippedScanWndInTexSpace = scanWindowInTexSpace
.intersect(Rect.fromLTWH(0, 0, textureSize.width, textureSize.height));
final clippedScanWndInTexSpace = scanWindowInTexSpace.intersect(
Rect.fromLTWH(0, 0, textureSize.width, textureSize.height),
);
// Compute relative rectangle coordinates,
// with respect to the texture size, i.e. scan image.
... ...
... ... @@ -20,49 +20,58 @@ void main() {
test('wl tp: BoxFit.none', () {
ctx.testScanWindow(
BoxFit.none, const Rect.fromLTRB(0.275, 0.4, 0.725, 0.6));
BoxFit.none,
const Rect.fromLTRB(0.275, 0.4, 0.725, 0.6),
);
});
test('wl tp: BoxFit.fill', () {
ctx.testScanWindow(
BoxFit.fill, const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75));
BoxFit.fill,
const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75),
);
});
test('wl tp: BoxFit.fitHeight', () {
ctx.testScanWindow(
BoxFit.fitHeight, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75));
BoxFit.fitHeight,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
test('wl tp: BoxFit.fitWidth', () {
ctx.testScanWindow(
BoxFit.fitWidth,
const Rect.fromLTRB(
0.25, 0.38888888888888895, 0.75, 0.6111111111111112));
BoxFit.fitWidth,
const Rect.fromLTRB(0.25, 0.38888888888888895, 0.75, 0.6111111111111112),
);
});
test('wl tp: BoxFit.cover', () {
// equal to fitWidth
ctx.testScanWindow(
BoxFit.cover,
const Rect.fromLTRB(
0.25, 0.38888888888888895, 0.75, 0.6111111111111112));
BoxFit.cover,
const Rect.fromLTRB(0.25, 0.38888888888888895, 0.75, 0.6111111111111112),
);
});
test('wl tp: BoxFit.contain', () {
// equal to fitHeigth
ctx.testScanWindow(
BoxFit.contain, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75));
BoxFit.contain,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
test('wl tp: BoxFit.scaleDown', () {
// equal to fitHeigth, contain
ctx.testScanWindow(
BoxFit.scaleDown, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75));
BoxFit.scaleDown,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
});
group('Widget (landscape) smaller than texture and texture (landscape)',
() {
group('Widget (landscape) smaller than texture and texture (landscape)', () {
const textureSize = Size(640.0, 480.0);
const widgetSize = Size(320.0, 120.0);
final ctx = ScanWindowTestContext(
... ... @@ -78,40 +87,54 @@ void main() {
test('wl tl: BoxFit.none', () {
ctx.testScanWindow(
BoxFit.none, const Rect.fromLTRB(0.375, 0.4375, 0.625, 0.5625));
BoxFit.none,
const Rect.fromLTRB(0.375, 0.4375, 0.625, 0.5625),
);
});
test('wl tl: BoxFit.fill', () {
ctx.testScanWindow(
BoxFit.fill, const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75));
BoxFit.fill,
const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75),
);
});
test('wl tl: BoxFit.fitHeight', () {
ctx.testScanWindow(
BoxFit.fitHeight, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75));
BoxFit.fitHeight,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
test('wl tl: BoxFit.fitWidth', () {
ctx.testScanWindow(
BoxFit.fitWidth, const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625));
BoxFit.fitWidth,
const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625),
);
});
test('wl tl: BoxFit.cover', () {
// equal to fitWidth
ctx.testScanWindow(
BoxFit.cover, const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625));
BoxFit.cover,
const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625),
);
});
test('wl tl: BoxFit.contain', () {
// equal to fitHeigth
ctx.testScanWindow(
BoxFit.contain, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75));
BoxFit.contain,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
test('wl tl: BoxFit.scaleDown', () {
// equal to fitHeigth, contain
ctx.testScanWindow(
BoxFit.scaleDown, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75));
BoxFit.scaleDown,
const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75),
);
});
});
});
... ... @@ -132,8 +155,8 @@ class ScanWindowTestContext {
final actual = calculateScanWindowRelativeToTextureInPercentage(
fit,
scanWindow,
textureSize,
widgetSize,
textureSize: textureSize,
widgetSize: widgetSize,
);
// don't use expect(actual, expected) because Rect.toString() only shows one digit after the comma which can be confusing
... ...