Navaron Bracke

remove stream subscriptions & MediaQueryData.size usages

@@ -2,6 +2,7 @@ import 'dart:async'; @@ -2,6 +2,7 @@ import 'dart:async';
2 2
3 import 'package:flutter/material.dart'; 3 import 'package:flutter/material.dart';
4 import 'package:mobile_scanner/mobile_scanner.dart'; 4 import 'package:mobile_scanner/mobile_scanner.dart';
  5 +import 'package:mobile_scanner_example/scanned_barcode_label.dart';
5 import 'package:mobile_scanner_example/scanner_button_widgets.dart'; 6 import 'package:mobile_scanner_example/scanner_button_widgets.dart';
6 import 'package:mobile_scanner_example/scanner_error_widget.dart'; 7 import 'package:mobile_scanner_example/scanner_error_widget.dart';
7 8
@@ -15,8 +16,6 @@ class BarcodeScannerWithController extends StatefulWidget { @@ -15,8 +16,6 @@ class BarcodeScannerWithController extends StatefulWidget {
15 16
16 class _BarcodeScannerWithControllerState 17 class _BarcodeScannerWithControllerState
17 extends State<BarcodeScannerWithController> { 18 extends State<BarcodeScannerWithController> {
18 - BarcodeCapture? barcode;  
19 -  
20 final MobileScannerController controller = MobileScannerController( 19 final MobileScannerController controller = MobileScannerController(
21 torchEnabled: true, useNewCameraSelector: true, 20 torchEnabled: true, useNewCameraSelector: true,
22 // formats: [BarcodeFormat.qrCode] 21 // formats: [BarcodeFormat.qrCode]
@@ -26,21 +25,9 @@ class _BarcodeScannerWithControllerState @@ -26,21 +25,9 @@ class _BarcodeScannerWithControllerState
26 // returnImage: false, 25 // returnImage: false,
27 ); 26 );
28 27
29 - StreamSubscription<Object?>? _barcodesSubscription;  
30 -  
31 @override 28 @override
32 void initState() { 29 void initState() {
33 super.initState(); 30 super.initState();
34 - _barcodesSubscription = controller.barcodes.listen((event) {  
35 - if (!context.mounted) {  
36 - return;  
37 - }  
38 -  
39 - setState(() {  
40 - barcode = event;  
41 - });  
42 - });  
43 -  
44 controller.start(); 31 controller.start();
45 } 32 }
46 33
@@ -69,20 +56,9 @@ class _BarcodeScannerWithControllerState @@ -69,20 +56,9 @@ class _BarcodeScannerWithControllerState
69 children: [ 56 children: [
70 ToggleFlashlightButton(controller: controller), 57 ToggleFlashlightButton(controller: controller),
71 StartStopMobileScannerButton(controller: controller), 58 StartStopMobileScannerButton(controller: controller),
72 - Center(  
73 - child: SizedBox(  
74 - width: MediaQuery.of(context).size.width - 200,  
75 - height: 50,  
76 - child: FittedBox(  
77 - child: Text(  
78 - barcode?.barcodes.first.rawValue ?? 'Scan something!',  
79 - overflow: TextOverflow.fade,  
80 - style: Theme.of(context)  
81 - .textTheme  
82 - .headlineMedium!  
83 - .copyWith(color: Colors.white),  
84 - ),  
85 - ), 59 + Expanded(
  60 + child: Center(
  61 + child: ScannedBarcodeLabel(barcodes: controller.barcodes),
86 ), 62 ),
87 ), 63 ),
88 SwitchCameraButton(controller: controller), 64 SwitchCameraButton(controller: controller),
@@ -98,7 +74,6 @@ class _BarcodeScannerWithControllerState @@ -98,7 +74,6 @@ class _BarcodeScannerWithControllerState
98 74
99 @override 75 @override
100 Future<void> dispose() async { 76 Future<void> dispose() async {
101 - _barcodesSubscription?.cancel();  
102 await controller.dispose(); 77 await controller.dispose();
103 super.dispose(); 78 super.dispose();
104 } 79 }
@@ -2,6 +2,7 @@ import 'dart:async'; @@ -2,6 +2,7 @@ import 'dart:async';
2 2
3 import 'package:flutter/material.dart'; 3 import 'package:flutter/material.dart';
4 import 'package:mobile_scanner/mobile_scanner.dart'; 4 import 'package:mobile_scanner/mobile_scanner.dart';
  5 +import 'package:mobile_scanner_example/scanned_barcode_label.dart';
5 import 'package:mobile_scanner_example/scanner_error_widget.dart'; 6 import 'package:mobile_scanner_example/scanner_error_widget.dart';
6 7
7 class BarcodeScannerPageView extends StatefulWidget { 8 class BarcodeScannerPageView extends StatefulWidget {
@@ -85,25 +86,7 @@ class _BarcodeScannerPage extends StatelessWidget { @@ -85,25 +86,7 @@ class _BarcodeScannerPage extends StatelessWidget {
85 height: 100, 86 height: 100,
86 color: Colors.black.withOpacity(0.4), 87 color: Colors.black.withOpacity(0.4),
87 child: Center( 88 child: Center(
88 - child: StreamBuilder<BarcodeCapture>(  
89 - stream: controller.barcodes,  
90 - builder: (context, snapshot) {  
91 - final barcodes = snapshot.data?.barcodes;  
92 -  
93 - if (barcodes == null || barcodes.isEmpty) {  
94 - return const Text(  
95 - 'Scan Something!',  
96 - style: TextStyle(color: Colors.white, fontSize: 20),  
97 - );  
98 - }  
99 -  
100 - return Text(  
101 - barcodes.first.rawValue ?? 'No raw value',  
102 - overflow: TextOverflow.fade,  
103 - style: const TextStyle(color: Colors.white),  
104 - );  
105 - },  
106 - ), 89 + child: ScannedBarcodeLabel(barcodes: controller.barcodes),
107 ), 90 ),
108 ), 91 ),
109 ), 92 ),
@@ -2,6 +2,7 @@ import 'dart:math'; @@ -2,6 +2,7 @@ import 'dart:math';
2 2
3 import 'package:flutter/material.dart'; 3 import 'package:flutter/material.dart';
4 import 'package:mobile_scanner/mobile_scanner.dart'; 4 import 'package:mobile_scanner/mobile_scanner.dart';
  5 +import 'package:mobile_scanner_example/scanned_barcode_label.dart';
5 import 'package:mobile_scanner_example/scanner_button_widgets.dart'; 6 import 'package:mobile_scanner_example/scanner_button_widgets.dart';
6 import 'package:mobile_scanner_example/scanner_error_widget.dart'; 7 import 'package:mobile_scanner_example/scanner_error_widget.dart';
7 8
@@ -116,29 +117,8 @@ class _BarcodeScannerReturningImageState @@ -116,29 +117,8 @@ class _BarcodeScannerReturningImageState
116 ), 117 ),
117 Expanded( 118 Expanded(
118 child: Center( 119 child: Center(
119 - child: StreamBuilder<BarcodeCapture>(  
120 - stream: controller.barcodes,  
121 - builder: (context, snapshot) {  
122 - final barcodes = snapshot.data?.barcodes;  
123 -  
124 - if (barcodes == null || barcodes.isEmpty) {  
125 - return const Text(  
126 - 'Scan something!',  
127 - style: TextStyle(  
128 - color: Colors.white,  
129 - fontSize: 20,  
130 - ),  
131 - );  
132 - }  
133 -  
134 - return Text(  
135 - barcodes.first.rawValue ?? 'No raw value',  
136 - overflow: TextOverflow.fade,  
137 - style: const TextStyle(  
138 - color: Colors.white,  
139 - ),  
140 - );  
141 - }, 120 + child: ScannedBarcodeLabel(
  121 + barcodes: controller.barcodes,
142 ), 122 ),
143 ), 123 ),
144 ), 124 ),
@@ -3,6 +3,7 @@ import 'dart:io'; @@ -3,6 +3,7 @@ import 'dart:io';
3 import 'package:flutter/foundation.dart'; 3 import 'package:flutter/foundation.dart';
4 import 'package:flutter/material.dart'; 4 import 'package:flutter/material.dart';
5 import 'package:mobile_scanner/mobile_scanner.dart'; 5 import 'package:mobile_scanner/mobile_scanner.dart';
  6 +import 'package:mobile_scanner_example/scanned_barcode_label.dart';
6 7
7 import 'package:mobile_scanner_example/scanner_error_widget.dart'; 8 import 'package:mobile_scanner_example/scanner_error_widget.dart';
8 9
@@ -117,33 +118,7 @@ class _BarcodeScannerWithScanWindowState @@ -117,33 +118,7 @@ class _BarcodeScannerWithScanWindowState
117 padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), 118 padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
118 height: 100, 119 height: 100,
119 color: Colors.black.withOpacity(0.4), 120 color: Colors.black.withOpacity(0.4),
120 - child: StreamBuilder<BarcodeCapture>(  
121 - stream: controller.barcodes,  
122 - builder: (context, snapshot) {  
123 - final barcodeCapture = snapshot.data;  
124 -  
125 - String displayValue = 'Scan something!';  
126 -  
127 - if (barcodeCapture != null &&  
128 - barcodeCapture.barcodes.isNotEmpty) {  
129 - final String? value =  
130 - barcodeCapture.barcodes.first.displayValue;  
131 -  
132 - if (value != null) {  
133 - displayValue = value;  
134 - }  
135 - }  
136 -  
137 - return Text(  
138 - displayValue,  
139 - overflow: TextOverflow.fade,  
140 - style: Theme.of(context)  
141 - .textTheme  
142 - .headlineMedium!  
143 - .copyWith(color: Colors.white),  
144 - );  
145 - },  
146 - ), 121 + child: ScannedBarcodeLabel(barcodes: controller.barcodes),
147 ), 122 ),
148 ), 123 ),
149 ], 124 ],
@@ -2,6 +2,7 @@ import 'dart:async'; @@ -2,6 +2,7 @@ import 'dart:async';
2 2
3 import 'package:flutter/material.dart'; 3 import 'package:flutter/material.dart';
4 import 'package:mobile_scanner/mobile_scanner.dart'; 4 import 'package:mobile_scanner/mobile_scanner.dart';
  5 +import 'package:mobile_scanner_example/scanned_barcode_label.dart';
5 6
6 import 'package:mobile_scanner_example/scanner_button_widgets.dart'; 7 import 'package:mobile_scanner_example/scanner_button_widgets.dart';
7 import 'package:mobile_scanner_example/scanner_error_widget.dart'; 8 import 'package:mobile_scanner_example/scanner_error_widget.dart';
@@ -14,29 +15,15 @@ class BarcodeScannerWithZoom extends StatefulWidget { @@ -14,29 +15,15 @@ class BarcodeScannerWithZoom extends StatefulWidget {
14 } 15 }
15 16
16 class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom> { 17 class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom> {
17 - BarcodeCapture? barcode;  
18 -  
19 final MobileScannerController controller = MobileScannerController( 18 final MobileScannerController controller = MobileScannerController(
20 torchEnabled: true, 19 torchEnabled: true,
21 ); 20 );
22 21
23 double _zoomFactor = 0.0; 22 double _zoomFactor = 0.0;
24 23
25 - StreamSubscription<Object?>? _barcodesSubscription;  
26 -  
27 @override 24 @override
28 void initState() { 25 void initState() {
29 super.initState(); 26 super.initState();
30 - _barcodesSubscription = controller.barcodes.listen((event) {  
31 - if (!context.mounted) {  
32 - return;  
33 - }  
34 -  
35 - setState(() {  
36 - barcode = event;  
37 - });  
38 - });  
39 -  
40 controller.start(); 27 controller.start();
41 } 28 }
42 29
@@ -113,20 +100,10 @@ class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom> { @@ -113,20 +100,10 @@ class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom> {
113 children: [ 100 children: [
114 ToggleFlashlightButton(controller: controller), 101 ToggleFlashlightButton(controller: controller),
115 StartStopMobileScannerButton(controller: controller), 102 StartStopMobileScannerButton(controller: controller),
116 - Center(  
117 - child: SizedBox(  
118 - width: MediaQuery.of(context).size.width - 200,  
119 - height: 50,  
120 - child: FittedBox(  
121 - child: Text(  
122 - barcode?.barcodes.first.rawValue ??  
123 - 'Scan something!',  
124 - overflow: TextOverflow.fade,  
125 - style: Theme.of(context)  
126 - .textTheme  
127 - .headlineMedium!  
128 - .copyWith(color: Colors.white),  
129 - ), 103 + Expanded(
  104 + child: Center(
  105 + child: ScannedBarcodeLabel(
  106 + barcodes: controller.barcodes,
130 ), 107 ),
131 ), 108 ),
132 ), 109 ),
@@ -145,7 +122,6 @@ class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom> { @@ -145,7 +122,6 @@ class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom> {
145 122
146 @override 123 @override
147 Future<void> dispose() async { 124 Future<void> dispose() async {
148 - _barcodesSubscription?.cancel();  
149 await controller.dispose(); 125 await controller.dispose();
150 super.dispose(); 126 super.dispose();
151 } 127 }