Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable navigation using the keyboard #9

Closed
bdlukaa opened this issue Apr 4, 2021 · 1 comment
Closed

Enable navigation using the keyboard #9

bdlukaa opened this issue Apr 4, 2021 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@bdlukaa
Copy link
Owner

bdlukaa commented Apr 4, 2021

Currently, we can only navigate using the mouse. This can be achieved using the FocusManager class:

...state with WidgetsBindingObserver

  bool _hasPrimaryFocus = false;
  late FocusHighlightMode _focusHighlightMode;

  void _handleFocusHighlightModeChange(FocusHighlightMode mode) {
    if (!mounted) {
      return;
    }
    setState(() {
      _focusHighlightMode = mode;
    });
  }

  void _handleFocusChanged() {
    if (_hasPrimaryFocus != focusNode!.hasPrimaryFocus) {
      setState(() {
        _hasPrimaryFocus = focusNode!.hasPrimaryFocus;
      });
    }
  }

  @override
  void initState() {
    super.initState();
    focusNode!.addListener(_handleFocusChanged);
    final FocusManager focusManager = WidgetsBinding.instance!.focusManager;
    _focusHighlightMode = focusManager.highlightMode;
    focusManager.addHighlightModeListener(_handleFocusHighlightModeChange);
  }

  @override
  void dispose() {
    WidgetsBinding.instance!.removeObserver(this);
    WidgetsBinding.instance!.focusManager.removeHighlightModeListener(_handleFocusHighlightModeChange);
    focusNode!.removeListener(_handleFocusChanged);
    super.dispose();
  }

  bool get _showHighlight {
    switch (_focusHighlightMode) {
      case FocusHighlightMode.touch:
        return false;
      case FocusHighlightMode.traditional:
        return _hasPrimaryFocus;
    }
  }
This code is from flutter/dropdown

If _showHighlight is true, we should change the widget decoration.

@bdlukaa bdlukaa added enhancement New feature or request help wanted Extra attention is needed labels Apr 4, 2021
@bdlukaa
Copy link
Owner Author

bdlukaa commented Apr 7, 2021

This was implemented in 1.7.1 using the widget FocusableActionDetector. All the inputs can now be focused using the keyboard, and be pressed using enter and space. The widget NavigationPaneBody was improved so that focus doesn't feel weird

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant