Added alwaysCaps property and longPress for backspace.

This commit is contained in:
Gurgen Hovhannisyan 2019-04-02 15:45:40 +04:00
parent 8310f6bdc4
commit 6fee432a8b
5 changed files with 60 additions and 15 deletions

View File

@ -9,3 +9,7 @@
## [0.1.1] - 03/28/2019. ## [0.1.1] - 03/28/2019.
* Fixed state update issue. * Fixed state update issue.
## [0.1.2] - 03/01/2019.
* Added alwaysCaps property and longPress for backspace.

View File

@ -40,6 +40,10 @@ Color textColor;
// Font size for keyboard keys. // Font size for keyboard keys.
double fontSize;; double fontSize;;
``` ```
```dart
// Only Caps letters enabled.
bool alwaysCaps;;
```
### VirtualKeyboardType ### VirtualKeyboardType
enum of Available Virtual Keyboard Types. enum of Available Virtual Keyboard Types.

View File

@ -4,6 +4,8 @@ part of virtual_keyboard;
/// `height` argument to `VirtualKeyboard` widget. /// `height` argument to `VirtualKeyboard` widget.
const double _virtualKeyboardDefaultHeight = 300; const double _virtualKeyboardDefaultHeight = 300;
const int _virtualKeyboardBackspaceEventPerioud = 250;
/// Virtual Keyboard widget. /// Virtual Keyboard widget.
class VirtualKeyboard extends StatefulWidget { class VirtualKeyboard extends StatefulWidget {
/// Keyboard Type: Should be inited in creation time. /// Keyboard Type: Should be inited in creation time.
@ -18,12 +20,15 @@ class VirtualKeyboard extends StatefulWidget {
/// Color for key texts and icons. /// Color for key texts and icons.
final Color textColor; final Color textColor;
// Font size for keyboard keys. /// Font size for keyboard keys.
final double fontSize; final double fontSize;
// 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;
/// Set to true if you want only to show Caps letters.
final bool alwaysCaps;
VirtualKeyboard( VirtualKeyboard(
{Key key, {Key key,
@required this.type, @required this.type,
@ -31,7 +36,8 @@ class VirtualKeyboard extends StatefulWidget {
this.builder, this.builder,
this.height = _virtualKeyboardDefaultHeight, this.height = _virtualKeyboardDefaultHeight,
this.textColor = Colors.black, this.textColor = Colors.black,
this.fontSize = 14}) this.fontSize = 14,
this.alwaysCaps = false})
: super(key: key); : super(key: key);
@override @override
@ -49,7 +55,7 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
double height; double height;
Color textColor; Color textColor;
double fontSize; double fontSize;
bool alwaysCaps;
// Text Style for keys. // Text Style for keys.
TextStyle textStyle; TextStyle textStyle;
@ -65,6 +71,7 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
height = widget.height; height = widget.height;
textColor = widget.textColor; textColor = widget.textColor;
fontSize = widget.fontSize; fontSize = widget.fontSize;
alwaysCaps = widget.alwaysCaps;
// Init the Text Style for keys. // Init the Text Style for keys.
textStyle = TextStyle( textStyle = TextStyle(
@ -83,6 +90,8 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
height = widget.height; height = widget.height;
textColor = widget.textColor; textColor = widget.textColor;
fontSize = widget.fontSize; fontSize = widget.fontSize;
alwaysCaps = widget.alwaysCaps;
// Init the Text Style for keys. // Init the Text Style for keys.
textStyle = TextStyle( textStyle = TextStyle(
fontSize: fontSize, fontSize: fontSize,
@ -189,7 +198,9 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
height: height / _keyRows.length, height: height / _keyRows.length,
child: Center( child: Center(
child: Text( child: Text(
isShiftEnabled ? key.capsText : key.text, alwaysCaps
? key.capsText
: (isShiftEnabled ? key.capsText : key.text),
style: textStyle, style: textStyle,
)), )),
), ),
@ -201,12 +212,35 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
// Holds the action key widget. // Holds the action key widget.
Widget actionKey; Widget actionKey;
// True if long press is enabled.
bool longPress;
// Switch the action type to build action Key widget. // Switch the action type to build action Key widget.
switch (key.action) { switch (key.action) {
case VirtualKeyboardKeyAction.Backspace: case VirtualKeyboardKeyAction.Backspace:
actionKey = Icon( actionKey = GestureDetector(
onLongPress: () {
longPress = true;
// Start sending backspace key events while longPress is true
Timer.periodic(
Duration(milliseconds: _virtualKeyboardBackspaceEventPerioud),
(timer) {
if (longPress) {
onKeyPress(key);
} else {
// Cancel timer.
timer.cancel();
}
});
},
onLongPressUp: () {
// Cancel event loop
longPress = false;
},
child: Icon(
Icons.backspace, Icons.backspace,
color: textColor, color: textColor,
),
); );
break; break;
case VirtualKeyboardKeyAction.Shift: case VirtualKeyboardKeyAction.Shift:
@ -226,6 +260,7 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
return Expanded( return Expanded(
child: InkWell( child: InkWell(
onTap: () { onTap: () {
if (!alwaysCaps) {
if (key.action == VirtualKeyboardKeyAction.Shift) { if (key.action == VirtualKeyboardKeyAction.Shift) {
setState(() { setState(() {
isShiftEnabled = !isShiftEnabled; isShiftEnabled = !isShiftEnabled;
@ -233,6 +268,7 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
} }
onKeyPress(key); onKeyPress(key);
}
}, },
child: Container( child: Container(
height: height / _keyRows.length, height: height / _keyRows.length,

View File

@ -1,5 +1,6 @@
library virtual_keyboard; library virtual_keyboard;
import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
part './src/key_action.dart'; part './src/key_action.dart';

View File

@ -1,6 +1,6 @@
name: virtual_keyboard name: virtual_keyboard
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.1.1 version: 0.1.2
author: Gurgen Hovhannisyan <g.hovhannisyan@digitalpomegranate.com> author: Gurgen Hovhannisyan <g.hovhannisyan@digitalpomegranate.com>
homepage: https://github.com/gurgenDP/virtual_keyboard homepage: https://github.com/gurgenDP/virtual_keyboard