Skip to content

Instantly share code, notes, and snippets.

@loic-sharma
Last active May 1, 2023 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loic-sharma/7c4e36958acbb4f4a1f674299826de2b to your computer and use it in GitHub Desktop.
Save loic-sharma/7c4e36958acbb4f4a1f674299826de2b to your computer and use it in GitHub Desktop.
CallbackShortcuts widget of the week example
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('CallbackShortcuts example')),
body: const Center(
child: CallbackShortcutsExample(),
),
),
);
}
}
class CallbackShortcutsExample extends StatefulWidget {
const CallbackShortcutsExample({super.key});
@override
State<CallbackShortcutsExample> createState() =>
_CallbackShortcutsExampleState();
}
class _CallbackShortcutsExampleState extends State<CallbackShortcutsExample> {
int count = 0;
@override
Widget build(BuildContext context) {
return CallbackShortcuts(
bindings: <ShortcutActivator, VoidCallback>{
const CharacterActivator('+'): () {
setState(() => count = count + 1);
},
const CharacterActivator('-'): () {
setState(() => count = count - 1);
},
const SingleActivator(LogicalKeyboardKey.arrowUp): () {
setState(() => count = count + 1);
},
const SingleActivator(LogicalKeyboardKey.arrowDown): () {
setState(() => count = count - 1);
},
},
child: Focus(
autofocus: true,
child: Text('Counter: $count'),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment