Browse Source

updated example

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

90
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,35 +18,73 @@ 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(),
length: 2, );
child: Scaffold( }
appBar: AppBar( }
title: const Text('Flutter Zxing Example'),
bottom: const TabBar( class DemoPage extends StatefulWidget {
tabs: [ const DemoPage({Key? key}) : super(key: key);
Tab(text: 'Scan Code'),
Tab(text: 'Create Code'), @override
], State<DemoPage> createState() => _DemoPageState();
), }
),
body: TabBarView( class _DemoPageState extends State<DemoPage> {
physics: const NeverScrollableScrollPhysics(), Uint8List? createdCodeBytes;
children: [
ReaderWidget( @override
onScan: (value) { Widget build(BuildContext context) {
debugPrint(value.textString ?? ''); return DefaultTabController(
}, length: 2,
), child: Scaffold(
WriterWidget( appBar: AppBar(
onSuccess: (result, bytes) {}, title: const Text('Flutter Zxing Example'),
onError: (error) {}, bottom: const TabBar(
), tabs: [
Tab(text: 'Scan Code'),
Tab(text: 'Create Code'),
], ],
), ),
), ),
body: TabBarView(
physics: const NeverScrollableScrollPhysics(),
children: [
ReaderWidget(
onScan: (value) {
showMessage(context, 'Scanned: ${value.textString ?? ''}');
},
),
ListView(
children: [
WriterWidget(
onSuccess: (result, bytes) {
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