flutter_hotkeys 1.0.1
flutter_hotkeys: ^1.0.1 copied to clipboard
The ultimate global and scoped keyboard shortcut manager for Flutter. Register hotkeys like 'ctrl+s' with a simple static API. Optimized for Desktop, Web, and Mobile.
import 'package:flutter/material.dart';
import 'package:flutter_hotkeys/flutter_hotkeys.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Hotkeys Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const HotkeyDemoPage(),
);
}
}
class HotkeyDemoPage extends StatefulWidget {
const HotkeyDemoPage({super.key});
@override
State<HotkeyDemoPage> createState() => _HotkeyDemoPageState();
}
class _HotkeyDemoPageState extends State<HotkeyDemoPage> {
String _lastAction = "Press a hotkey!";
@override
void initState() {
super.initState();
// Register global hotkeys
Hotkeys.register('ctrl+s', () {
_updateLastAction('Global Save triggered (ctrl+s)');
});
Hotkeys.register('ctrl+shift+p', () {
_updateLastAction('Global Command Palette triggered (ctrl+shift+p)');
});
Hotkeys.register('alt+enter', () {
_updateLastAction('Global Quick Fix triggered (alt+enter)');
});
}
void _updateLastAction(String action) {
setState(() {
_lastAction = action;
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(action), duration: const Duration(seconds: 1)),
);
}
@override
void dispose() {
Hotkeys.clear();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Hotkeys Demo'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Last Action:',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 8),
Text(
_lastAction,
style: Theme.of(context).textTheme.headlineMedium?.copyWith(
color: Colors.deepPurple,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 48),
const Text('Global shortcuts: ctrl+s, ctrl+shift+p, alt+enter'),
const SizedBox(height: 24),
const Divider(),
const SizedBox(height: 24),
const Text('Scoped Shortcuts (Focus the field below):'),
const SizedBox(height: 16),
SizedBox(
width: 300,
child: HotkeyScope(
shortcuts: {
'ctrl+b': () =>
_updateLastAction('Scoped Bold triggered (ctrl+b)'),
'ctrl+i': () =>
_updateLastAction('Scoped Italic triggered (ctrl+i)'),
},
child: const TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Editor (ctrl+b/ctrl+i)',
helperText: 'Shortcuts only work when focused',
),
),
),
),
],
),
),
);
}
}