Navaron Bracke

remove stream subscriptions & MediaQueryData.size usages

... ... @@ -2,6 +2,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:mobile_scanner_example/scanned_barcode_label.dart';
import 'package:mobile_scanner_example/scanner_button_widgets.dart';
import 'package:mobile_scanner_example/scanner_error_widget.dart';
... ... @@ -15,8 +16,6 @@ class BarcodeScannerWithController extends StatefulWidget {
class _BarcodeScannerWithControllerState
extends State<BarcodeScannerWithController> {
BarcodeCapture? barcode;
final MobileScannerController controller = MobileScannerController(
torchEnabled: true, useNewCameraSelector: true,
// formats: [BarcodeFormat.qrCode]
... ... @@ -26,21 +25,9 @@ class _BarcodeScannerWithControllerState
// returnImage: false,
);
StreamSubscription<Object?>? _barcodesSubscription;
@override
void initState() {
super.initState();
_barcodesSubscription = controller.barcodes.listen((event) {
if (!context.mounted) {
return;
}
setState(() {
barcode = event;
});
});
controller.start();
}
... ... @@ -69,20 +56,9 @@ class _BarcodeScannerWithControllerState
children: [
ToggleFlashlightButton(controller: controller),
StartStopMobileScannerButton(controller: controller),
Center(
child: SizedBox(
width: MediaQuery.of(context).size.width - 200,
height: 50,
child: FittedBox(
child: Text(
barcode?.barcodes.first.rawValue ?? 'Scan something!',
overflow: TextOverflow.fade,
style: Theme.of(context)
.textTheme
.headlineMedium!
.copyWith(color: Colors.white),
),
),
Expanded(
child: Center(
child: ScannedBarcodeLabel(barcodes: controller.barcodes),
),
),
SwitchCameraButton(controller: controller),
... ... @@ -98,7 +74,6 @@ class _BarcodeScannerWithControllerState
@override
Future<void> dispose() async {
_barcodesSubscription?.cancel();
await controller.dispose();
super.dispose();
}
... ...
... ... @@ -2,6 +2,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:mobile_scanner_example/scanned_barcode_label.dart';
import 'package:mobile_scanner_example/scanner_error_widget.dart';
class BarcodeScannerPageView extends StatefulWidget {
... ... @@ -85,25 +86,7 @@ class _BarcodeScannerPage extends StatelessWidget {
height: 100,
color: Colors.black.withOpacity(0.4),
child: Center(
child: StreamBuilder<BarcodeCapture>(
stream: controller.barcodes,
builder: (context, snapshot) {
final barcodes = snapshot.data?.barcodes;
if (barcodes == null || barcodes.isEmpty) {
return const Text(
'Scan Something!',
style: TextStyle(color: Colors.white, fontSize: 20),
);
}
return Text(
barcodes.first.rawValue ?? 'No raw value',
overflow: TextOverflow.fade,
style: const TextStyle(color: Colors.white),
);
},
),
child: ScannedBarcodeLabel(barcodes: controller.barcodes),
),
),
),
... ...
... ... @@ -2,6 +2,7 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:mobile_scanner_example/scanned_barcode_label.dart';
import 'package:mobile_scanner_example/scanner_button_widgets.dart';
import 'package:mobile_scanner_example/scanner_error_widget.dart';
... ... @@ -116,29 +117,8 @@ class _BarcodeScannerReturningImageState
),
Expanded(
child: Center(
child: StreamBuilder<BarcodeCapture>(
stream: controller.barcodes,
builder: (context, snapshot) {
final barcodes = snapshot.data?.barcodes;
if (barcodes == null || barcodes.isEmpty) {
return const Text(
'Scan something!',
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
);
}
return Text(
barcodes.first.rawValue ?? 'No raw value',
overflow: TextOverflow.fade,
style: const TextStyle(
color: Colors.white,
),
);
},
child: ScannedBarcodeLabel(
barcodes: controller.barcodes,
),
),
),
... ...
... ... @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:mobile_scanner_example/scanned_barcode_label.dart';
import 'package:mobile_scanner_example/scanner_error_widget.dart';
... ... @@ -117,33 +118,7 @@ class _BarcodeScannerWithScanWindowState
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
height: 100,
color: Colors.black.withOpacity(0.4),
child: StreamBuilder<BarcodeCapture>(
stream: controller.barcodes,
builder: (context, snapshot) {
final barcodeCapture = snapshot.data;
String displayValue = 'Scan something!';
if (barcodeCapture != null &&
barcodeCapture.barcodes.isNotEmpty) {
final String? value =
barcodeCapture.barcodes.first.displayValue;
if (value != null) {
displayValue = value;
}
}
return Text(
displayValue,
overflow: TextOverflow.fade,
style: Theme.of(context)
.textTheme
.headlineMedium!
.copyWith(color: Colors.white),
);
},
),
child: ScannedBarcodeLabel(barcodes: controller.barcodes),
),
),
],
... ...
... ... @@ -2,6 +2,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:mobile_scanner_example/scanned_barcode_label.dart';
import 'package:mobile_scanner_example/scanner_button_widgets.dart';
import 'package:mobile_scanner_example/scanner_error_widget.dart';
... ... @@ -14,29 +15,15 @@ class BarcodeScannerWithZoom extends StatefulWidget {
}
class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom> {
BarcodeCapture? barcode;
final MobileScannerController controller = MobileScannerController(
torchEnabled: true,
);
double _zoomFactor = 0.0;
StreamSubscription<Object?>? _barcodesSubscription;
@override
void initState() {
super.initState();
_barcodesSubscription = controller.barcodes.listen((event) {
if (!context.mounted) {
return;
}
setState(() {
barcode = event;
});
});
controller.start();
}
... ... @@ -113,20 +100,10 @@ class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom> {
children: [
ToggleFlashlightButton(controller: controller),
StartStopMobileScannerButton(controller: controller),
Center(
child: SizedBox(
width: MediaQuery.of(context).size.width - 200,
height: 50,
child: FittedBox(
child: Text(
barcode?.barcodes.first.rawValue ??
'Scan something!',
overflow: TextOverflow.fade,
style: Theme.of(context)
.textTheme
.headlineMedium!
.copyWith(color: Colors.white),
),
Expanded(
child: Center(
child: ScannedBarcodeLabel(
barcodes: controller.barcodes,
),
),
),
... ... @@ -145,7 +122,6 @@ class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom> {
@override
Future<void> dispose() async {
_barcodesSubscription?.cancel();
await controller.dispose();
super.dispose();
}
... ...