zakariaf/flutter-skills/custom-canvas-and-gestures
Claude Code and agent skill by zakariaf/flutter-skills
- community
Checking that the SKILL.md file is still in the repository.
Enforces CustomPainter/Canvas discipline — the View/Painter/Scene split with a dumb painter fed one immutable Scene value type, shouldRepaint as a single value compare kept strictly separate from the AnimationController-as-repaint animation path, one shared affine transform read by BOTH painter and hit-tester (toCanvas/toLogical exact inverses, never re-derive scale), geometry hit-testing (integer lattice or rasterized region-ID buffer, never Path.contains), zero-allocation paint(), gesture-as-pure-translator emitting a typed command to a Notifier (never mutating in the handler), ExcludeSemantics + sibling Semantics speaking display values with redundant non-colour encoding, measured TextPainter fitting, first-party RoundedSuperellipseBorder, physical-pixel hairlines, and Directional-only geometry. Use when writing or reviewing a CustomPainter/CustomPaint, gestures on a canvas, tap/drag hit-testing, canvas animation, measured text fitting, or Semantics over custom-drawn pixels.
Install
npx vskill@latest install zakariaf/flutter-skills/custom-canvas-and-gesturesSKILL.md
The same SKILL.md was published earlier as zakariaf/flutter-skills/custom-canvas-and-gestures.
Custom Canvas & Gestures
Hand-painted surfaces (CustomPainter/Canvas) own every pixel, so they look byte-identical across platforms — but only if the painter stays dumb, the coordinate math is shared, and gestures translate rather than decide. This skill is the contract for that layer: how it splits, how pixels and pointers agree, and how a screen reader hears a canvas.
Read the reference for the task at hand:
references/painter-and-scene.md— the View/Painter/Scene split, the shared transform, hit-testing by geometry class, zero-allocationpaint(), the two-path repaint pitfall.references/gestures-and-semantics.md— gesture-as-translator, arena arbitration, clamp-not-collision drags, custom-canvas Semantics, action/gesture parity.references/text-and-shapes.md— measuredTextPainterfitting, optical centring,RoundedSuperellipseBorder, physical-pixel hairlines, concentric radii, painter-over-image-asset.
Run scripts/check_painter_hygiene.sh and scripts/analyze.sh before a PR.
Non-negotiable rules
- A canvas surface is three collaborators — a View, a Painter, and an immutable Scene — never one god-widget. The View watches the ViewModel, builds an immutable
Scenevalue type holding everything the painter needs and nothing more, and hands it toCustomPaint. ThePainter extends CustomPainteris dumb: noNotifier, noBuildContext, noDateTime, no domain rules — it draws the Scene and never decides state. A dumb painter is testable and re-skinnable.
- `shouldRepaint` is one value compare — `old.scene != scene` — and nothing else. Because
Scenehas value equality, this is both correct and cheap.=> truerepaints every frame; a wrongfalsefreezes the surface — both fail silently. Config-driven repaints go throughshouldRepaint; per-frame animation goes through therepaint:Listenable. The two paths must never double-repaint.
- Exactly one affine transform maps logical space ↔ canvas pixels, read by BOTH the painter and the hit-tester. Build it once per layout from the incoming
Size; exposetoCanvas/toLogicalas exact inverses. The single most corrosive bug is a painter and hit-tester that disagree by a few pixels; a shared, tested transform forecloses it. The hit-tester never re-derives scale from `size` — it callstransform.toLogical.
- Hit-test by geometry class — integers for a lattice, a rasterized ID buffer for irregular regions — never `Path.contains` in the hot path. A grid cell is
(x ~/ cell, y ~/ cell), one division per axis. An irregular region is resolved by indexing acell → RegionIdUint32Listrasterized once at load. O(1) and concavity-proof; testing a tap against N concave polygons every touch is not.
- Allocate nothing inside `paint()`. Precompute every
Paint,Path, andGradientas painter fields and mutate.colorper element rather than constructing.Paint()/Path()insidepaint()is the classic jank source; reservesaveLayerfor a real group opacity/blend, never a plain fill.
- Every animation is driven by an `AnimationController` passed as the painter's `repaint:` `Listenable` — never `setState`/`notifyListeners` in a ticker. That repaints the painter directly without rebuilding the widget tree. Resolve the animation's duration through
design-system-structure's reduced-motion token /resolveMotionhelper (it owns that rule — collapse to zero, not gentler); when motion is suppressed, cut straight to the end state rather than tweening. A state change must never rely on motion or hue as its only signal — carry a redundant non-colour channel (shape + label + haptic) peraccessibility-as-code.
- A gesture handler is a pure translator: pointer → `localPosition` → shared transform → hit-test → typed immutable command → a ViewModel method. It mutates nothing. Read
TapDownDetails.localPosition/DragUpdateDetails.localPosition(neverglobalPosition), map throughtransform.toLogical, and callref.read(vmProvider.notifier).<command>(...). Wrap theCustomPaintinHitTestBehavior.opaqueso the whole rect is live over transparent gaps.
- One verb, one recognizer; arbitrate collisions explicitly in the arena. The primary verb owns a single recognizer; secondary verbs get separate recognizers. Where two can claim a pointer, resolve it via
RawGestureDetector— do not hope. An axis-locked drag is a clamp (bounds snapshot at drag-start), not per-frame collision detection.
- Canonical in, display out — never convert units, format dates, or shape numerals inside `paint()`. The painter receives values already converted to display units and formatted upstream; it maps numbers to pixels. Bucket/downsample large histories off-isolate via
Isolate.run, keyed off a revision counter — never loop thousands of points inpaint().
- A custom-drawn surface is opaque to screen readers — author Semantics explicitly. Either wrap the
CustomPaintinExcludeSemanticswith a siblingSemanticsnode that speaks the display value (the answer, not the shape), or returnList<CustomPainterSemantics>fromsemanticsBuilderfor per-element nodes. Colour is never the only channel — the never-colour-alone rule is owned byaccessibility-as-code. The a11y action commits the same command the gesture does.
- Geometry is direction-agnostic; only chrome mirrors. Plotted data, a physical drag, a waveform have no handedness — never flip them for RTL. Legends, tooltips, axis placement, and label lead-edge mirror. Derive any sign from
Directionality.of(context)and use*Directionalinsets — never a hard-codedOffset(-x, y),.left, or.right.
- Isolate the surface behind a `RepaintBoundary`. It gets its own compositor layer so a canvas tick does not repaint the surrounding chrome and vice versa. Set
isComplex/willChangehonestly. Do not blanket-wrap everything — each boundary costs GPU memory.
View / Painter / Scene
The Scene is the painter's entire input, an immutable value type (@freezed or a hand-rolled @immutable with ==/hashCode). Value equality is what makes shouldRepaint a cheap compare.
The View watches the ViewModel (a Riverpod Notifier/AsyncNotifier), projects its state into the Scene, and never mutates state from inside the painter. See state-management-riverpod for the ViewModel spine.
The shared transform
One uniform scale + centering origin; toLogical is the exact inverse of toCanvas. Both the painter and the hit-tester read it.
Hit-testing by geometry class
Build the buffer once by drawing each region in a unique id-colour into a PictureRecorder → Picture.toImage → Image.toByteData, read into a Uint32List, and cache it. Rebuild only on a genuine geometry change. Full recipe in references/painter-and-scene.md.
Zero-allocation paint()
Gesture → typed command
Semantics over a canvas
For per-element nodes (each tappable), return List<CustomPainterSemantics> from semanticsBuilder and override shouldRebuildSemantics — see references/gestures-and-semantics.md. The a11y onTap must call the same command the gesture does.
Measured text fitting
Scale text to a measured width with a linear TextPainter probe. Layout unconstrained — a constrained layout() wraps and tp.width then reports the constraint, silently returning the same size for every line.
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/custom-canvas-and-gestures/SKILL.md
Popularity
- Repository stars
- 22
- Repository forks
- 4
- 7-day trend score
- 16
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 14 Oct 2026, a month ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/zakariaf/flutter-skills/custom-canvas-and-gestures)