diff --git a/CHANGELOG.md b/CHANGELOG.md index 87b007f..c8198a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ * Fixed the bug with backspace key. -## [0.2.0] - 19/03/2020. +## [0.2.1] - 19/03/2020. * Adding multi-language support. * Customizable layout. \ No newline at end of file diff --git a/README.md b/README.md index 79c9b2c..58a2db9 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,10 @@ Function onKeyPress; double height; ``` ```dart +/// Virtual keyboard height. Default is full screen width + double width; +``` +```dart // Color for key texts and icons. Color textColor; ``` @@ -55,6 +59,15 @@ double fontSize;; // Only Caps letters enabled. bool alwaysCaps;; ``` +```dart +/// the custom layout for multi or single language +VirtualKeyboardLayoutKeys customLayoutKeys; +``` +```dart +/// used for multi-languages with default layouts, the default is English only +/// will be ignored if customLayoutKeys is not null +List defaultLayouts; +``` ### VirtualKeyboardType enum of Available Virtual Keyboard Types. @@ -114,10 +127,14 @@ Container( child: VirtualKeyboard( // Default height is 300 height: 350, + // Default height is will screen width + width: 600, // Default is black textColor: Colors.white, // Default 14 fontSize: 20, + // the layouts supported + defaultLayouts = [VirtualKeyboardDefaultLayouts.English], // [A-Z, 0-9] type: VirtualKeyboardType.Alphanumeric, // Callback for key press event diff --git a/example/lib/main.dart b/example/lib/main.dart index 74aa42f..dce9c9b 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -33,11 +33,14 @@ class _MyHomePageState extends State { bool shiftEnabled = false; // is true will show the numeric keyboard. - bool isNumericMode = true; + bool isNumericMode = false; + + TextEditingController _controllerText; @override void initState() { _customLayoutKeys = CustomLayoutKeys(); + _controllerText = TextEditingController(); super.initState(); } @@ -54,6 +57,10 @@ class _MyHomePageState extends State { text, style: Theme.of(context).textTheme.display1, ), + Text( + _controllerText.text, + style: TextStyle(color: Colors.red), + ), SwitchListTile( title: Text( 'Keyboard Type = ' + @@ -75,9 +82,11 @@ class _MyHomePageState extends State { color: Colors.deepPurple, child: VirtualKeyboard( height: 300, - width: 700, + //width: 500, textColor: Colors.white, - layoutKeys: _customLayoutKeys, + textController: _controllerText, + //customLayoutKeys: _customLayoutKeys, + defaultLayouts: [VirtualKeyboardDefaultLayouts.Arabic,VirtualKeyboardDefaultLayouts.English], type: isNumericMode ? VirtualKeyboardType.Numeric : VirtualKeyboardType.Alphanumeric, diff --git a/lib/src/keyboard.dart b/lib/src/keyboard.dart index 9fce61a..8464b65 100644 --- a/lib/src/keyboard.dart +++ b/lib/src/keyboard.dart @@ -17,7 +17,7 @@ class VirtualKeyboard extends StatefulWidget { /// Virtual keyboard height. Default is 300 final double height; - /// Virtual keyboard height. Default is screen width + /// Virtual keyboard height. Default is full screen width final double width; /// Color for key texts and icons. @@ -26,7 +26,11 @@ class VirtualKeyboard extends StatefulWidget { /// Font size for keyboard keys. final double fontSize; - final VirtualKeyboardLayoutKeys layoutKeys; + /// the custom layout for multi or single language + final VirtualKeyboardLayoutKeys customLayoutKeys; + + /// the text controller go get the output and send the default input + final TextEditingController textController; /// The builder function will be called for each Key object. final Widget Function(BuildContext context, VirtualKeyboardKey key) builder; @@ -34,13 +38,19 @@ class VirtualKeyboard extends StatefulWidget { /// Set to true if you want only to show Caps letters. final bool alwaysCaps; + /// used for multi-languages with default layouts, the default is English only + /// will be ignored if customLayoutKeys is not null + final List defaultLayouts; + VirtualKeyboard( {Key key, @required this.type, - @required this.onKeyPress, + this.onKeyPress, this.builder, this.width, - this.layoutKeys, + this.defaultLayouts, + this.customLayoutKeys, + this.textController, this.height = _virtualKeyboardDefaultHeight, this.textColor = Colors.black, this.fontSize = 14, @@ -57,6 +67,7 @@ class VirtualKeyboard extends StatefulWidget { class _VirtualKeyboardState extends State { VirtualKeyboardType type; Function onKeyPress; + TextEditingController textController; // The builder function will be called for each Key object. Widget Function(BuildContext context, VirtualKeyboardKey key) builder; double height; @@ -64,13 +75,46 @@ class _VirtualKeyboardState extends State { Color textColor; double fontSize; bool alwaysCaps; - VirtualKeyboardLayoutKeys layoutKeys; + VirtualKeyboardLayoutKeys customLayoutKeys; // Text Style for keys. TextStyle textStyle; // True if shift is enabled. bool isShiftEnabled = false; + void _onKeyPress(VirtualKeyboardKey key){ + + if (key.keyType == VirtualKeyboardKeyType.String) { + textController.text += (isShiftEnabled ? key.capsText : key.text); + } else if (key.keyType == VirtualKeyboardKeyType.Action) { + switch (key.action) { + case VirtualKeyboardKeyAction.Backspace: + if (textController.text.length == 0) return; + textController.text = textController.text.substring(0, textController.text.length - 1); + break; + case VirtualKeyboardKeyAction.Return: + textController.text += '\n'; + break; + case VirtualKeyboardKeyAction.Space: + textController.text += key.text; + break; + case VirtualKeyboardKeyAction.Shift: + break; + default: + } + } + + if(onKeyPress != null) + onKeyPress(key); + + } + + @override dispose(){ + if(widget.textController == null) // dispose if created locally only + textController?.dispose(); + super.dispose(); + } + @override void didUpdateWidget(Widget oldWidget) { super.didUpdateWidget(oldWidget); @@ -82,7 +126,8 @@ class _VirtualKeyboardState extends State { textColor = widget.textColor; fontSize = widget.fontSize; alwaysCaps = widget.alwaysCaps; - layoutKeys = widget.layoutKeys ?? VirtualKeyboardLayoutKeys(); + textController = widget.textController ?? textController; + customLayoutKeys = widget.customLayoutKeys ?? customLayoutKeys ; // Init the Text Style for keys. textStyle = TextStyle( fontSize: fontSize, @@ -94,10 +139,11 @@ class _VirtualKeyboardState extends State { @override void initState() { super.initState(); - + + textController = widget.textController ?? TextEditingController(); width = widget.width; type = widget.type; - layoutKeys = widget.layoutKeys ?? VirtualKeyboardLayoutKeys(); + customLayoutKeys = widget.customLayoutKeys ?? VirtualKeyboardDefaultLayoutKeys(widget.defaultLayouts ?? [VirtualKeyboardDefaultLayouts.English]); onKeyPress = widget.onKeyPress; height = widget.height; textColor = widget.textColor; @@ -146,7 +192,7 @@ class _VirtualKeyboardState extends State { List> keyboardRows = type == VirtualKeyboardType.Numeric ? _getKeyboardRowsNumeric() - : _getKeyboardRows(layoutKeys); + : _getKeyboardRows(customLayoutKeys); // Generate keyboard row. List rows = List.generate(keyboardRows.length, (int rowNum) { @@ -207,10 +253,10 @@ class _VirtualKeyboardState extends State { return Expanded( child: InkWell( onTap: () { - onKeyPress(key); + _onKeyPress(key); }, child: Container( - height: height / layoutKeys.activeLayout.length, + height: height / customLayoutKeys.activeLayout.length, child: Center( child: Text( alwaysCaps @@ -238,7 +284,7 @@ class _VirtualKeyboardState extends State { Duration(milliseconds: _virtualKeyboardBackspaceEventPerioud), (timer) { if (longPress) { - onKeyPress(key); + _onKeyPress(key); } else { // Cancel timer. timer.cancel(); @@ -274,7 +320,7 @@ class _VirtualKeyboardState extends State { actionKey = GestureDetector( onTap: () { setState(() { - layoutKeys.switchLanguage(); + customLayoutKeys.switchLanguage(); }); }, child: Container( @@ -299,11 +345,11 @@ class _VirtualKeyboardState extends State { } } - onKeyPress(key); + _onKeyPress(key); }, child: Container( alignment: Alignment.center, - height: height / layoutKeys.activeLayout.length, + height: height / customLayoutKeys.activeLayout.length, child: actionKey, ), ); diff --git a/lib/src/layout_keys.dart b/lib/src/layout_keys.dart index af44a60..790d182 100644 --- a/lib/src/layout_keys.dart +++ b/lib/src/layout_keys.dart @@ -1,15 +1,16 @@ part of virtual_keyboard_multi_language; //import '../virtual_keyboard_multi_language.dart'; -class VirtualKeyboardLayoutKeys{ - - int getLanguagesCount() => 1; +abstract class VirtualKeyboardLayoutKeys{ int activeIndex =0; - List getLanguage(int index){ - return _defaultEnglishLayout; - } + List get defaultEnglishLayout => _defaultEnglishLayout; + List get defaultArabicLayout => _defaultArabicLayout; + + List get activeLayout => getLanguage(activeIndex); + int getLanguagesCount(); + List getLanguage(int index); void switchLanguage(){ if((activeIndex+1) == getLanguagesCount()) @@ -17,9 +18,28 @@ class VirtualKeyboardLayoutKeys{ else activeIndex++; } - List get defaultEnglishLayout => _defaultEnglishLayout; +} + +class VirtualKeyboardDefaultLayoutKeys extends VirtualKeyboardLayoutKeys{ + + List defaultLayouts; + VirtualKeyboardDefaultLayoutKeys(this.defaultLayouts); + + int getLanguagesCount() => defaultLayouts.length; + + List getLanguage(int index){ + + switch(defaultLayouts[index]){ + case VirtualKeyboardDefaultLayouts.English: + return _defaultEnglishLayout; + case VirtualKeyboardDefaultLayouts.Arabic: + return _defaultArabicLayout; + default: + } + return _defaultEnglishLayout; + } + - List get activeLayout => getLanguage(activeIndex); } @@ -91,3 +111,73 @@ const List _defaultEnglishLayout = [ '_', ] ]; + + +const List _defaultArabicLayout = [ + // Row 1 + const [ + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + '0', + ], + // Row 2 + const [ + 'ض', + 'ص', + 'ث', + 'ق', + 'ف', + 'غ', + 'ع', + 'ه', + 'خ', + 'ح', + 'د', + VirtualKeyboardKeyAction.Backspace + ], + // Row 3 + const [ + 'ش', + 'س', + 'ي', + 'ب', + 'ل', + 'ا', + 'ت', + 'ن', + 'م', + 'ك', + 'ط', + VirtualKeyboardKeyAction.Return + ], + // Row 4 + const [ + 'ذ', + 'ئ', + 'ء', + 'ؤ', + 'ر', + 'لا', + 'ى', + 'ة', + 'و', + '.', + 'ظ', + VirtualKeyboardKeyAction.Shift + ], + // Row 5 + const [ + VirtualKeyboardKeyAction.SwithLanguage, + '@', + VirtualKeyboardKeyAction.Space, + '-', + '_', + ] +]; \ No newline at end of file diff --git a/lib/src/layouts.dart b/lib/src/layouts.dart new file mode 100644 index 0000000..8e7b2b8 --- /dev/null +++ b/lib/src/layouts.dart @@ -0,0 +1,6 @@ +part of virtual_keyboard_multi_language; + +enum VirtualKeyboardDefaultLayouts{ + Arabic, + English +} \ No newline at end of file diff --git a/lib/virtual_keyboard_multi_language.dart b/lib/virtual_keyboard_multi_language.dart index 41a38f3..2e78239 100644 --- a/lib/virtual_keyboard_multi_language.dart +++ b/lib/virtual_keyboard_multi_language.dart @@ -10,3 +10,4 @@ part './src/keyboard.dart'; part './src/rows.dart'; part './src/type.dart'; part './src/layout_keys.dart'; +part './src/layouts.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index 5bbe575..652cadf 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: virtual_keyboard_multi_language description: A simple package for dispaying virtual keyboards on a devices like kiosks and ATMs. The library is written in Dart and has no native code dependancy. -version: 0.2.0 +version: 0.2.1 author: Ahmed El-Araby homepage: https://github.com/ahmed-eg/virtual_keyboard_multi_language