simple_keyboard_listener 1.0.1
simple_keyboard_listener: ^1.0.1 copied to clipboard
A widget that trigger an action when given keys are pressed
example/README.md
A simple keyboard listener widget #
Here is an example :
import 'package:flutter/material.dart';
import 'package:simple_keyboard_listener/simple_keyboard_listener.dart';
void main() {
runApp(const MyApp());
}
/// This is the default "New Flutter project" counter app
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple)),
home: const MyHomePage(title: 'A Simple Keyboard listener'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String? _lastTypedKey;
int _counter = 0;
void _onKeyboardKeyPress(KeyEvent e) {
setState(() {
_counter++;
_lastTypedKey = e.label;
});
}
@override
Widget build(BuildContext context) {
return SimpleKeyboardListener(
onPressed: _onKeyboardKeyPress,
child: Scaffold(
appBar: AppBar(backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title)),
body: Center(
child: Column(
spacing: 12.0,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircleAvatar(
radius: 64,
child: Text(_lastTypedKey ?? '?'),
),
const Text('You have typed keyboard keys this many times:'),
Text('$_counter', style: Theme.of(context).textTheme.headlineMedium),
],
),
),
),
);
}
}