paint method
Called whenever the object needs to paint. The given Canvas has its
coordinate space configured such that the origin is at the top left of the
box. The area of the box is the size of the size argument.
Paint operations should remain inside the given area. Graphical operations outside the bounds may be silently ignored, clipped, or not clipped. It may sometimes be difficult to guarantee that a certain operation is inside the bounds (e.g., drawing a rectangle whose size is determined by user inputs). In that case, consider calling Canvas.clipRect at the beginning of paint so everything that follows will be guaranteed to only draw within the clipped area.
Implementations should be wary of correctly pairing any calls to Canvas.save/Canvas.saveLayer and Canvas.restore, otherwise all subsequent painting on this canvas may be affected, with potentially hilarious but confusing results.
To paint text on a Canvas, use a TextPainter.
To paint an image on a Canvas:
-
Obtain an ImageStream, for example by calling ImageProvider.resolve on an AssetImage or NetworkImage object.
-
Whenever the ImageStream's underlying ImageInfo object changes (see ImageStream.addListener), create a new instance of your custom paint delegate, giving it the new ImageInfo object.
-
In your delegate's paint method, call the Canvas.drawImage, Canvas.drawImageRect, or Canvas.drawImageNine methods to paint the ImageInfo.image object, applying the ImageInfo.scale value to obtain the correct rendering size.
Implementation
@override
void paint(Canvas canvas, Size size) {
canvas.drawRect(Offset.zero & size, Paint()..color = _ground);
// The one line that says where the app stops. A slab the same value as a
// white app has no edge at all, and the app's own bottom row then looks
// like the keyboard's top row.
canvas.drawRect(
Rect.fromLTWH(0, 0, size.width, 0.5),
Paint()..color = const Color(0x22000000),
);
// The bands, top to bottom. The suggestion strip is inside the measured
// number rather than beside it, and so is the home indicator, which is
// why the rows stop short of the bottom.
//
// **A keypad has no strip**, and that is not a drawing decision: a digit
// pad predicts nothing, which is most of why it is the shorter keyboard
// in the first place.
var strip = variant == KeyboardVariant.keypad ? 0.0 : size.height * 0.14;
var foot = size.height * (_android ? 0.05 : 0.10);
var rowsHeight = size.height - strip - foot;
if (rowsHeight <= 0) return;
if (strip > 0) {
_paintStrip(canvas, Rect.fromLTWH(0, 0, size.width, strip));
}
var gap = math.max(1.0, rowsHeight * 0.055);
var rowHeight = (rowsHeight - gap * 4) / 4;
if (rowHeight <= 0) return;
var margin = size.width * 0.012;
var radius = Radius.circular(_android ? rowHeight * 0.16 : rowHeight * 0.2);
double rowTop(int index) => strip + gap * (index + 1) + rowHeight * index;
if (variant == KeyboardVariant.keypad) {
_paintKeypad(canvas, size, rowTop, rowHeight, margin, radius, gap);
return;
}
// 10, 9 inset by half a key, then shift + 7 + backspace. The alphabet
// every Latin layout is some permutation of.
_paintKeys(canvas, size, rowTop(0), rowHeight, margin, radius, 10, gap);
_paintKeys(
canvas,
size,
rowTop(1),
rowHeight,
margin + (size.width - margin * 2) / 20,
radius,
9,
gap,
);
_paintWeighted(
canvas,
size,
rowTop(2),
rowHeight,
margin,
radius,
gap,
const [
(1.5, true, null),
(1, false, null),
(1, false, null),
(1, false, null),
(1, false, null),
(1, false, null),
(1, false, null),
(1, false, null),
(1.5, true, null),
],
);
// The bottom row is the whole of what a reader can see the *type* in. A
// real email keyboard grows an `@` and a dot here, a URL keyboard a slash
// and a `.com`, and both take the width out of the space bar — which is
// exactly what makes them recognisable at a glance without a single glyph
// being drawn.
_paintWeighted(
canvas,
size,
rowTop(3),
rowHeight,
margin,
radius,
gap,
_bottomRow,
);
}