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_zxing/flutter_zxing.dart';
@ -16,35 +18,73 @@ class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
FlutterZxing.setLogEnabled(true);
return MaterialApp(
return const MaterialApp(
title: 'Flutter Zxing Example',
home: DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: const Text('Flutter Zxing Example'),
bottom: const TabBar(
tabs: [
Tab(text: 'Scan Code'),
Tab(text: 'Create Code'),
],
),
),
body: TabBarView(
physics: const NeverScrollableScrollPhysics(),
children: [
ReaderWidget(
onScan: (value) {
debugPrint(value.textString ?? '');
},
),
WriterWidget(
onSuccess: (result, bytes) {},
onError: (error) {},
),
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,
child: Scaffold(
appBar: AppBar(
title: const Text('Flutter Zxing Example'),
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>
with TickerProviderStateMixin {
late List<CameraDescription> cameras;
List<CameraDescription>? cameras;
CameraController? controller;
bool isAndroid() => Theme.of(context).platform == TargetPlatform.android;
@ -195,7 +195,10 @@ class _ReaderWidgetState extends State<ReaderWidget>
// Display the preview from the camera.
Widget _cameraPreviewWidget(double cropSize) {
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();
} else {
final size = MediaQuery.of(context).size;

3
lib/writer_widget.dart

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

Loading…
Cancel
Save