Skip to content

Library for creating and managing a canvas similar to CustomPaint but with more features.

License

Notifications You must be signed in to change notification settings

PlugFox/repaint

Repository files navigation

RePaint

Pub Actions Status Coverage License: MIT Linter GitHub stars

Library for creating and managing a canvas similar to CustomPaint but with more features.

How to

Handle mouse events

@override
@mustCallSuper
void onPointerEvent(PointerEvent event) {
  case (event) {
    case PointerDownEvent e:
    case PointerUpEvent e:
    case PointerCancelEvent e:
    case PointerPanZoomStartEvent e:
    case PointerPanZoomUpdateEvent e:
    case PointerPanZoomEndEvent e:
    case PointerScrollEvent e:
    case PointerSignalEvent e:
    case PointerHoverEvent e:
      break;
    case PointerMoveEvent e:
      // Move the [_rect] by the [_offset] on mouse drag
      final rect = _rect.shift(_offset);
      if (!rect.contains(e.localPosition)) return;
      _offset += move.delta;
  }
}

Handle keyboard events

To handle keyboard events you can use HardwareKeyboard manager:

bool _onKeyEvent(KeyEvent event) {
  if (event.deviceType != KeyEventDeviceType.keyboard) return false;
  if (event is! KeyDownEvent) return false;
  // F1 - do something
  switch (event.logicalKey) {
    case LogicalKeyboardKey.f1:
      doSomething();
      return true;
    default:
      return false;
  }
}

@override
void mount(_, __) {
  HardwareKeyboard.instance.addHandler(_onKeyEvent);
}

@override
void unmount() {
  HardwareKeyboard.instance.removeHandler(_onKeyEvent);
}

or wrap RePaint with Focus widget and use onKeyEvent to handle keyboard events:

final painter = RePainterImpl();

Widget build(BuildContext context) {
  return Focus(
    onKeyEvent: painter.onKeyEvent,
    child: RePaint(
      painter: painter,
    ),
  );
}