text controller and handler added
This commit is contained in:
parent
ff589ca673
commit
f5c6e2be5a
@ -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.
|
17
README.md
17
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<VirtualKeyboardDefaultLayouts> 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
|
||||
|
@ -33,11 +33,14 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
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<MyHomePage> {
|
||||
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<MyHomePage> {
|
||||
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,
|
||||
|
@ -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<VirtualKeyboardDefaultLayouts> 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<VirtualKeyboard> {
|
||||
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<VirtualKeyboard> {
|
||||
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<VirtualKeyboard> {
|
||||
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,
|
||||
@ -95,9 +140,10 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
|
||||
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<VirtualKeyboard> {
|
||||
List<List<VirtualKeyboardKey>> keyboardRows =
|
||||
type == VirtualKeyboardType.Numeric
|
||||
? _getKeyboardRowsNumeric()
|
||||
: _getKeyboardRows(layoutKeys);
|
||||
: _getKeyboardRows(customLayoutKeys);
|
||||
|
||||
// Generate keyboard row.
|
||||
List<Widget> rows = List.generate(keyboardRows.length, (int rowNum) {
|
||||
@ -207,10 +253,10 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
|
||||
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<VirtualKeyboard> {
|
||||
Duration(milliseconds: _virtualKeyboardBackspaceEventPerioud),
|
||||
(timer) {
|
||||
if (longPress) {
|
||||
onKeyPress(key);
|
||||
_onKeyPress(key);
|
||||
} else {
|
||||
// Cancel timer.
|
||||
timer.cancel();
|
||||
@ -274,7 +320,7 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
|
||||
actionKey = GestureDetector(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
layoutKeys.switchLanguage();
|
||||
customLayoutKeys.switchLanguage();
|
||||
});
|
||||
},
|
||||
child: Container(
|
||||
@ -299,11 +345,11 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
|
||||
}
|
||||
}
|
||||
|
||||
onKeyPress(key);
|
||||
_onKeyPress(key);
|
||||
},
|
||||
child: Container(
|
||||
alignment: Alignment.center,
|
||||
height: height / layoutKeys.activeLayout.length,
|
||||
height: height / customLayoutKeys.activeLayout.length,
|
||||
child: actionKey,
|
||||
),
|
||||
);
|
||||
|
@ -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<List> getLanguage(int index){
|
||||
return _defaultEnglishLayout;
|
||||
}
|
||||
List<List> get defaultEnglishLayout => _defaultEnglishLayout;
|
||||
List<List> get defaultArabicLayout => _defaultArabicLayout;
|
||||
|
||||
List<List> get activeLayout => getLanguage(activeIndex);
|
||||
int getLanguagesCount();
|
||||
List<List> getLanguage(int index);
|
||||
|
||||
void switchLanguage(){
|
||||
if((activeIndex+1) == getLanguagesCount())
|
||||
@ -17,9 +18,28 @@ class VirtualKeyboardLayoutKeys{
|
||||
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
6
lib/src/layouts.dart
Normal file
@ -0,0 +1,6 @@
|
||||
part of virtual_keyboard_multi_language;
|
||||
|
||||
enum VirtualKeyboardDefaultLayouts{
|
||||
Arabic,
|
||||
English
|
||||
}
|
@ -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';
|
||||
|
@ -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 <ahmed-eg@live.com>
|
||||
homepage: https://github.com/ahmed-eg/virtual_keyboard_multi_language
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user