diff --git a/example/lib/main.dart b/example/lib/main.dart index 98456b3..9516e8e 100644 --- a/example/lib/main.dart +++ b/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 { @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 createState() => _DemoPageState(); +} + +class _DemoPageState extends State { + 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), ), ); } diff --git a/lib/reader_widget.dart b/lib/reader_widget.dart index 6a3646a..b31b6e4 100644 --- a/lib/reader_widget.dart +++ b/lib/reader_widget.dart @@ -39,7 +39,7 @@ class ReaderWidget extends StatefulWidget { class _ReaderWidgetState extends State with TickerProviderStateMixin { - late List cameras; + List? cameras; CameraController? controller; bool isAndroid() => Theme.of(context).platform == TargetPlatform.android; @@ -195,7 +195,10 @@ class _ReaderWidgetState extends State // 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; diff --git a/lib/writer_widget.dart b/lib/writer_widget.dart index b2a1494..522e22f 100644 --- a/lib/writer_widget.dart +++ b/lib/writer_widget.dart @@ -170,7 +170,7 @@ class _WriterWidgetState extends State onPressed: createBarcode, child: const Text('Create'), ), - const SizedBox(height: 20), + const SizedBox(height: 10), ], ), ), @@ -202,7 +202,6 @@ class _WriterWidgetState extends State error = result.errorMessage; } if (error != null) { - debugPrint(error); widget.onError?.call(error); } }