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(
children: [
WriterWidget(
messages: const Messages(
createButton: 'Create Code',
),
onSuccess: (result, bytes) {
setState(() {
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/utils/extentions.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.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<WriterWidget>
@override
Widget build(BuildContext context) {
final Messages m = widget.messages;
return SingleChildScrollView(
child: Form(
key: _formKey,
@ -91,13 +94,13 @@ class _WriterWidgetState extends State<WriterWidget>
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<WriterWidget>
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<WriterWidget>
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<WriterWidget>
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<WriterWidget>
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<WriterWidget>
// Write button
ElevatedButton(
onPressed: createBarcode,
child: const Text('Create'),
child: Text(m.createButton),
),
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