Skip to content

Canvas

A sub-cell drawing surface for custom plots, diagrams, and markers.

Import: package:fleury/fleury.dart, or package:fleury/fleury_core.dart in browser code.

// import 'dart:math' show sin;
class SineWavePainter extends CanvasPainter {
@override
void paint(CanvasContext ctx) {
const segments = 96;
for (var i = 0; i < segments; i++) {
final x1 = 6.28 * i / segments;
final x2 = 6.28 * (i + 1) / segments;
ctx.drawLine(x1, sin(x1), x2, sin(x2));
}
}
}
// In build:
Canvas(
painter: SineWavePainter(),
bounds: const CanvasBounds(minX: 0, maxX: 6.28, minY: -1, maxY: 1),
semanticRole: SemanticRole.chart,
semanticLabel: 'Sine wave',
)

Subclass CanvasPainter and draw with CanvasContext.drawLine and CanvasContext.drawDot in the logical coordinate space described by bounds. marker selects how those points map onto terminal cells; the braille default packs a 2×4 grid of dots into each cell, and LineChart is built on the same primitive. Provide semantic metadata when the drawing communicates information that is not otherwise represented by an enclosing widget.

ParameterTypeDefaultDescription
painter:CanvasPainterrequiredDrawing routine invoked with logical bounds during canvas painting. More
bounds:CanvasBounds?—Logical coordinate range. Defaults to CanvasBounds.unit.
marker:CanvasMarkerCanvasMarker.brailleSub-cell rendering style. See CanvasMarker for the tradeoffs.
semanticRole:SemanticRoleSemanticRole.imageSemantic role used when this canvas opts into semantics. More
semanticLabel:String?—Optional label that exposes the canvas to the semantic app graph. More
semanticValue:Object?—Optional semantic value for the drawn content.
semanticHint:String?—Optional semantic hint for the drawn content.
semanticState:SemanticStateSemanticState.emptyAdditional semantic state for custom canvas surfaces.
  • painter: The canvas repaints whenever it is rebuilt, even with the same painter instance, so a painter can read data that changes between builds.
  • semanticRole: Defaults to SemanticRole.image. Custom plots can use SemanticRole.chart and provide chart-specific semanticState.
  • semanticLabel: Plain canvases do not contribute semantics because higher-level widgets such as charts wrap their drawing surface with richer meaning.

Canvas is defined in packages/fleury/lib/src/catalog/canvas.dart.

Category: Charts & meters · All widgets