Browse Source

allow to set custom localization messages

pull/30/head
Khoren Markosyan 2 years ago
parent
commit
15fd3ec85d
  1. 3
      example/lib/main.dart
  2. 1
      lib/flutter_zxing.dart
  3. 33
      lib/src/ui/writer_widget.dart
  4. 29
      lib/src/utils/messages.dart

3
example/lib/main.dart

@ -59,6 +59,9 @@ class _DemoPageState extends State<DemoPage> {
ListView( ListView(
children: [ children: [
WriterWidget( WriterWidget(
messages: const Messages(
createButton: 'Create Code',
),
onSuccess: (result, bytes) { onSuccess: (result, bytes) {
setState(() { setState(() {
createdCodeBytes = bytes; createdCodeBytes = bytes;

1
lib/flutter_zxing.dart

@ -5,3 +5,4 @@ export 'src/ui/scanner_overlay.dart';
export 'src/ui/writer_widget.dart'; export 'src/ui/writer_widget.dart';
export 'src/utils/extentions.dart'; export 'src/utils/extentions.dart';
export 'src/utils/image_converter.dart'; export 'src/utils/image_converter.dart';
export 'src/utils/messages.dart';

33
lib/src/ui/writer_widget.dart

@ -14,6 +14,7 @@ class WriterWidget extends StatefulWidget {
this.height = 120, this.height = 120,
this.margin = 0, this.margin = 0,
this.eccLevel = 0, this.eccLevel = 0,
this.messages = const Messages(),
this.onSuccess, this.onSuccess,
this.onError, this.onError,
}); });
@ -24,6 +25,7 @@ class WriterWidget extends StatefulWidget {
final int height; final int height;
final int margin; final int margin;
final int eccLevel; final int eccLevel;
final Messages messages;
final Function(EncodeResult result, Uint8List? bytes)? onSuccess; final Function(EncodeResult result, Uint8List? bytes)? onSuccess;
final Function(String error)? onError; final Function(String error)? onError;
@ -69,6 +71,7 @@ class _WriterWidgetState extends State<WriterWidget>
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final Messages m = widget.messages;
return SingleChildScrollView( return SingleChildScrollView(
child: Form( child: Form(
key: _formKey, key: _formKey,
@ -91,13 +94,13 @@ class _WriterWidgetState extends State<WriterWidget>
decoration: InputDecoration( decoration: InputDecoration(
border: const OutlineInputBorder(), border: const OutlineInputBorder(),
filled: true, filled: true,
labelText: 'Enter barcode text here', labelText: m.textLabel,
counterText: counterText:
'${_textController.value.text.length} / $_maxTextLength', '${_textController.value.text.length} / $_maxTextLength',
), ),
validator: (String? value) { validator: (String? value) {
if (value?.isEmpty ?? false) { if (value?.isEmpty ?? false) {
return 'Please enter some text'; return m.invalidText;
} }
return null; return null;
}, },
@ -125,13 +128,13 @@ class _WriterWidgetState extends State<WriterWidget>
child: TextFormField( child: TextFormField(
controller: _widthController, controller: _widthController,
keyboardType: TextInputType.number, keyboardType: TextInputType.number,
decoration: const InputDecoration( decoration: InputDecoration(
labelText: 'Width', labelText: m.widthLabel,
), ),
validator: (String? value) { validator: (String? value) {
final int? width = int.tryParse(value ?? ''); final int? width = int.tryParse(value ?? '');
if (width == null) { if (width == null) {
return 'Invalid number'; return m.invalidWidth;
} }
return null; return null;
}, },
@ -142,13 +145,13 @@ class _WriterWidgetState extends State<WriterWidget>
child: TextFormField( child: TextFormField(
controller: _heightController, controller: _heightController,
keyboardType: TextInputType.number, keyboardType: TextInputType.number,
decoration: const InputDecoration( decoration: InputDecoration(
labelText: 'Height', labelText: m.heightLabel,
), ),
validator: (String? value) { validator: (String? value) {
final int? width = int.tryParse(value ?? ''); final int? width = int.tryParse(value ?? '');
if (width == null) { if (width == null) {
return 'Invalid number'; return m.invalidHeight;
} }
return null; return null;
}, },
@ -163,13 +166,13 @@ class _WriterWidgetState extends State<WriterWidget>
child: TextFormField( child: TextFormField(
controller: _marginController, controller: _marginController,
keyboardType: TextInputType.number, keyboardType: TextInputType.number,
decoration: const InputDecoration( decoration: InputDecoration(
labelText: 'Margin', labelText: m.marginLabel,
), ),
validator: (String? value) { validator: (String? value) {
final int? width = int.tryParse(value ?? ''); final int? width = int.tryParse(value ?? '');
if (width == null) { if (width == null) {
return 'Invalid number'; return m.invalidMargin;
} }
return null; return null;
}, },
@ -180,13 +183,13 @@ class _WriterWidgetState extends State<WriterWidget>
child: TextFormField( child: TextFormField(
controller: _eccController, controller: _eccController,
keyboardType: TextInputType.number, keyboardType: TextInputType.number,
decoration: const InputDecoration( decoration: InputDecoration(
labelText: 'ECC Level', labelText: m.eccLevelLabel,
), ),
validator: (String? value) { validator: (String? value) {
final int? width = int.tryParse(value ?? ''); final int? width = int.tryParse(value ?? '');
if (width == null) { if (width == null) {
return 'Invalid number'; return m.invalidEccLevel;
} }
return null; return null;
}, },
@ -198,7 +201,7 @@ class _WriterWidgetState extends State<WriterWidget>
// Write button // Write button
ElevatedButton( ElevatedButton(
onPressed: createBarcode, onPressed: createBarcode,
child: const Text('Create'), child: Text(m.createButton),
), ),
const SizedBox(height: 10), const SizedBox(height: 10),
], ],

29
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;
}
Loading…
Cancel
Save