Skip to content

Commit

Permalink
Added alwaysCaps property and longPress for backspace.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurgen Hovhannisyan authored and Gurgen Hovhannisyan committed Apr 2, 2019
1 parent 8310f6b commit 6fee432
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
## [0.1.1] - 03/28/2019.

* Fixed state update issue.

## [0.1.2] - 03/01/2019.

* Added alwaysCaps property and longPress for backspace.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Color textColor;
// Font size for keyboard keys.
double fontSize;;
```
```dart
// Only Caps letters enabled.
bool alwaysCaps;;
```

### VirtualKeyboardType
enum of Available Virtual Keyboard Types.
Expand Down
64 changes: 50 additions & 14 deletions lib/src/keyboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ part of virtual_keyboard;
/// `height` argument to `VirtualKeyboard` widget.
const double _virtualKeyboardDefaultHeight = 300;

const int _virtualKeyboardBackspaceEventPerioud = 250;

/// Virtual Keyboard widget.
class VirtualKeyboard extends StatefulWidget {
/// Keyboard Type: Should be inited in creation time.
Expand All @@ -18,20 +20,24 @@ class VirtualKeyboard extends StatefulWidget {
/// Color for key texts and icons.
final Color textColor;

// Font size for keyboard keys.
/// Font size for keyboard keys.
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;

/// Set to true if you want only to show Caps letters.
final bool alwaysCaps;

VirtualKeyboard(
{Key key,
@required this.type,
@required this.onKeyPress,
this.builder,
this.height = _virtualKeyboardDefaultHeight,
this.textColor = Colors.black,
this.fontSize = 14})
this.fontSize = 14,
this.alwaysCaps = false})
: super(key: key);

@override
Expand All @@ -49,7 +55,7 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
double height;
Color textColor;
double fontSize;

bool alwaysCaps;
// Text Style for keys.
TextStyle textStyle;

Expand All @@ -65,6 +71,7 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
height = widget.height;
textColor = widget.textColor;
fontSize = widget.fontSize;
alwaysCaps = widget.alwaysCaps;

// Init the Text Style for keys.
textStyle = TextStyle(
Expand All @@ -83,6 +90,8 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
height = widget.height;
textColor = widget.textColor;
fontSize = widget.fontSize;
alwaysCaps = widget.alwaysCaps;

// Init the Text Style for keys.
textStyle = TextStyle(
fontSize: fontSize,
Expand Down Expand Up @@ -189,7 +198,9 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
height: height / _keyRows.length,
child: Center(
child: Text(
isShiftEnabled ? key.capsText : key.text,
alwaysCaps
? key.capsText
: (isShiftEnabled ? key.capsText : key.text),
style: textStyle,
)),
),
Expand All @@ -201,12 +212,35 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
// Holds the action key widget.
Widget actionKey;

// True if long press is enabled.
bool longPress;

// Switch the action type to build action Key widget.
switch (key.action) {
case VirtualKeyboardKeyAction.Backspace:
actionKey = Icon(
Icons.backspace,
color: textColor,
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,
color: textColor,
),
);
break;
case VirtualKeyboardKeyAction.Shift:
Expand All @@ -226,13 +260,15 @@ class _VirtualKeyboardState extends State<VirtualKeyboard> {
return Expanded(
child: InkWell(
onTap: () {
if (key.action == VirtualKeyboardKeyAction.Shift) {
setState(() {
isShiftEnabled = !isShiftEnabled;
});
}
if (!alwaysCaps) {
if (key.action == VirtualKeyboardKeyAction.Shift) {
setState(() {
isShiftEnabled = !isShiftEnabled;
});
}

onKeyPress(key);
onKeyPress(key);
}
},
child: Container(
height: height / _keyRows.length,
Expand Down
1 change: 1 addition & 0 deletions lib/virtual_keyboard.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library virtual_keyboard;

import 'dart:async';
import 'package:flutter/material.dart';

part './src/key_action.dart';
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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.
version: 0.1.1
version: 0.1.2
author: Gurgen Hovhannisyan <g.hovhannisyan@digitalpomegranate.com>
homepage: https://github.com/gurgenDP/virtual_keyboard

Expand Down

0 comments on commit 6fee432

Please sign in to comment.