Skip to content

Commit

Permalink
feat: allow passing a focus node
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasgangso committed Dec 21, 2023
1 parent 9e9bfad commit ead5b9f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/focusable_control_builder.dart
Expand Up @@ -21,6 +21,7 @@ class FocusableControlBuilder extends StatefulWidget {
this.autoFocus = false,
this.descendantsAreFocusable = true,
this.descendantsAreTraversable = true,
this.focusNode,
}) : super(key: key);

/// Return a widget representing the control based on the current [FocusableControlState]
Expand Down Expand Up @@ -68,6 +69,9 @@ class FocusableControlBuilder extends StatefulWidget {
/// Passed along to the [FocusableActionDetector]
final bool descendantsAreTraversable;

/// Optional. Passed along to the [FocusableActionDetector]. Otherwise, a new [FocusNode] will be created and managed internally.
final FocusNode? focusNode;

@override
State<FocusableControlBuilder> createState() => FocusableControlState();
}
Expand All @@ -76,7 +80,7 @@ class FocusableControlBuilder extends StatefulWidget {
/// This is passed to the builder function, and can be used to determine the current state of the control.
class FocusableControlState extends State<FocusableControlBuilder> {
final FocusNode _focusNode = FocusNode();
FocusNode get focusNode => _focusNode;
FocusNode get focusNode => widget.focusNode ?? _focusNode;

bool _isHovered = false;
bool get isHovered => _isHovered;
Expand Down Expand Up @@ -113,7 +117,7 @@ class FocusableControlState extends State<FocusableControlBuilder> {

void _handlePressed() {
if (widget.requestFocusOnPress) {
_focusNode.requestFocus();
focusNode.requestFocus();
}
widget.onPressed?.call();
}
Expand All @@ -138,7 +142,7 @@ class FocusableControlState extends State<FocusableControlBuilder> {
// Create the core FocusableActionDetector
Widget content = FocusableActionDetector(
enabled: widget.enabled,
focusNode: _focusNode,
focusNode: focusNode,
autofocus: widget.autoFocus,
descendantsAreFocusable: widget.descendantsAreFocusable,
descendantsAreTraversable: widget.descendantsAreTraversable,
Expand Down

0 comments on commit ead5b9f

Please sign in to comment.