Skip to content

Commit

Permalink
fix on click event
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoaudy committed Jun 20, 2023
1 parent 38d8201 commit 5c69b5c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 53 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [2.0.4] - 20/06/2023

- Fix double on click event

## [2.0.3] - 03/02/2023

- Code formatting standard
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
108 changes: 57 additions & 51 deletions lib/dpad_container.dart
Original file line number Diff line number Diff line change
@@ -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,
);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 5c69b5c

Please sign in to comment.