Browse Source

updated example

pull/3/head
Khoren Markosyan 3 years ago
parent
commit
4ecab6c3a9
  1. 50
      example/lib/main.dart
  2. 7
      lib/reader_widget.dart
  3. 3
      lib/writer_widget.dart

50
example/lib/main.dart

@ -1,3 +1,5 @@
import 'dart:typed_data';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_zxing/flutter_zxing.dart'; import 'package:flutter_zxing/flutter_zxing.dart';
@ -16,9 +18,26 @@ class _MyAppState extends State<MyApp> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
FlutterZxing.setLogEnabled(true); FlutterZxing.setLogEnabled(true);
return MaterialApp( return const MaterialApp(
title: 'Flutter Zxing Example', title: 'Flutter Zxing Example',
home: DefaultTabController( home: DemoPage(),
);
}
}
class DemoPage extends StatefulWidget {
const DemoPage({Key? key}) : super(key: key);
@override
State<DemoPage> createState() => _DemoPageState();
}
class _DemoPageState extends State<DemoPage> {
Uint8List? createdCodeBytes;
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2, length: 2,
child: Scaffold( child: Scaffold(
appBar: AppBar( appBar: AppBar(
@ -35,16 +54,37 @@ class _MyAppState extends State<MyApp> {
children: [ children: [
ReaderWidget( ReaderWidget(
onScan: (value) { onScan: (value) {
debugPrint(value.textString ?? ''); showMessage(context, 'Scanned: ${value.textString ?? ''}');
}, },
), ),
ListView(
children: [
WriterWidget( WriterWidget(
onSuccess: (result, bytes) {}, onSuccess: (result, bytes) {
onError: (error) {}, setState(() {
createdCodeBytes = bytes;
});
},
onError: (error) {
showMessage(context, 'Error: $error');
},
),
if (createdCodeBytes != null)
Image.memory(createdCodeBytes ?? Uint8List(0), height: 200),
],
), ),
], ],
), ),
), ),
);
}
showMessage(BuildContext context, String message) {
debugPrint(message);
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
), ),
); );
} }

7
lib/reader_widget.dart

@ -39,7 +39,7 @@ class ReaderWidget extends StatefulWidget {
class _ReaderWidgetState extends State<ReaderWidget> class _ReaderWidgetState extends State<ReaderWidget>
with TickerProviderStateMixin { with TickerProviderStateMixin {
late List<CameraDescription> cameras; List<CameraDescription>? cameras;
CameraController? controller; CameraController? controller;
bool isAndroid() => Theme.of(context).platform == TargetPlatform.android; bool isAndroid() => Theme.of(context).platform == TargetPlatform.android;
@ -195,7 +195,10 @@ class _ReaderWidgetState extends State<ReaderWidget>
// Display the preview from the camera. // Display the preview from the camera.
Widget _cameraPreviewWidget(double cropSize) { Widget _cameraPreviewWidget(double cropSize) {
final CameraController? cameraController = controller; final CameraController? cameraController = controller;
if (cameraController == null || !cameraController.value.isInitialized) { if (cameras != null && cameras?.isEmpty == true) {
return const Text('No cameras found');
} else if (cameraController == null ||
!cameraController.value.isInitialized) {
return const CircularProgressIndicator(); return const CircularProgressIndicator();
} else { } else {
final size = MediaQuery.of(context).size; final size = MediaQuery.of(context).size;

3
lib/writer_widget.dart

@ -170,7 +170,7 @@ class _WriterWidgetState extends State<WriterWidget>
onPressed: createBarcode, onPressed: createBarcode,
child: const Text('Create'), child: const Text('Create'),
), ),
const SizedBox(height: 20), const SizedBox(height: 10),
], ],
), ),
), ),
@ -202,7 +202,6 @@ class _WriterWidgetState extends State<WriterWidget>
error = result.errorMessage; error = result.errorMessage;
} }
if (error != null) { if (error != null) {
debugPrint(error);
widget.onError?.call(error); widget.onError?.call(error);
} }
} }

Loading…
Cancel
Save