Last active
May 1, 2023 16:55
-
-
Save loic-sharma/7c4e36958acbb4f4a1f674299826de2b to your computer and use it in GitHub Desktop.
CallbackShortcuts widget of the week example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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