Navaron Bracke

fix the return image sample

... ... @@ -2,6 +2,7 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:mobile_scanner_example/scanner_button_widgets.dart';
import 'package:mobile_scanner_example/scanner_error_widget.dart';
class BarcodeScannerReturningImage extends StatefulWidget {
... ... @@ -13,11 +14,7 @@ class BarcodeScannerReturningImage extends StatefulWidget {
}
class _BarcodeScannerReturningImageState
extends State<BarcodeScannerReturningImage>
with SingleTickerProviderStateMixin {
BarcodeCapture? barcode;
// MobileScannerArguments? arguments;
extends State<BarcodeScannerReturningImage> {
final MobileScannerController controller = MobileScannerController(
torchEnabled: true,
// formats: [BarcodeFormat.qrCode]
... ... @@ -27,27 +24,11 @@ class _BarcodeScannerReturningImageState
returnImage: true,
);
bool isStarted = true;
void _startOrStop() {
try {
if (isStarted) {
controller.stop();
} else {
@override
void initState() {
super.initState();
controller.start();
}
setState(() {
isStarted = !isStarted;
});
} on Exception catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Something went wrong! $e'),
backgroundColor: Colors.red,
),
);
}
}
@override
Widget build(BuildContext context) {
... ... @@ -57,19 +38,54 @@ class _BarcodeScannerReturningImageState
child: Column(
children: [
Expanded(
child: barcode?.image != null
? Transform.rotate(
angle: 90 * pi / 180,
child: Image(
gaplessPlayback: true,
image: MemoryImage(barcode!.image!),
fit: BoxFit.contain,
),
)
: const Center(
child: StreamBuilder<BarcodeCapture>(
stream: controller.barcodes,
builder: (context, snapshot) {
final barcode = snapshot.data;
if (barcode == null) {
return const Center(
child: Text(
'Your scanned barcode will appear here!',
),
);
}
final barcodeImage = barcode.image;
if (barcodeImage == null) {
return const Center(
child: Text('No image for this barcode.'),
);
}
return Image.memory(
barcodeImage,
fit: BoxFit.contain,
errorBuilder: (context, error, stackTrace) {
return Center(
child: Text('Could not decode image bytes. $error'),
);
},
frameBuilder: (
BuildContext context,
Widget child,
int? frame,
bool? wasSynchronouslyLoaded,
) {
if (wasSynchronouslyLoaded == true || frame != null) {
return Transform.rotate(
angle: 90 * pi / 180,
child: child,
);
}
return const Center(
child: CircularProgressIndicator(),
);
},
);
},
),
),
Expanded(
... ... @@ -84,11 +100,6 @@ class _BarcodeScannerReturningImageState
return ScannerErrorWidget(error: error);
},
fit: BoxFit.contain,
onDetect: (barcode) {
setState(() {
this.barcode = barcode;
});
},
),
Align(
alignment: Alignment.bottomCenter,
... ... @@ -99,69 +110,39 @@ class _BarcodeScannerReturningImageState
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
ToggleFlashlightButton(controller: controller),
StartStopMobileScannerButton(
controller: controller,
),
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,
icon: ValueListenableBuilder<TorchState>(
valueListenable: controller.torchState,
builder: (context, state, child) {
switch (state) {
case TorchState.off:
return const Icon(
Icons.flash_off,
color: Colors.grey,
);
case TorchState.on:
return const Icon(
Icons.flash_on,
color: Colors.yellow,
fontSize: 20,
),
);
}
},
),
iconSize: 32.0,
onPressed: () => controller.toggleTorch(),
),
IconButton(
color: Colors.white,
icon: isStarted
? const Icon(Icons.stop)
: const Icon(Icons.play_arrow),
iconSize: 32.0,
onPressed: _startOrStop,
),
Center(
child: SizedBox(
width: MediaQuery.of(context).size.width - 200,
height: 50,
child: FittedBox(
child: Text(
barcode?.barcodes.first.rawValue ??
'Scan something!',
return Text(
barcodes.first.rawValue ?? 'No raw value',
overflow: TextOverflow.fade,
style: Theme.of(context)
.textTheme
.headlineMedium!
.copyWith(color: Colors.white),
),
),
),
),
IconButton(
style: const TextStyle(
color: Colors.white,
icon: ValueListenableBuilder<CameraFacing>(
valueListenable: controller.cameraFacingState,
builder: (context, state, child) {
switch (state) {
case CameraFacing.front:
return const Icon(Icons.camera_front);
case CameraFacing.back:
return const Icon(Icons.camera_rear);
}
),
);
},
),
iconSize: 32.0,
onPressed: () => controller.switchCamera(),
),
),
SwitchCameraButton(controller: controller),
],
),
),
... ... @@ -177,8 +158,8 @@ class _BarcodeScannerReturningImageState
}
@override
void dispose() {
controller.dispose();
Future<void> dispose() async {
await controller.dispose();
super.dispose();
}
}
... ...