FocusMoveEvent.fromKeyboardEvent constructor

FocusMoveEvent.fromKeyboardEvent(
  1. FocusableItem item,
  2. KeyboardEvent kbEvent
)

Builds a FocusMoveEvent instance from a keyboard event, iff the keycode is a next, previous, home or end key (i.e. up/down/left/right/home/end).

Implementation

factory FocusMoveEvent.fromKeyboardEvent(
    FocusableItem item, KeyboardEvent kbEvent) {
  int keyCode = kbEvent.keyCode;
  final preventDefaultFn = () {
    kbEvent.preventDefault();
  };
  if (_isHomeKey(keyCode)) {
    return FocusMoveEvent.homeKey(item, preventDefaultFn);
  }
  if (_isEndKey(keyCode)) {
    return FocusMoveEvent.endKey(item, preventDefaultFn);
  }
  if (!_isNextKey(keyCode) && !_isPrevKey(keyCode)) {
    //return null;
    return FocusMoveEvent.none(item, 0);
  }

  int offset = _isNextKey(keyCode) ? 1 : -1;
  if (keyCode == KeyCode.UP || keyCode == KeyCode.DOWN) {
    return FocusMoveEvent.upDownKey(item, offset, preventDefaultFn);
  }

  return FocusMoveEvent(item, offset, preventDefaultFn);
}