Reputation: 624
I have a Flutter widget that looks like this:
class KeyboardShortcuts extends ConsumerWidget {
final Widget child;
const KeyboardShortcuts({super.key, required this.child});
@override
Widget build(BuildContext context, WidgetRef ref) {
final km = Keymap().keys;
final Map<ShortcutActivator, Intent> shortcutMap = {
km[KeymapId.toggleRightPanel]!: TogglePanelRightIntent(),
km[KeymapId.toggleLeftPanel]!: TogglePanelLeftIntent()
};
final Map<Type, Action> actionMap = {
TogglePanelRightIntent: TogglePanelRightAction(ref),
TogglePanelLeftIntent: TogglePanelLeftAction(ref),
};
return Shortcuts(
shortcuts: shortcutMap,
child: Actions(actions: actionMap, child: child),
);
}
}
This app is likely to grow a pretty length list of keyboard shortcuts, and I'd love to be able to apply this pattern through a for loop, but I run into this issue with dart's Type
type. I would like to accomplish something like this:
class KeyboardShortcutGroup {
final Type action;
final Type intent;
final Activator activator;
const KeyboardShortcutGroup({required this.action, required this.intent, required this.activator});
}
List<KeyboardShortcutGroup> getShortcuts() {
return [
KeyboardShortcutGroup(action: TogglePanelLeftAction, intent: TogglePanelRightIntent)
];
}
But my question is then, how do I convert the Type
types back to their appropriate class? It's a bit of a conundrum because the Actions
widget requires the Type
type as the key of the actions
map. Also, if you hadn't already guessed... I've worked with Flutter and Dart part time for less than a week... so forgive me if the answer is obvious.
Upvotes: 0
Views: 39