From 6fee432a8bbc1a56e86e07f69fa4c6e4cdead4e1 Mon Sep 17 00:00:00 2001 From: Gurgen Hovhannisyan Date: Tue, 2 Apr 2019 15:45:40 +0400 Subject: [PATCH] Added alwaysCaps property and longPress for backspace. --- CHANGELOG.md | 4 +++ README.md | 4 +++ lib/src/keyboard.dart | 64 ++++++++++++++++++++++++++++++--------- lib/virtual_keyboard.dart | 1 + pubspec.yaml | 2 +- 5 files changed, 60 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e1529f..96e90e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index dc7bcf1..211224a 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/src/keyboard.dart b/lib/src/keyboard.dart index aaeb967..815fbe7 100644 --- a/lib/src/keyboard.dart +++ b/lib/src/keyboard.dart @@ -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. @@ -18,12 +20,15 @@ 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, @@ -31,7 +36,8 @@ class VirtualKeyboard extends StatefulWidget { this.builder, this.height = _virtualKeyboardDefaultHeight, this.textColor = Colors.black, - this.fontSize = 14}) + this.fontSize = 14, + this.alwaysCaps = false}) : super(key: key); @override @@ -49,7 +55,7 @@ class _VirtualKeyboardState extends State { double height; Color textColor; double fontSize; - + bool alwaysCaps; // Text Style for keys. TextStyle textStyle; @@ -65,6 +71,7 @@ class _VirtualKeyboardState extends State { height = widget.height; textColor = widget.textColor; fontSize = widget.fontSize; + alwaysCaps = widget.alwaysCaps; // Init the Text Style for keys. textStyle = TextStyle( @@ -83,6 +90,8 @@ class _VirtualKeyboardState extends State { height = widget.height; textColor = widget.textColor; fontSize = widget.fontSize; + alwaysCaps = widget.alwaysCaps; + // Init the Text Style for keys. textStyle = TextStyle( fontSize: fontSize, @@ -189,7 +198,9 @@ class _VirtualKeyboardState extends State { height: height / _keyRows.length, child: Center( child: Text( - isShiftEnabled ? key.capsText : key.text, + alwaysCaps + ? key.capsText + : (isShiftEnabled ? key.capsText : key.text), style: textStyle, )), ), @@ -201,12 +212,35 @@ class _VirtualKeyboardState extends State { // 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: @@ -226,13 +260,15 @@ class _VirtualKeyboardState extends State { 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, diff --git a/lib/virtual_keyboard.dart b/lib/virtual_keyboard.dart index 18b1093..2958ed0 100644 --- a/lib/virtual_keyboard.dart +++ b/lib/virtual_keyboard.dart @@ -1,5 +1,6 @@ library virtual_keyboard; +import 'dart:async'; import 'package:flutter/material.dart'; part './src/key_action.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index e202b31..231d4cc 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 homepage: https://github.com/gurgenDP/virtual_keyboard