Created
December 21, 2021 19:27
-
-
Save halka9000stg/ed6d8ae283945fd396b0b018af7c3acd to your computer and use it in GitHub Desktop.
キーボードショートカットとそのフォーカス管理のためのスニペット/ライブラリ(まだ正しい挙動はしない)
This file contains 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/services.dart'; | |
import 'package:flutter/widgets.dart'; | |
class ShortcutPrefer{ | |
LogicalKeyboardKey key; | |
void Function(Event) asNew; | |
static void Function(BuildContext) defaultHandler = (BuildContext context){}; | |
ShortcutPrefer(this.key, this.asNew); | |
} | |
class ShortcutPreferAfresh extends ShortcutPrefer{ | |
ShortcutPreferAfresh(LogicalKeyboardKey key, void Function(Event) asNew): super(key, asNew); | |
} | |
class ShortcutPreferRemove extends ShortcutPrefer{ | |
ShortcutPreferRemove(LogicalKeyboardKey key): super(key, ShortcutPrefer.defaultHandler); | |
} | |
class ShortcutPreferChange extends ShortcutPrefer{ | |
ShortcutPreferChange(LogicalKeyboardKey key, void Function(Event) asNew): super(key, asNew); | |
} | |
extension ShortcutListManager on List<ShortcutPrefer>{ | |
List<ShortcutPrefer> onKind(List<Type> tps){ | |
Type typrOfShortcuts = tps.where((Type tp)=>tp == ShortcutPreferAfresh || tp == ShortcutPreferRemove || tp == ShortcutPreferChange).toList(); | |
return this; | |
} | |
Map<LogicalKeySet, Intent> get toDisable{ | |
List<ShortcutPrefer> list = this.onKind([ShortcutPreferRemove, ShortcutPreferChange]); | |
return list.asMap().map((int ind, ShortcutPrefer pf)=>MapEntry(LogicalKeySet(key),Intent.doNothing)); | |
} | |
void toHandle(LogicalKeyboardKey actKey, BuildContext context){ | |
this.forEach((ShortcutPrefer pf)=>pf.key == actKey ? pf.asNew(context) : (){}()); | |
} | |
} | |
Widget Function(BuildContext) shortcutBuilder(FocusNode focusNode, List<ShortcutPrefer> preferences){ | |
return (BuildContext context){ | |
return Shortcuts( | |
shortcuts: preferences.toDisable, | |
child: RawKeyboardListener( | |
focusNode: focusNode, | |
onKey: (event) { | |
if (event is RawKeyDownEvent) { | |
final LogicalKeyboardKey logicalKey = event.logicalKey; | |
preferences.toHandle(logicalKey, context); | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cf.