Add KeyboardArrowListener to experiment with hz arrow key support on lists
This commit is contained in:
parent
1a51d2e906
commit
0c255bec9a
56
lib/ui/common/keyboard_arrows_listener.dart
Normal file
56
lib/ui/common/keyboard_arrows_listener.dart
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import 'package:wonders/common_libs.dart';
|
||||||
|
|
||||||
|
class ArrowDir {
|
||||||
|
ArrowDir(this.hz, this.vt);
|
||||||
|
int hz;
|
||||||
|
int vt;
|
||||||
|
|
||||||
|
bool get isNotZero => hz != 0 || vt != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
class KeyboardArrowsListener extends StatefulWidget {
|
||||||
|
const KeyboardArrowsListener({Key? key, required this.child, required this.onArrow}) : super(key: key);
|
||||||
|
final Widget child;
|
||||||
|
final void Function(ArrowDir dir) onArrow;
|
||||||
|
@override
|
||||||
|
State<KeyboardArrowsListener> createState() => _KeyboardArrowsListenerState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _KeyboardArrowsListenerState extends State<KeyboardArrowsListener> {
|
||||||
|
final FocusNode _focusNode = FocusNode();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return RawKeyboardListener(
|
||||||
|
autofocus: true,
|
||||||
|
focusNode: _focusNode,
|
||||||
|
onKey: _handleKey,
|
||||||
|
child: widget.child,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _handleKey(RawKeyEvent event) {
|
||||||
|
if (event is! RawKeyDownEvent) return;
|
||||||
|
if (event.repeat) return;
|
||||||
|
final arrowDir = ArrowDir(0, 0);
|
||||||
|
if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
|
||||||
|
arrowDir.hz = -1;
|
||||||
|
} else if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
|
||||||
|
arrowDir.hz = 1;
|
||||||
|
}
|
||||||
|
if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
|
||||||
|
arrowDir.vt = 1;
|
||||||
|
} else if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
|
||||||
|
arrowDir.vt = -1;
|
||||||
|
}
|
||||||
|
if (arrowDir.isNotZero) {
|
||||||
|
widget.onArrow(arrowDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_focusNode.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user