Navaron Bracke
Committed by GitHub

Merge pull request #977 from navaronbracke/cherry-pick_scan_window_fix

fix: port scan window update fix to the new beta release
## NEXT
Bugs fixed:
* Fixed an issue where the scan window was not updated when its size was changed. (thanks @navaronbracke !)
## 5.0.0-beta.1
**BREAKING CHANGES:**
... ...
... ... @@ -24,6 +24,7 @@ class MobileScanner extends StatefulWidget {
this.overlayBuilder,
this.placeholderBuilder,
this.scanWindow,
this.scanWindowUpdateThreshold = 0.0,
super.key,
});
... ... @@ -94,6 +95,19 @@ class MobileScanner extends StatefulWidget {
/// ```
final Rect? scanWindow;
/// The threshold for updates to the [scanWindow].
///
/// If the [scanWindow] would be updated,
/// due to new layout constraints for the scanner,
/// and the width or height of the new scan window have not changed by this threshold,
/// then the scan window is not updated.
///
/// It is recommended to set this threshold
/// if scan window updates cause performance issues.
///
/// Defaults to no threshold for scan window updates.
final double scanWindowUpdateThreshold;
@override
State<MobileScanner> createState() => _MobileScannerState();
}
... ... @@ -109,13 +123,49 @@ class _MobileScannerState extends State<MobileScanner> {
MobileScannerState scannerState,
BoxConstraints constraints,
) {
if (widget.scanWindow != null && scanWindow == null) {
scanWindow = calculateScanWindowRelativeToTextureInPercentage(
widget.fit,
widget.scanWindow!,
textureSize: scannerState.size,
widgetSize: constraints.biggest,
);
if (widget.scanWindow == null) {
return;
}
final Rect newScanWindow = calculateScanWindowRelativeToTextureInPercentage(
widget.fit,
widget.scanWindow!,
textureSize: scannerState.size,
widgetSize: constraints.biggest,
);
// The scan window was never set before.
// Set the initial scan window.
if (scanWindow == null) {
scanWindow = newScanWindow;
unawaited(widget.controller.updateScanWindow(scanWindow));
return;
}
// The scan window did not not change.
// The left, right, top and bottom are the same.
if (scanWindow == newScanWindow) {
return;
}
// The update threshold is not set, allow updating the scan window.
if (widget.scanWindowUpdateThreshold == 0.0) {
scanWindow = newScanWindow;
unawaited(widget.controller.updateScanWindow(scanWindow));
return;
}
final double dx = (newScanWindow.width - scanWindow!.width).abs();
final double dy = (newScanWindow.height - scanWindow!.height).abs();
// The new scan window has changed enough, allow updating the scan window.
if (dx >= widget.scanWindowUpdateThreshold ||
dy >= widget.scanWindowUpdateThreshold) {
scanWindow = newScanWindow;
unawaited(widget.controller.updateScanWindow(scanWindow));
}
... ...