Skip to content

Instantly share code, notes, and snippets.

@elliette
Created January 26, 2022 22:11
Show Gist options
  • Save elliette/04871fd9513f32d63f91b2b3c00416b7 to your computer and use it in GitHub Desktop.
Save elliette/04871fd9513f32d63f91b2b3c00416b7 to your computer and use it in GitHub Desktop.
Keyboard shortcuts and focus demo
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Keyboard Shorcuts Focus',
home: Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.escape): const EscapeIntent()
},
child: Actions(
actions: <Type, Action<Intent>>{
EscapeIntent: EscapeAction(),
},
child: Scaffold(
body: Center(
child: Row(
children: const <Widget>[
Spacer(),
Expanded(
child: TextField(),
),
Spacer(),
],
),
),
),
),
),
);
}
}
class EscapeIntent extends Intent {
const EscapeIntent();
}
class EscapeAction extends Action<EscapeIntent> {
@override
Object? invoke(covariant EscapeIntent intent) {
print('CALLED ESCAPE.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment