Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
juliansteenbakker committed Sep 26, 2023
1 parent 86b7ca3 commit 1760f73
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 86 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.0.1
- Fixing null errors resulting from detachment from engine
- Upgraded gradle to version 8
- `QRView.kt`: the allowed barcode types are enforced at the start of `startScan`, and not just filtered afterwards
- Upgraded ZXING

## 1.0.0
Breaking changes:
Minimum Flutter version is now Flutter 3.0.0 (Dart 2.17.0).
Expand Down
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include: package:flutter_lints/flutter.yaml
include: package:lint/package.yaml
2 changes: 1 addition & 1 deletion example/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include: package:flutter_lints/flutter.yaml
include: package:lint/package.yaml
23 changes: 9 additions & 14 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MyHome extends StatelessWidget {
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => const QRViewExample(),
));
),);
},
child: const Text('qrView'),
),
Expand Down Expand Up @@ -58,20 +58,17 @@ class _QRViewExampleState extends State<QRViewExample> {
children: <Widget>[
Expanded(flex: 4, child: _buildQrView(context)),
Expanded(
flex: 1,
child: FittedBox(
fit: BoxFit.contain,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
if (result != null)
Text(
'Barcode Type: ${describeEnum(result!.format)} Data: ${result!.code}')
'Barcode Type: ${describeEnum(result!.format)} Data: ${result!.code}',)
else
const Text('Scan a code'),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin: const EdgeInsets.all(8),
Expand All @@ -85,7 +82,7 @@ class _QRViewExampleState extends State<QRViewExample> {
builder: (context, snapshot) {
return Text('Flash: ${snapshot.data}');
},
)),
),),
),
Container(
margin: const EdgeInsets.all(8),
Expand All @@ -99,18 +96,17 @@ class _QRViewExampleState extends State<QRViewExample> {
builder: (context, snapshot) {
if (snapshot.data != null) {
return Text(
'Camera facing ${describeEnum(snapshot.data!)}');
'Camera facing ${describeEnum(snapshot.data!)}',);
} else {
return const Text('loading');
}
},
)),
),),
)
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin: const EdgeInsets.all(8),
Expand All @@ -119,7 +115,7 @@ class _QRViewExampleState extends State<QRViewExample> {
await controller?.pauseCamera();
},
child: const Text('pause',
style: TextStyle(fontSize: 20)),
style: TextStyle(fontSize: 20),),
),
),
Container(
Expand All @@ -129,7 +125,7 @@ class _QRViewExampleState extends State<QRViewExample> {
await controller?.resumeCamera();
},
child: const Text('resume',
style: TextStyle(fontSize: 20)),
style: TextStyle(fontSize: 20),),
),
)
],
Expand All @@ -145,7 +141,7 @@ class _QRViewExampleState extends State<QRViewExample> {

Widget _buildQrView(BuildContext context) {
// For this example we check how width or tall the device is and change the scanArea and overlay accordingly.
var scanArea = (MediaQuery.of(context).size.width < 400 ||
final scanArea = (MediaQuery.of(context).size.width < 400 ||
MediaQuery.of(context).size.height < 400)
? 150.0
: 300.0;
Expand All @@ -155,11 +151,10 @@ class _QRViewExampleState extends State<QRViewExample> {
key: qrKey,
onQRViewCreated: _onQRViewCreated,
overlay: QrScannerOverlayShape(
borderColor: Colors.red,
borderRadius: 10,
borderLength: 30,
borderWidth: 10,
cutOutSize: scanArea),
cutOutSize: scanArea,),
onPermissionSet: (ctrl, p) => _onPermissionSet(context, ctrl, p),
);
}
Expand Down
3 changes: 2 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ dependencies:
sdk: flutter

dev_dependencies:
flutter_lints: ^1.0.4
lint: ^2.1.2

flutter_test:
sdk: flutter

Expand Down
2 changes: 1 addition & 1 deletion lib/src/lifecycle_event_handler.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class LifecycleEventHandler extends WidgetsBindingObserver {
LifecycleEventHandler({
Expand Down
68 changes: 36 additions & 32 deletions lib/src/qr_code_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'lifecycle_event_handler.dart';
import 'qr_scanner_overlay_shape.dart';
import 'types/barcode.dart';
import 'types/barcode_format.dart';
import 'types/camera.dart';
import 'types/camera_exception.dart';
import 'types/features.dart';
import 'web/flutter_qr_stub.dart'
import 'package:qr_code_scanner/src/lifecycle_event_handler.dart';
import 'package:qr_code_scanner/src/qr_scanner_overlay_shape.dart';
import 'package:qr_code_scanner/src/types/barcode.dart';
import 'package:qr_code_scanner/src/types/barcode_format.dart';
import 'package:qr_code_scanner/src/types/camera.dart';
import 'package:qr_code_scanner/src/types/camera_exception.dart';
import 'package:qr_code_scanner/src/types/features.dart';
import 'package:qr_code_scanner/src/web/flutter_qr_stub.dart'
// ignore: uri_does_not_exist
if (dart.library.html) 'web/flutter_qr_web.dart';

Expand Down Expand Up @@ -87,12 +87,17 @@ class _QRViewState extends State<QRView> {
}

Future<void> updateDimensions() async {
final key = widget.key as GlobalKey<State<StatefulWidget>>?;
if (key == null) {
debugPrint('cannot update dimensions; widget.key is null');
return;
}
await QRViewController.updateDimensions(
widget.key as GlobalKey<State<StatefulWidget>>, _channel,
overlay: widget.overlay);
key, _channel,
overlay: widget.overlay,);
}

bool onNotification(notification) {
bool onNotification(Notification notification) {
updateDimensions();
return false;
}
Expand All @@ -114,17 +119,17 @@ class _QRViewState extends State<QRView> {
}

Widget _getPlatformQrView() {
Widget _platformQrView;
Widget platformQrView;
if (kIsWeb) {
_platformQrView = createWebQrView(
platformQrView = createWebQrView(
onPlatformViewCreated: widget.onQRViewCreated,
onPermissionSet: widget.onPermissionSet,
cameraFacing: widget.cameraFacing,
);
} else {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
_platformQrView = AndroidView(
platformQrView = AndroidView(
viewType: 'net.touchcapture.qr.flutterqr/qrview',
onPlatformViewCreated: _onPlatformViewCreated,
creationParams:
Expand All @@ -133,7 +138,7 @@ class _QRViewState extends State<QRView> {
);
break;
case TargetPlatform.iOS:
_platformQrView = UiKitView(
platformQrView = UiKitView(
viewType: 'net.touchcapture.qr.flutterqr/qrview',
onPlatformViewCreated: _onPlatformViewCreated,
creationParams:
Expand All @@ -143,10 +148,10 @@ class _QRViewState extends State<QRView> {
break;
default:
throw UnsupportedError(
"Trying to use the default qrview implementation for $defaultTargetPlatform but there isn't a default one");
"Trying to use the default qrview implementation for $defaultTargetPlatform but there isn't a default one",);
}
}
return _platformQrView;
return platformQrView;
}

void _onPlatformViewCreated(int id) {
Expand All @@ -155,11 +160,10 @@ class _QRViewState extends State<QRView> {
// Start scan after creation of the view
final controller = QRViewController._(
_channel,
widget.key as GlobalKey<State<StatefulWidget>>?,
widget.onPermissionSet,
widget.cameraFacing)
.._startScan(widget.key as GlobalKey<State<StatefulWidget>>,
widget.overlay, widget.formatsAllowed);
widget.cameraFacing,)
.._startScan((widget.key as GlobalKey<State<StatefulWidget>>?)!,
widget.overlay, widget.formatsAllowed,);

// Initialize the controller for controlling the QRView
widget.onQRViewCreated(controller);
Expand All @@ -181,8 +185,8 @@ class _QrCameraSettings {
}

class QRViewController {
QRViewController._(MethodChannel channel, GlobalKey? qrKey,
PermissionSetCallback? onPermissionSet, CameraFacing cameraFacing)
QRViewController._(MethodChannel channel,
PermissionSetCallback? onPermissionSet, CameraFacing cameraFacing,)
: _channel = channel,
_cameraFacing = cameraFacing {
_channel.setMethodCallHandler((call) async {
Expand All @@ -205,7 +209,7 @@ class QRViewController {
break;
case 'onPermissionSet':
if (call.arguments != null && call.arguments is bool) {
_hasPermissions = call.arguments;
_hasPermissions = call.arguments as bool;
if (onPermissionSet != null) {
onPermissionSet(this, _hasPermissions);
}
Expand All @@ -227,12 +231,12 @@ class QRViewController {

/// Starts the barcode scanner
Future<void> _startScan(GlobalKey key, QrScannerOverlayShape? overlay,
List<BarcodeFormat>? barcodeFormats) async {
List<BarcodeFormat>? barcodeFormats,) async {
// We need to update the dimension before the scan is started.
try {
await QRViewController.updateDimensions(key, _channel, overlay: overlay);
return await _channel.invokeMethod(
'startScan', barcodeFormats?.map((e) => e.asInt()).toList() ?? []);
'startScan', barcodeFormats?.map((e) => e.asInt()).toList() ?? [],);
} on PlatformException catch (e) {
throw CameraException(e.code, e.message);
}
Expand All @@ -241,7 +245,7 @@ class QRViewController {
/// Gets information about which camera is active.
Future<CameraFacing> getCameraInfo() async {
try {
var cameraFacing = await _channel.invokeMethod('getCameraInfo') as int;
final cameraFacing = await _channel.invokeMethod('getCameraInfo') as int;
if (cameraFacing == -1) return _cameraFacing;
return CameraFacing
.values[await _channel.invokeMethod('getCameraInfo') as int];
Expand Down Expand Up @@ -308,7 +312,7 @@ class QRViewController {
/// Returns which features are available on device.
Future<SystemFeatures> getSystemFeatures() async {
try {
var features =
final features =
await _channel.invokeMapMethod<String, dynamic>('getSystemFeatures');
if (features != null) {
return SystemFeatures.fromJson(features);
Expand All @@ -327,16 +331,16 @@ class QRViewController {

/// Updates the view dimensions for iOS.
static Future<bool> updateDimensions(GlobalKey key, MethodChannel channel,
{QrScannerOverlayShape? overlay}) async {
{QrScannerOverlayShape? overlay,}) async {
if (defaultTargetPlatform == TargetPlatform.iOS) {
// Add small delay to ensure the render box is loaded
await Future.delayed(const Duration(milliseconds: 300));
if (key.currentContext == null) return false;
final renderBox = key.currentContext!.findRenderObject() as RenderBox;
final renderBox = key.currentContext!.findRenderObject() as RenderBox?;
try {
await channel.invokeMethod('setDimensions', {
'width': renderBox.size.width,
'height': renderBox.size.height,
'width': renderBox?.size.width,
'height': renderBox?.size.height,
'scanAreaWidth': overlay?.cutOutWidth ?? 0,
'scanAreaHeight': overlay?.cutOutHeight ?? 0,
'scanAreaOffset': overlay?.cutOutBottomOffset ?? 0
Expand Down
Loading

0 comments on commit 1760f73

Please sign in to comment.