tryDrawStyle method

({int colorArgb, double thickness, String tool})? tryDrawStyle()

Parses common draw style metadata from the action payload.

Expected schema: {tool: String, color: int, thickness: double}.

Implementation

({String tool, int colorArgb, double thickness})? tryDrawStyle() {
  final payload = this.payload;
  if (payload == null) return null;

  int? tryInt(Object? value) {
    if (value is int) return value;
    if (value is num) {
      final asInt = value.toInt();
      if (value == asInt) return asInt;
    }
    return null;
  }

  double? tryDouble(Object? value) {
    if (value is num) return value.toDouble();
    return null;
  }

  final tool = payload['tool'];
  if (tool is! String) return null;
  final colorArgb = tryInt(payload['color']);
  final thickness = tryDouble(payload['thickness']);
  if (colorArgb == null || thickness == null) return null;
  return (tool: tool, colorArgb: colorArgb, thickness: thickness);
}