From 5c69b5c02038de4ec1b1c10a5f1abf1ffa230fed Mon Sep 17 00:00:00 2001 From: Nico Audy Date: Tue, 20 Jun 2023 10:04:59 +0700 Subject: [PATCH] fix on click event --- CHANGELOG.md | 4 ++ README.md | 2 +- lib/dpad_container.dart | 108 +++++++++++++++++++++------------------- pubspec.yaml | 2 +- 4 files changed, 63 insertions(+), 53 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e27e862..5265d00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [2.0.4] - 20/06/2023 + +- Fix double on click event + ## [2.0.3] - 03/02/2023 - Code formatting standard diff --git a/README.md b/README.md index e9408b4..db808c3 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ In your flutter or dart project add the dependency: ```yml dependencies: ... - dpad_container: ^2.0.3 + dpad_container: ^2.0.4 ``` For help getting started with Flutter, view the online diff --git a/lib/dpad_container.dart b/lib/dpad_container.dart index 6e1b4f6..048e545 100644 --- a/lib/dpad_container.dart +++ b/lib/dpad_container.dart @@ -1,51 +1,57 @@ -import 'package:flutter/widgets.dart'; -import 'package:flutter_hooks/flutter_hooks.dart'; - -/// Action constants -const String keyUp = 'Arrow Up'; -const String keyDown = 'Arrow Down'; -const String keyLeft = 'Arrow Left'; -const String keyRight = 'Arrow Right'; -const String keyCenter = 'Select'; - -/// -/// Using Hooks instead of statefull builder -/// Make coding looks nice and compact -class DpadContainer extends HookWidget { - final Function onClick; - final Function(bool isFocused) onFocus; - final Widget child; - - const DpadContainer({ - Key? key, - required this.onClick, - required this.child, - required this.onFocus, - }) : super(key: key); - - @override - Widget build(BuildContext context) { - /// Focus Node - var node = useFocusNode; - - /// On focus state - var isFocused = useState(false); - - return KeyboardListener( - focusNode: node.call(), - onKeyEvent: (KeyEvent event) { - /// Action label - var label = event.logicalKey.keyLabel; - - /// If label equal to Key Event which arrow up, down, right, left or on Enter - if (label == keyCenter) { - onClick(); - } else { - isFocused.value = !(isFocused.value); - onFocus(isFocused.value); - } - }, - child: child, - ); - } -} +import 'package:flutter/services.dart'; +import 'package:flutter/widgets.dart'; +import 'package:flutter_hooks/flutter_hooks.dart'; + +/// Action constants +const String keyUp = 'Arrow Up'; +const String keyDown = 'Arrow Down'; +const String keyLeft = 'Arrow Left'; +const String keyRight = 'Arrow Right'; +const String keyCenter = 'Select'; + +/// +/// Using Hooks instead of statefull builder +/// Make coding looks nice and compact +class DpadContainer extends HookWidget { + final Function onClick; + final Function(bool isFocused) onFocus; + final Widget child; + + const DpadContainer({ + Key? key, + required this.onClick, + required this.child, + required this.onFocus, + }) : super(key: key); + + @override + Widget build(BuildContext context) { + /// Focus Node + final FocusNode focusNode = useFocusNode(); + useEffect(() { + return focusNode.requestFocus; + }, []); + + /// On focus state + var isFocused = useState(false); + + return RawKeyboardListener( + focusNode: focusNode, + onKey: (RawKeyEvent event) { + if(event is RawKeyDownEvent) { + /// Action label + var label = event.logicalKey.keyLabel; + + /// If label equal to Key Event which arrow up, down, right, left or on Enter + if (label == keyCenter) { + onClick(); + } + } else { + isFocused.value = !isFocused.value; + onFocus(isFocused.value); + } + }, + child: child, + ); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index b756e1a..2a33925 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: dpad_container description: Dpad container focus navigation support for android TV. Allow you to navigate widgets and get focused, just wrap your widget and that's it! -version: 2.0.3 +version: 2.0.4 homepage: https://github.com/nicoaudy/dpad_container environment: