Skip to content

Instantly share code, notes, and snippets.

@dkwingsmt
Last active June 8, 2022 22:36
Show Gist options
  • Select an option

  • Save dkwingsmt/023d4cb53f8678d138dc57670fc22fa8 to your computer and use it in GitHub Desktop.

Select an option

Save dkwingsmt/023d4cb53f8678d138dc57670fc22fa8 to your computer and use it in GitHub Desktop.
Shortcut priority
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
class MyIntent extends Intent {
const MyIntent(this.content);
final String content;
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Shortcuts(
shortcuts: const <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.keyB, control: true): MyIntent('Ctrl-B'),
SingleActivator(LogicalKeyboardKey.keyB, meta: true): MyIntent('Cmd-B'),
},
child: MaterialApp(
home: Scaffold(
body: MyWidget(),
),
),
);
}
}
class MyWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => MyWidgetState();
}
class MyWidgetState extends State<MyWidget> {
String status = "";
@override
Widget build(BuildContext context) {
return Actions(
actions: <Type, Action<Intent>>{
MyIntent: CallbackAction<MyIntent>(onInvoke: (MyIntent intent) {
setState(() {
status = intent.content;
});
return true;
}),
},
child: Column(
children: <Widget>[
Text(status),
const Shortcuts(
shortcuts: <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.tab): MyIntent('Tab'),
},
child: TextField(),
),
Shortcuts(
shortcuts: <ShortcutActivator, Intent>{
const SingleActivator(LogicalKeyboardKey.keyB, control: true): DoNothingAndStopPropagationIntent(),
const SingleActivator(LogicalKeyboardKey.keyB, meta: true): DoNothingAndStopPropagationIntent(),
},
child: const TextField(),
)
],
),
);
}
}
void main() => runApp(MyApp());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment