text controller and handler added

This commit is contained in:
Ahmed El-Araby 2020-03-19 16:43:25 +03:00
parent ff589ca673
commit f5c6e2be5a
8 changed files with 197 additions and 28 deletions

View File

@ -18,7 +18,7 @@
* Fixed the bug with backspace key. * Fixed the bug with backspace key.
## [0.2.0] - 19/03/2020. ## [0.2.1] - 19/03/2020.
* Adding multi-language support. * Adding multi-language support.
* Customizable layout. * Customizable layout.

View File

@ -44,6 +44,10 @@ Function onKeyPress;
double height; double height;
``` ```
```dart ```dart
/// Virtual keyboard height. Default is full screen width
double width;
```
```dart
// Color for key texts and icons. // Color for key texts and icons.
Color textColor; Color textColor;
``` ```
@ -55,6 +59,15 @@ double fontSize;;
// Only Caps letters enabled. // Only Caps letters enabled.
bool alwaysCaps;; 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<VirtualKeyboardDefaultLayouts> defaultLayouts;
```
### VirtualKeyboardType ### VirtualKeyboardType
enum of Available Virtual Keyboard Types. enum of Available Virtual Keyboard Types.
@ -114,10 +127,14 @@ Container(
child: VirtualKeyboard( child: VirtualKeyboard(
// Default height is 300 // Default height is 300
height: 350, height: 350,
// Default height is will screen width
width: 600,
// Default is black // Default is black
textColor: Colors.white, textColor: Colors.white,
// Default 14 // Default 14
fontSize: 20, fontSize: 20,
// the layouts supported
defaultLayouts = [VirtualKeyboardDefaultLayouts.English],
// [A-Z, 0-9] // [A-Z, 0-9]
type: VirtualKeyboardType.Alphanumeric, type: VirtualKeyboardType.Alphanumeric,
// Callback for key press event // Callback for key press event

View File

@ -33,11 +33,14 @@ class _MyHomePageState extends State<MyHomePage> {
bool shiftEnabled = false; bool shiftEnabled = false;
// is true will show the numeric keyboard. // is true will show the numeric keyboard.
bool isNumericMode = true; bool isNumericMode = false;
TextEditingController _controllerText;
@override @override
void initState() { void initState() {
_customLayoutKeys = CustomLayoutKeys(); _customLayoutKeys = CustomLayoutKeys();
_controllerText = TextEditingController();
super.initState(); super.initState();
} }
@ -54,6 +57,10 @@ class _MyHomePageState extends State<MyHomePage> {
text, text,
style: Theme.of(context).textTheme.display1, style: Theme.of(context).textTheme.display1,
), ),
Text(
_controllerText.text,
style: TextStyle(color: Colors.red),
),
SwitchListTile( SwitchListTile(
title: Text( title: Text(
'Keyboard Type = ' + 'Keyboard Type = ' +
@ -75,9 +82,11 @@ class _MyHomePageState extends State<MyHomePage> {
color: Colors.deepPurple, color: Colors.deepPurple,
child: VirtualKeyboard( child: VirtualKeyboard(
height: 300, height: 300,
width: 700, //width: 500,
textColor: Colors.white, textColor: Colors.white,
layoutKeys: _customLayoutKeys, textController: _controllerText,
//customLayoutKeys: _customLayoutKeys,
defaultLayouts: [VirtualKeyboardDefaultLayouts.Arabic,VirtualKeyboardDefaultLayouts.English],
type: isNumericMode type: isNumericMode
? VirtualKeyboardType.Numeric ? VirtualKeyboardType.Numeric
: VirtualKeyboardType.Alphanumeric, : VirtualKeyboardType.Alphanumeric,

View File

@ -17,7 +17,7 @@ class VirtualKeyboard extends StatefulWidget {
/// Virtual keyboard height. Default is 300 /// Virtual keyboard height. Default is 300
final double height; final double height;
/// Virtual keyboard height. Default is screen width /// Virtual keyboard height. Default is full screen width
final double width; final double width;
/// Color for key texts and icons. /// Color for key texts and icons.
@ -26,7 +26,11 @@ class VirtualKeyboard extends StatefulWidget {
/// Font size for keyboard keys. /// Font size for keyboard keys.
final double fontSize; 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. /// The builder function will be called for each Key object.
final Widget Function(BuildContext context, VirtualKeyboardKey key) builder; 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. /// Set to true if you want only to show Caps letters.
final bool alwaysCaps; 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<VirtualKeyboardDefaultLayouts> defaultLayouts;
VirtualKeyboard( VirtualKeyboard(
{Key key, {Key key,
@required this.type, @required this.type,
@required this.onKeyPress, this.onKeyPress,
this.builder, this.builder,
this.width, this.width,
this.layoutKeys, this.defaultLayouts,
this.customLayoutKeys,
this.textController,
this.height = _virtualKeyboardDefaultHeight, this.height = _virtualKeyboardDefaultHeight,
this.textColor = Colors.black, this.textColor = Colors.black,
this.fontSize = 14, this.fontSize = 14,
@ -57,6 +67,7 @@ class VirtualKeyboard extends StatefulWidget {
class _VirtualKeyboardState extends State<VirtualKeyboard> { class _VirtualKeyboardState extends State<VirtualKeyboard> {
VirtualKeyboardType type; VirtualKeyboardType type;
Function onKeyPress; Function onKeyPress;
TextEditingController textController;
// The builder function will be called for each Key object. // The builder function will be called for each Key object.
Widget Function(BuildContext context, VirtualKeyboardKey key) builder; Widget Function(BuildContext context, VirtualKeyboardKey key) builder;
double height; double height;
@ -64,13 +75,46 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
Color textColor; Color textColor;
double fontSize; double fontSize;
bool alwaysCaps; bool alwaysCaps;
VirtualKeyboardLayoutKeys layoutKeys; VirtualKeyboardLayoutKeys customLayoutKeys;
// Text Style for keys. // Text Style for keys.
TextStyle textStyle; TextStyle textStyle;
// True if shift is enabled. // True if shift is enabled.
bool isShiftEnabled = false; 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 @override
void didUpdateWidget(Widget oldWidget) { void didUpdateWidget(Widget oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
@ -82,7 +126,8 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
textColor = widget.textColor; textColor = widget.textColor;
fontSize = widget.fontSize; fontSize = widget.fontSize;
alwaysCaps = widget.alwaysCaps; alwaysCaps = widget.alwaysCaps;
layoutKeys = widget.layoutKeys ?? VirtualKeyboardLayoutKeys(); textController = widget.textController ?? textController;
customLayoutKeys = widget.customLayoutKeys ?? customLayoutKeys ;
// Init the Text Style for keys. // Init the Text Style for keys.
textStyle = TextStyle( textStyle = TextStyle(
fontSize: fontSize, fontSize: fontSize,
@ -95,9 +140,10 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
void initState() { void initState() {
super.initState(); super.initState();
textController = widget.textController ?? TextEditingController();
width = widget.width; width = widget.width;
type = widget.type; type = widget.type;
layoutKeys = widget.layoutKeys ?? VirtualKeyboardLayoutKeys(); customLayoutKeys = widget.customLayoutKeys ?? VirtualKeyboardDefaultLayoutKeys(widget.defaultLayouts ?? [VirtualKeyboardDefaultLayouts.English]);
onKeyPress = widget.onKeyPress; onKeyPress = widget.onKeyPress;
height = widget.height; height = widget.height;
textColor = widget.textColor; textColor = widget.textColor;
@ -146,7 +192,7 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
List<List<VirtualKeyboardKey>> keyboardRows = List<List<VirtualKeyboardKey>> keyboardRows =
type == VirtualKeyboardType.Numeric type == VirtualKeyboardType.Numeric
? _getKeyboardRowsNumeric() ? _getKeyboardRowsNumeric()
: _getKeyboardRows(layoutKeys); : _getKeyboardRows(customLayoutKeys);
// Generate keyboard row. // Generate keyboard row.
List<Widget> rows = List.generate(keyboardRows.length, (int rowNum) { List<Widget> rows = List.generate(keyboardRows.length, (int rowNum) {
@ -207,10 +253,10 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
return Expanded( return Expanded(
child: InkWell( child: InkWell(
onTap: () { onTap: () {
onKeyPress(key); _onKeyPress(key);
}, },
child: Container( child: Container(
height: height / layoutKeys.activeLayout.length, height: height / customLayoutKeys.activeLayout.length,
child: Center( child: Center(
child: Text( child: Text(
alwaysCaps alwaysCaps
@ -238,7 +284,7 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
Duration(milliseconds: _virtualKeyboardBackspaceEventPerioud), Duration(milliseconds: _virtualKeyboardBackspaceEventPerioud),
(timer) { (timer) {
if (longPress) { if (longPress) {
onKeyPress(key); _onKeyPress(key);
} else { } else {
// Cancel timer. // Cancel timer.
timer.cancel(); timer.cancel();
@ -274,7 +320,7 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
actionKey = GestureDetector( actionKey = GestureDetector(
onTap: () { onTap: () {
setState(() { setState(() {
layoutKeys.switchLanguage(); customLayoutKeys.switchLanguage();
}); });
}, },
child: Container( child: Container(
@ -299,11 +345,11 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
} }
} }
onKeyPress(key); _onKeyPress(key);
}, },
child: Container( child: Container(
alignment: Alignment.center, alignment: Alignment.center,
height: height / layoutKeys.activeLayout.length, height: height / customLayoutKeys.activeLayout.length,
child: actionKey, child: actionKey,
), ),
); );

View File

@ -1,15 +1,16 @@
part of virtual_keyboard_multi_language; part of virtual_keyboard_multi_language;
//import '../virtual_keyboard_multi_language.dart'; //import '../virtual_keyboard_multi_language.dart';
class VirtualKeyboardLayoutKeys{ abstract class VirtualKeyboardLayoutKeys{
int getLanguagesCount() => 1;
int activeIndex =0; int activeIndex =0;
List<List> getLanguage(int index){ List<List> get defaultEnglishLayout => _defaultEnglishLayout;
return _defaultEnglishLayout; List<List> get defaultArabicLayout => _defaultArabicLayout;
}
List<List> get activeLayout => getLanguage(activeIndex);
int getLanguagesCount();
List<List> getLanguage(int index);
void switchLanguage(){ void switchLanguage(){
if((activeIndex+1) == getLanguagesCount()) if((activeIndex+1) == getLanguagesCount())
@ -17,9 +18,28 @@ class VirtualKeyboardLayoutKeys{
else activeIndex++; else activeIndex++;
} }
List<List> get defaultEnglishLayout => _defaultEnglishLayout; }
class VirtualKeyboardDefaultLayoutKeys extends VirtualKeyboardLayoutKeys{
List<VirtualKeyboardDefaultLayouts> defaultLayouts;
VirtualKeyboardDefaultLayoutKeys(this.defaultLayouts);
int getLanguagesCount() => defaultLayouts.length;
List<List> getLanguage(int index){
switch(defaultLayouts[index]){
case VirtualKeyboardDefaultLayouts.English:
return _defaultEnglishLayout;
case VirtualKeyboardDefaultLayouts.Arabic:
return _defaultArabicLayout;
default:
}
return _defaultEnglishLayout;
}
List<List> get activeLayout => getLanguage(activeIndex);
} }
@ -91,3 +111,73 @@ const List<List> _defaultEnglishLayout = [
'_', '_',
] ]
]; ];
const List<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,
'-',
'_',
]
];

6
lib/src/layouts.dart Normal file
View File

@ -0,0 +1,6 @@
part of virtual_keyboard_multi_language;
enum VirtualKeyboardDefaultLayouts{
Arabic,
English
}

View File

@ -10,3 +10,4 @@ part './src/keyboard.dart';
part './src/rows.dart'; part './src/rows.dart';
part './src/type.dart'; part './src/type.dart';
part './src/layout_keys.dart'; part './src/layout_keys.dart';
part './src/layouts.dart';

View File

@ -1,6 +1,6 @@
name: virtual_keyboard_multi_language 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. 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 <ahmed-eg@live.com> author: Ahmed El-Araby <ahmed-eg@live.com>
homepage: https://github.com/ahmed-eg/virtual_keyboard_multi_language homepage: https://github.com/ahmed-eg/virtual_keyboard_multi_language