Browse Source

code improvements

pull/3/head
Khoren Markosyan 3 years ago
parent
commit
ca3425d45b
  1. 6
      example/ios/Podfile.lock
  2. 2
      example/ios/Runner/Info.plist
  3. 12
      example/lib/pages/barcodes_page.dart
  4. 7
      example/lib/pages/creator_page.dart
  5. 15
      example/lib/pages/help_page.dart
  6. 13
      example/lib/pages/history_page.dart
  7. 4
      example/lib/pages/home_page.dart
  8. 42
      example/lib/pages/scanner_page.dart
  9. 17
      example/lib/utils/extensions.dart
  10. 56
      example/pubspec.lock
  11. 2
      example/pubspec.yaml
  12. 8
      ios/Classes/src/native_zxing.cpp
  13. 6
      ios/Classes/src/native_zxing.h
  14. 2
      lib/flutter_zxing.dart
  15. 4
      lib/generated_bindings.dart
  16. 154
      lib/zxing_writer_widget.dart

6
example/ios/Podfile.lock

@ -12,6 +12,8 @@ PODS:
- fluttertoast (0.0.2):
- Flutter
- Toast
- image_picker_ios (0.0.1):
- Flutter
- nb_utils (0.0.1):
- Flutter
- path_provider_ios (0.0.1):
@ -32,6 +34,7 @@ DEPENDENCIES:
- flutter_beep (from `.symlinks/plugins/flutter_beep/ios`)
- flutter_zxing (from `.symlinks/plugins/flutter_zxing/ios`)
- fluttertoast (from `.symlinks/plugins/fluttertoast/ios`)
- image_picker_ios (from `.symlinks/plugins/image_picker_ios/ios`)
- nb_utils (from `.symlinks/plugins/nb_utils/ios`)
- path_provider_ios (from `.symlinks/plugins/path_provider_ios/ios`)
- share_plus (from `.symlinks/plugins/share_plus/ios`)
@ -56,6 +59,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/flutter_zxing/ios"
fluttertoast:
:path: ".symlinks/plugins/fluttertoast/ios"
image_picker_ios:
:path: ".symlinks/plugins/image_picker_ios/ios"
nb_utils:
:path: ".symlinks/plugins/nb_utils/ios"
path_provider_ios:
@ -74,6 +79,7 @@ SPEC CHECKSUMS:
flutter_beep: 54fb393b22dfa0f0e4573c81b1c74dd71c4e5af8
flutter_zxing: 19a866d17c8a87ee1026d68521c69d2f008635f6
fluttertoast: 16fbe6039d06a763f3533670197d01fc73459037
image_picker_ios: b786a5dcf033a8336a657191401bfdf12017dabb
nb_utils: ada4338858d8827ec92fdab2a545206b4ba4cfb1
path_provider_ios: 14f3d2fd28c4fdb42f44e0f751d12861c43cee02
ReachabilitySwift: 985039c6f7b23a1da463388634119492ff86c825

2
example/ios/Runner/Info.plist

@ -43,6 +43,8 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>NSPhotoLibraryUsageDescription</key>
<string>$(APP_DISPLAY_NAME) needs photo library access to scan barcodes</string>
<key>NSCameraUsageDescription</key>
<string>$(APP_DISPLAY_NAME) needs camera access to scan barcodes</string>
</dict>

12
example/lib/pages/barcodes_page.dart

@ -5,6 +5,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_zxing_example/models/models.dart';
import 'package:flutter_zxing_example/utils/db_service.dart';
import 'package:flutter_zxing_example/utils/router.dart';
import 'package:flutter_zxing_example/widgets/common_widgets.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:hive_flutter/hive_flutter.dart';
@ -49,7 +50,8 @@ class _BarcodesPageState extends State<BarcodesPage> {
itemCount: results.length,
itemBuilder: (context, index) {
final result = results[index];
return ListTile(
return ContainerX(
child: ListTile(
leading: Image.memory(result.data ?? Uint8List(0)),
title: Text(result.text ?? ''),
subtitle: Text(result.formatName),
@ -57,8 +59,8 @@ class _BarcodesPageState extends State<BarcodesPage> {
mainAxisSize: MainAxisSize.min,
children: [
// Copy button
TextButton(
child: const Text('Copy'),
IconButton(
icon: const Icon(FontAwesomeIcons.copy),
onPressed: () {
Clipboard.setData(
ClipboardData(text: result.text));
@ -66,7 +68,8 @@ class _BarcodesPageState extends State<BarcodesPage> {
),
// Remove button
IconButton(
icon: const Icon(Icons.delete, color: Colors.red),
icon: const Icon(FontAwesomeIcons.trash,
color: Colors.red),
onPressed: () {
DbService.instance.deleteEncode(result);
setState(() {});
@ -74,6 +77,7 @@ class _BarcodesPageState extends State<BarcodesPage> {
),
],
),
),
);
},
);

7
example/lib/pages/creator_page.dart

@ -3,8 +3,10 @@ import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_zxing/flutter_zxing.dart';
import 'package:flutter_zxing_example/configs/constants.dart';
import 'package:flutter_zxing_example/models/encode.dart';
import 'package:flutter_zxing_example/utils/db_service.dart';
import 'package:flutter_zxing_example/widgets/common_widgets.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
@ -46,6 +48,7 @@ class _CreatorPageState extends State<CreatorPage> {
title: const Text('Creator'),
),
body: SingleChildScrollView(
child: ContainerX(
child: Column(
children: [
ZxingWriterWidget(
@ -76,6 +79,7 @@ class _CreatorPageState extends State<CreatorPage> {
],
),
),
),
);
}
@ -84,7 +88,7 @@ class _CreatorPageState extends State<CreatorPage> {
children: [
// Barcode image
Image.memory(encode?.data ?? Uint8List(0)),
const SizedBox(height: 20),
const SizedBox(height: spaceLarge),
// Share button
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
@ -112,6 +116,7 @@ class _CreatorPageState extends State<CreatorPage> {
),
],
),
const SizedBox(height: spaceLarge),
],
);
}

15
example/lib/pages/help_page.dart

@ -0,0 +1,15 @@
import 'package:flutter/material.dart';
class HelpPage extends StatelessWidget {
const HelpPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Help'),
),
body: Container(),
);
}
}

13
example/lib/pages/history_page.dart

@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_zxing_example/models/code.dart';
import 'package:flutter_zxing_example/utils/db_service.dart';
import 'package:flutter_zxing_example/widgets/common_widgets.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:hive_flutter/hive_flutter.dart';
class HistoryPage extends StatefulWidget {
@ -39,15 +41,16 @@ class _HistoryPageState extends State<HistoryPage> {
itemCount: results.length,
itemBuilder: (context, index) {
final result = results[index];
return ListTile(
return ContainerX(
child: ListTile(
title: Text(result.text ?? ''),
subtitle: Text(result.formatName),
trailing: ButtonBar(
mainAxisSize: MainAxisSize.min,
children: [
// Copy button
TextButton(
child: const Text('Copy'),
IconButton(
icon: const Icon(FontAwesomeIcons.copy),
onPressed: () {
Clipboard.setData(
ClipboardData(text: result.text));
@ -55,7 +58,8 @@ class _HistoryPageState extends State<HistoryPage> {
),
// Remove button
IconButton(
icon: const Icon(Icons.delete, color: Colors.red),
icon: const Icon(FontAwesomeIcons.trash,
color: Colors.red),
onPressed: () {
DbService.instance.deleteCode(result);
setState(() {});
@ -63,6 +67,7 @@ class _HistoryPageState extends State<HistoryPage> {
),
],
),
),
);
},
);

4
example/lib/pages/home_page.dart

@ -1,7 +1,7 @@
import 'package:convex_bottom_bar/convex_bottom_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_zxing_example/pages/barcodes_page.dart';
import 'package:flutter_zxing_example/pages/creator_page.dart';
import 'package:flutter_zxing_example/pages/help_page.dart';
import 'package:flutter_zxing_example/pages/history_page.dart';
import 'package:flutter_zxing_example/pages/scanner_page.dart';
import 'package:flutter_zxing_example/pages/settings_page.dart';
@ -20,7 +20,7 @@ class _HomePageState extends State<HomePage> {
final barcodesPage = const BarcodesPage();
final historyPage = const HistoryPage();
final scannerPage = const ScannerPage();
final helpPage = Container();
final helpPage = const HelpPage();
final settingsPage = const SettingsPage();
dynamic pages() => [

42
example/lib/pages/scanner_page.dart

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_zxing/flutter_zxing.dart';
import 'package:flutter_zxing_example/models/models.dart';
import 'package:flutter_zxing_example/utils/db_service.dart';
import 'package:flutter_zxing_example/utils/extensions.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:image_picker/image_picker.dart';
import 'package:image/image.dart' as imglib;
@ -33,10 +34,24 @@ class _ScannerPageState extends State<ScannerPage> {
),
floatingActionButton: FloatingActionButton(
child: const Icon(FontAwesomeIcons.image),
onPressed: () async {
final XFile? file =
await _picker.pickImage(source: ImageSource.gallery);
onPressed: pickImage,
),
);
}
pickImage() async {
try {
final XFile? file = await _picker.pickImage(source: ImageSource.gallery);
if (file != null) {
readCodeFromImage(file);
}
} catch (e) {
debugPrint(e.toString());
context.showToast(e.toString());
}
}
readCodeFromImage(XFile file) async {
final Uint8List bytes = await file.readAsBytes();
imglib.Image? image = imglib.decodeImage(bytes);
if (image != null) {
@ -50,30 +65,15 @@ class _ScannerPageState extends State<ScannerPage> {
);
if (result.isValidBool) {
addCode(result);
} else {
context.showToast('No code found');
}
}
}
},
),
);
}
void addCode(CodeResult result) {
Code code = Code.fromCodeResult(result);
DbService.instance.addCode(code);
// show snackbar
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Padding(
padding: const EdgeInsets.only(bottom: 30.0),
child: Text(
code.text ?? '',
textAlign: TextAlign.center,
),
),
),
);
context.showToast('Code added:\n${code.text ?? ''}');
}
}

17
example/lib/utils/extensions.dart

@ -1,4 +1,4 @@
import 'dart:ui';
import 'package:flutter/material.dart';
extension LocaleParsing on String {
Locale parseLocale() {
@ -46,3 +46,18 @@ extension LocaleParsing on String {
return split('_').first;
}
}
// context extension to show a toast message
extension ContextExtension on BuildContext {
void showToast(String message) {
ScaffoldMessenger.of(this).hideCurrentSnackBar();
ScaffoldMessenger.of(this).showSnackBar(
SnackBar(
content: Padding(
padding: const EdgeInsets.only(bottom: 30.0),
child: Text(message, textAlign: TextAlign.center),
),
),
);
}
}

56
example/pubspec.lock

@ -98,14 +98,14 @@ packages:
name: built_value
url: "https://pub.dartlang.org"
source: hosted
version: "8.2.3"
version: "8.3.0"
camera:
dependency: transitive
description:
name: camera
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.4+21"
version: "0.9.4+22"
camera_platform_interface:
dependency: transitive
description:
@ -119,7 +119,7 @@ packages:
name: camera_web
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.1+3"
version: "0.2.1+5"
characters:
dependency: transitive
description:
@ -325,7 +325,7 @@ packages:
name: flutter_plugin_android_lifecycle
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
version: "2.0.6"
flutter_test:
dependency: "direct dev"
description: flutter
@ -433,28 +433,28 @@ packages:
name: image_picker
url: "https://pub.dartlang.org"
source: hosted
version: "0.8.5"
version: "0.8.5+1"
image_picker_android:
dependency: transitive
description:
name: image_picker_android
url: "https://pub.dartlang.org"
source: hosted
version: "0.8.4+11"
version: "0.8.4+12"
image_picker_for_web:
dependency: transitive
description:
name: image_picker_for_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.6"
version: "2.1.7"
image_picker_ios:
dependency: transitive
description:
name: image_picker_ios
url: "https://pub.dartlang.org"
source: hosted
version: "0.8.5"
version: "0.8.5+1"
image_picker_platform_interface:
dependency: transitive
description:
@ -580,35 +580,35 @@ packages:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.9"
version: "2.0.10"
path_provider_android:
dependency: transitive
description:
name: path_provider_android
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.13"
version: "2.0.14"
path_provider_ios:
dependency: transitive
description:
name: path_provider_ios
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
version: "2.0.9"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.5"
version: "2.1.6"
path_provider_macos:
dependency: transitive
description:
name: path_provider_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
version: "2.0.6"
path_provider_platform_interface:
dependency: transitive
description:
@ -622,7 +622,7 @@ packages:
name: path_provider_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
version: "2.0.6"
petitparser:
dependency: transitive
description:
@ -727,35 +727,35 @@ packages:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.13"
version: "2.0.14"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
version: "2.0.12"
shared_preferences_ios:
dependency: transitive
description:
name: shared_preferences_ios
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
shared_preferences_macos:
dependency: transitive
description:
name: shared_preferences_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
version: "2.0.4"
shared_preferences_platform_interface:
dependency: transitive
description:
@ -769,14 +769,14 @@ packages:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
version: "2.0.4"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
shelf:
dependency: transitive
description:
@ -879,35 +879,35 @@ packages:
name: url_launcher
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.0"
version: "6.1.1"
url_launcher_android:
dependency: transitive
description:
name: url_launcher_android
url: "https://pub.dartlang.org"
source: hosted
version: "6.0.16"
version: "6.0.17"
url_launcher_ios:
dependency: transitive
description:
name: url_launcher_ios
url: "https://pub.dartlang.org"
source: hosted
version: "6.0.15"
version: "6.0.16"
url_launcher_linux:
dependency: transitive
description:
name: url_launcher_linux
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
version: "3.0.1"
url_launcher_macos:
dependency: transitive
description:
name: url_launcher_macos
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
version: "3.0.1"
url_launcher_platform_interface:
dependency: transitive
description:
@ -921,14 +921,14 @@ packages:
name: url_launcher_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.9"
version: "2.0.10"
url_launcher_windows:
dependency: transitive
description:
name: url_launcher_windows
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
version: "3.0.1"
vector_math:
dependency: transitive
description:

2
example/pubspec.yaml

@ -22,7 +22,7 @@ dependencies:
hive_flutter: ^1.1.0
image_picker: ^0.8.5
nb_utils: ^4.5.0
path_provider: ^2.0.9
path_provider: ^2.0.10
share_plus: ^4.0.4
flutter_intl:

8
ios/Classes/src/native_zxing.cpp

@ -10,7 +10,7 @@ using namespace ZXing;
extern "C"
{
FUNCTION_ATTRIBUTE
char *version()
char const *version()
{
return "1.3.0";
}
@ -21,7 +21,7 @@ extern "C"
long long start = get_now();
long length = width * height;
uint8_t *data = new uint8_t[length];
auto *data = new uint8_t[length];
memcpy(data, bytes, length);
BarcodeFormats formats = BarcodeFormat(format); // BarcodeFormat::Any;
@ -57,7 +57,7 @@ extern "C"
long long start = get_now();
long length = width * height;
uint8_t *data = new uint8_t[length];
auto *data = new uint8_t[length];
memcpy(data, bytes, length);
BarcodeFormats formats = BarcodeFormat(format); // BarcodeFormat::Any;
@ -69,7 +69,7 @@ extern "C"
}
Results results = ReadBarcodes(image, hints);
struct CodeResult *codes = new struct CodeResult [results.size()];
auto *codes = new struct CodeResult [results.size()];
int i = 0;
for (auto &result : results)
{

6
ios/Classes/src/native_zxing.h

@ -46,11 +46,11 @@ extern "C"
};
/**
* Returns the version of the zxing library.
* Returns the version of the zxing-cpp library.
*
* @return The version of the zxing library.
* @return The version of the zxing-cpp library.
*/
char *version();
char const *version();
/**
* @brief Reads barcode from image.

2
lib/flutter_zxing.dart

@ -23,7 +23,7 @@ class FlutterZxing {
static final bindings = GeneratedBindings(dylib);
static bool logEnabled = true;
static bool logEnabled = false;
static String version() {
return bindings.version().cast<Utf8>().toDartString();

4
lib/generated_bindings.dart

@ -19,9 +19,9 @@ class GeneratedBindings {
lookup)
: _lookup = lookup;
/// Returns the version of the zxing library.
/// Returns the version of the zxing-cpp library.
///
/// @return The version of the zxing library.
/// @return The version of the zxing-cpp library.
ffi.Pointer<ffi.Int8> version() {
return _version();
}

154
lib/zxing_writer_widget.dart

@ -23,6 +23,13 @@ class _ZxingWriterWidgetState extends State<ZxingWriterWidget>
with TickerProviderStateMixin {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final TextEditingController _textController = TextEditingController();
final TextEditingController _widthController =
TextEditingController(text: "300");
final TextEditingController _heightController =
TextEditingController(text: "300");
final TextEditingController _marginController =
TextEditingController(text: "10");
final TextEditingController _eccController = TextEditingController(text: "0");
bool isAndroid() => Theme.of(context).platform == TargetPlatform.android;
@ -41,6 +48,30 @@ class _ZxingWriterWidgetState extends State<ZxingWriterWidget>
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const SizedBox(height: 20),
// Input multiline text
TextFormField(
controller: _textController,
keyboardType: TextInputType.multiline,
maxLines: null,
maxLength: _maxTextLength,
onChanged: (value) {
setState(() {});
},
decoration: InputDecoration(
border: const OutlineInputBorder(),
filled: true,
labelText: 'Enter barcode text here',
counterText:
'${_textController.value.text.length} / $_maxTextLength',
),
validator: (value) {
if (value?.isEmpty == true) {
return 'Please enter some text';
}
return null;
},
),
const SizedBox(height: 20),
// Format DropDown button
DropdownButtonFormField<int>(
@ -58,41 +89,111 @@ class _ZxingWriterWidgetState extends State<ZxingWriterWidget>
},
),
const SizedBox(height: 20),
// Input multiline text
TextFormField(
controller: _textController,
keyboardType: TextInputType.multiline,
maxLines: null,
maxLength: _maxTextLength,
onChanged: (value) {
setState(() {});
Row(
children: [
Flexible(
child: TextFormField(
controller: _widthController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Width',
),
validator: (value) {
final width = int.tryParse(value ?? '');
if (width == null) {
return 'Invalid number';
}
return null;
},
decoration: InputDecoration(
border: const OutlineInputBorder(),
filled: true,
hintText: 'Enter text to create a barcode',
counterText:
'${_textController.value.text.length} / $_maxTextLength',
),
),
const SizedBox(width: 8),
Flexible(
child: TextFormField(
controller: _heightController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Height',
),
validator: (value) {
final width = int.tryParse(value ?? '');
if (width == null) {
return 'Invalid number';
}
return null;
},
),
),
],
),
const SizedBox(height: 20),
Row(
children: [
Flexible(
child: TextFormField(
controller: _marginController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Margin',
),
validator: (value) {
final width = int.tryParse(value ?? '');
if (width == null) {
return 'Invalid number';
}
return null;
},
),
),
const SizedBox(width: 8),
Flexible(
child: TextFormField(
controller: _eccController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'ECC Level',
),
validator: (value) {
final width = int.tryParse(value ?? '');
if (width == null) {
return 'Invalid number';
}
return null;
},
),
),
],
),
const SizedBox(height: 20),
// Write button
ElevatedButton(
onPressed: () {
onPressed: createBarcode,
child: const Text('Create'),
),
const SizedBox(height: 20),
],
),
),
),
);
}
void createBarcode() {
if (_formKey.currentState?.validate() ?? false) {
_formKey.currentState?.save();
FocusScope.of(context).unfocus();
final text = _textController.value.text;
const width = 300;
const height = 300;
final width = int.parse(_widthController.value.text);
final height = int.parse(_heightController.value.text);
final margin = int.parse(_marginController.value.text);
final ecc = int.parse(_eccController.value.text);
var result = FlutterZxing.encodeBarcode(
text, width, height, _codeFormat, 5, 0);
text, width, height, _codeFormat, margin, ecc);
String? error;
if (result.isValidBool) {
try {
final img =
imglib.Image.fromBytes(width, height, result.bytes);
final encodedBytes =
Uint8List.fromList(imglib.encodeJpg(img));
final img = imglib.Image.fromBytes(width, height, result.bytes);
final encodedBytes = Uint8List.fromList(imglib.encodeJpg(img));
widget.onSuccess?.call(result, encodedBytes);
} on Exception catch (e) {
error = e.toString();
@ -105,14 +206,5 @@ class _ZxingWriterWidgetState extends State<ZxingWriterWidget>
widget.onError?.call(error);
}
}
},
child: const Text('Create'),
),
const SizedBox(height: 20),
],
),
),
),
);
}
}

Loading…
Cancel
Save