flutter_onscreen_keyboard 0.0.3+1
flutter_onscreen_keyboard: ^0.0.3+1 copied to clipboard
A customizable and extensible virtual keyboard widget for Flutter desktop and touchscreen apps.
flutter_onscreen_keyboard #
A customizable and extensible on-screen virtual keyboard for Flutter applications. Ideal for desktop and touchscreen environments where physical keyboards are unavailable or limited.
β¨ Features #
- π§© Customizable Layouts β Tailor the keyboard layout and style to suit your UI.
- π οΈ Extensible Architecture β Add custom keys or override behavior easily.
- π» Full Desktop Keyboard β Complete support for alphabetic, numeric, symbol, and function keys.
- π€ Integrated Text Field β Comes with a dedicated
OnscreenKeyboardTextField
widget to easily handle user input. - π₯οΈ Designed for Desktop and Touch Devices β Ideal for touchscreen setups like POS systems.
π Getting Started #
π¦ Installation #
Add the package to your pubspec.yaml
:
dependencies:
flutter_onscreen_keyboard: ^0.0.3+1
π§ͺ Usage #
β Add OnscreenKeyboard
to Your Root Widget #
There are two ways to integrate the keyboard into your root widget:
- Using
OnscreenKeyboard.builder
.
return MaterialApp(
builder: OnscreenKeyboard.builder(), // add this line
home: const HomeScreen(),
);
- Using
OnscreenKeyboard.wrap
.
return MaterialApp(
builder: (context, child) {
// your other codes
// child = OtherWidgets(child: child!);
// wrap with OnscreenKeyboard.wrap
return OnscreenKeyboard.wrap(child: child);
},
home: const HomeScreen(),
);
π§Ύ Use OnscreenKeyboardTextField
Anywhere #
You can place the OnscreenKeyboardTextField
widget anywhere in your app. It will automatically connect with the keyboard and handle input seamlessly.
@override
Widget build(BuildContext context) {
return const OnscreenKeyboardTextField(
// enableOnscreenKeyboard: false, // default to true
),
}
ποΈ Access the Keyboard Controller #
Use OnscreenKeyboard.of(context)
to get the keyboard controller instance.
final keyboardController = OnscreenKeyboard.of(context);
π Open and Close the Keyboard Programmatically #
With the controller, you can open or close the keyboard as needed in your application flow.
keyboardController.open(); // open the keyboard
keyboardController.close(); // close the keyboard
πΉ Listen to Key Events #
To respond to key presses globally, use the addRawKeyDownListener
method.
class _AppState extends State<App> {
late final keyboard = OnscreenKeyboard.of(context);
@override
void initState() {
super.initState();
// listener for raw keyboard events
keyboard.addRawKeyDownListener(_listener);
}
@override
void dispose() {
keyboard.removeRawKeyDownListener(_listener);
super.dispose();
}
void _listener(OnscreenKeyboardKey key) {
switch (key) {
case TextKey(:final primary): // a text key: "a", "b", "4", etc.
log('key: "$primary"');
case ActionKey(:final name): // an action key: "shift", "backspace", etc.
log('action: $name');
}
}
@override
Widget build(BuildContext context) {
// ...
}
}
π¨ Customization #
- Styles: Customize key colors, shapes, and padding.
- Layouts: Switch between full, numeric, or custom layouts (coming soon).
- Behaviors: Override key presses and implement custom actions.
return MaterialApp(
builder: OnscreenKeyboard.builder(
theme: const OnscreenKeyboardThemeData(
textKeyThemeData: TextKeyThemeData(
decoration: BoxDecoration(
color: Colors.red,
),
),
),
// ...more options
),
);
π Repository #
Browse the source code and contribute here: π https://github.com/albinpk/flutter_onscreen_keyboard
π οΈ Contributing #
Contributions, issues, and feature requests are welcome! See the CONTRIBUTING.md for guidelines.
π License #
This project is licensed under the MIT License.
π Credits #
Created and maintained by Albin PK. If you find this package useful, consider giving it a β on GitHub and a like on pub.flutter-io.cn!