From 15fd3ec85d78bc8dabc84cfc51b668344bd1793b Mon Sep 17 00:00:00 2001 From: Khoren Markosyan Date: Sun, 11 Sep 2022 01:03:22 +0400 Subject: [PATCH] allow to set custom localization messages --- example/lib/main.dart | 3 +++ lib/flutter_zxing.dart | 1 + lib/src/ui/writer_widget.dart | 33 ++++++++++++++++++--------------- lib/src/utils/messages.dart | 29 +++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 lib/src/utils/messages.dart diff --git a/example/lib/main.dart b/example/lib/main.dart index 6f6663a..5a01071 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -59,6 +59,9 @@ class _DemoPageState extends State { ListView( children: [ WriterWidget( + messages: const Messages( + createButton: 'Create Code', + ), onSuccess: (result, bytes) { setState(() { createdCodeBytes = bytes; diff --git a/lib/flutter_zxing.dart b/lib/flutter_zxing.dart index e987ccc..1ddc2d1 100644 --- a/lib/flutter_zxing.dart +++ b/lib/flutter_zxing.dart @@ -5,3 +5,4 @@ export 'src/ui/scanner_overlay.dart'; export 'src/ui/writer_widget.dart'; export 'src/utils/extentions.dart'; export 'src/utils/image_converter.dart'; +export 'src/utils/messages.dart'; diff --git a/lib/src/ui/writer_widget.dart b/lib/src/ui/writer_widget.dart index 73df9fe..9ef8c62 100644 --- a/lib/src/ui/writer_widget.dart +++ b/lib/src/ui/writer_widget.dart @@ -14,6 +14,7 @@ class WriterWidget extends StatefulWidget { this.height = 120, this.margin = 0, this.eccLevel = 0, + this.messages = const Messages(), this.onSuccess, this.onError, }); @@ -24,6 +25,7 @@ class WriterWidget extends StatefulWidget { final int height; final int margin; final int eccLevel; + final Messages messages; final Function(EncodeResult result, Uint8List? bytes)? onSuccess; final Function(String error)? onError; @@ -69,6 +71,7 @@ class _WriterWidgetState extends State @override Widget build(BuildContext context) { + final Messages m = widget.messages; return SingleChildScrollView( child: Form( key: _formKey, @@ -91,13 +94,13 @@ class _WriterWidgetState extends State decoration: InputDecoration( border: const OutlineInputBorder(), filled: true, - labelText: 'Enter barcode text here', + labelText: m.textLabel, counterText: '${_textController.value.text.length} / $_maxTextLength', ), validator: (String? value) { if (value?.isEmpty ?? false) { - return 'Please enter some text'; + return m.invalidText; } return null; }, @@ -125,13 +128,13 @@ class _WriterWidgetState extends State child: TextFormField( controller: _widthController, keyboardType: TextInputType.number, - decoration: const InputDecoration( - labelText: 'Width', + decoration: InputDecoration( + labelText: m.widthLabel, ), validator: (String? value) { final int? width = int.tryParse(value ?? ''); if (width == null) { - return 'Invalid number'; + return m.invalidWidth; } return null; }, @@ -142,13 +145,13 @@ class _WriterWidgetState extends State child: TextFormField( controller: _heightController, keyboardType: TextInputType.number, - decoration: const InputDecoration( - labelText: 'Height', + decoration: InputDecoration( + labelText: m.heightLabel, ), validator: (String? value) { final int? width = int.tryParse(value ?? ''); if (width == null) { - return 'Invalid number'; + return m.invalidHeight; } return null; }, @@ -163,13 +166,13 @@ class _WriterWidgetState extends State child: TextFormField( controller: _marginController, keyboardType: TextInputType.number, - decoration: const InputDecoration( - labelText: 'Margin', + decoration: InputDecoration( + labelText: m.marginLabel, ), validator: (String? value) { final int? width = int.tryParse(value ?? ''); if (width == null) { - return 'Invalid number'; + return m.invalidMargin; } return null; }, @@ -180,13 +183,13 @@ class _WriterWidgetState extends State child: TextFormField( controller: _eccController, keyboardType: TextInputType.number, - decoration: const InputDecoration( - labelText: 'ECC Level', + decoration: InputDecoration( + labelText: m.eccLevelLabel, ), validator: (String? value) { final int? width = int.tryParse(value ?? ''); if (width == null) { - return 'Invalid number'; + return m.invalidEccLevel; } return null; }, @@ -198,7 +201,7 @@ class _WriterWidgetState extends State // Write button ElevatedButton( onPressed: createBarcode, - child: const Text('Create'), + child: Text(m.createButton), ), const SizedBox(height: 10), ], diff --git a/lib/src/utils/messages.dart b/lib/src/utils/messages.dart new file mode 100644 index 0000000..a12b7c2 --- /dev/null +++ b/lib/src/utils/messages.dart @@ -0,0 +1,29 @@ +class Messages { + const Messages({ + this.createButton = 'Create', + this.textLabel = 'Enter barcode text here', + this.marginLabel = 'Margin', + this.eccLevelLabel = 'ECC Level', + this.widthLabel = 'Width', + this.heightLabel = 'Height', + this.invalidText = 'Please enter some text', + this.invalidWidth = 'Invalid width', + this.invalidHeight = 'Invalid height', + this.invalidMargin = 'Invalid margin', + this.invalidEccLevel = 'Invalid ECC level', + }); + + final String createButton; + + final String textLabel; + final String marginLabel; + final String eccLevelLabel; + final String widthLabel; + final String heightLabel; + + final String invalidText; + final String invalidWidth; + final String invalidHeight; + final String invalidMargin; + final String invalidEccLevel; +}