Callback identity is too fragile for CustomPaint
Many of the widgets that use CustomPaint were spamming repaints because CustomPaint repaints when the identity of the onPaint callback changes, which it does every build for StatelessComponents. This patch changes CustomPaint to use a CustomPainter, similar to the new custom layout widgets. The CustomPainter has a `shouldRepaint` function along with its `paint` function. This function gives clients explicit control over when the custom paint object repaints.
This commit is contained in:
@@ -6,6 +6,35 @@ import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
class _BaselinePainter extends CustomPainter {
|
||||
const _BaselinePainter({
|
||||
this.paragraph
|
||||
});
|
||||
|
||||
final RenderParagraph paragraph;
|
||||
|
||||
void paint(Canvas canvas, Size size) {
|
||||
double baseline = paragraph.getDistanceToBaseline(TextBaseline.alphabetic);
|
||||
double w = paragraph.getMaxIntrinsicWidth(new BoxConstraints.loose(size));
|
||||
double h = paragraph.getMaxIntrinsicHeight(new BoxConstraints.loose(size));
|
||||
Path path = new Path();
|
||||
path.moveTo(0.0, 0.0);
|
||||
path.lineTo(w, 0.0);
|
||||
path.moveTo(0.0, baseline);
|
||||
path.lineTo(w, baseline);
|
||||
path.moveTo(0.0, h);
|
||||
path.lineTo(w, h);
|
||||
Paint paint = new Paint()
|
||||
..color = const Color(0xFFFF9000)
|
||||
..style = ui.PaintingStyle.stroke
|
||||
..strokeWidth = 3.0;
|
||||
canvas.drawPath(path, paint);
|
||||
}
|
||||
|
||||
// TODO(abarth): We have no good way of detecting when the paragraph's intrinsic dimensions change.
|
||||
bool shouldRepaint(_BaselinePainter oldPainter) => true;
|
||||
}
|
||||
|
||||
RenderBox getBox(double lh) {
|
||||
RenderParagraph paragraph = new RenderParagraph(
|
||||
new StyledTextSpan(
|
||||
@@ -36,24 +65,10 @@ RenderBox getBox(double lh) {
|
||||
child: new RenderPadding(
|
||||
padding: new EdgeDims.all(10.0),
|
||||
child: new RenderCustomPaint(
|
||||
child: paragraph,
|
||||
onPaint: (PaintingCanvas canvas, Size size) {
|
||||
double baseline = paragraph.getDistanceToBaseline(TextBaseline.alphabetic);
|
||||
double w = paragraph.getMaxIntrinsicWidth(new BoxConstraints.loose(size));
|
||||
double h = paragraph.getMaxIntrinsicHeight(new BoxConstraints.loose(size));
|
||||
Path path = new Path();
|
||||
path.moveTo(0.0, 0.0);
|
||||
path.lineTo(w, 0.0);
|
||||
path.moveTo(0.0, baseline);
|
||||
path.lineTo(w, baseline);
|
||||
path.moveTo(0.0, h);
|
||||
path.lineTo(w, h);
|
||||
Paint paint = new Paint()
|
||||
..color = const Color(0xFFFF9000)
|
||||
..style = ui.PaintingStyle.stroke
|
||||
..strokeWidth = 3.0;
|
||||
canvas.drawPath(path, paint);
|
||||
}
|
||||
painter: new _BaselinePainter(
|
||||
paragraph: paragraph
|
||||
),
|
||||
child: paragraph
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -4,6 +4,50 @@
|
||||
|
||||
part of stocks;
|
||||
|
||||
class StockArrowPainter extends CustomPainter {
|
||||
StockArrowPainter({ this.color, this.percentChange });
|
||||
|
||||
final Color color;
|
||||
final double percentChange;
|
||||
|
||||
void paint(PaintingCanvas canvas, Size size) {
|
||||
Paint paint = new Paint()..color = color;
|
||||
paint.strokeWidth = 1.0;
|
||||
const double padding = 2.0;
|
||||
assert(padding > paint.strokeWidth / 2.0); // make sure the circle remains inside the box
|
||||
double r = (size - padding) / 2.0; // radius of the circle
|
||||
double centerX = padding + r;
|
||||
double centerY = padding + r;
|
||||
|
||||
// Draw the arrow.
|
||||
double w = 8.0;
|
||||
double h = 5.0;
|
||||
double arrowY;
|
||||
if (percentChange < 0.0) {
|
||||
h = -h;
|
||||
arrowY = centerX + 1.0;
|
||||
} else {
|
||||
arrowY = centerX - 1.0;
|
||||
}
|
||||
Path path = new Path();
|
||||
path.moveTo(centerX, arrowY - h); // top of the arrow
|
||||
path.lineTo(centerX + w, arrowY + h);
|
||||
path.lineTo(centerX - w, arrowY + h);
|
||||
path.close();
|
||||
paint.style = ui.PaintingStyle.fill;
|
||||
canvas.drawPath(path, paint);
|
||||
|
||||
// Draw a circle that circumscribes the arrow.
|
||||
paint.style = ui.PaintingStyle.stroke;
|
||||
canvas.drawCircle(new Point(centerX, centerY), r, paint);
|
||||
}
|
||||
|
||||
bool shouldRepaint(StockArrowPainter oldPainter) {
|
||||
return oldPainter.color != color
|
||||
|| oldPainter.percentChange != percentChange;
|
||||
}
|
||||
}
|
||||
|
||||
class StockArrow extends StatelessComponent {
|
||||
StockArrow({ Key key, this.percentChange }) : super(key: key);
|
||||
|
||||
@@ -22,46 +66,17 @@ class StockArrow extends StatelessComponent {
|
||||
}
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
// TODO(jackson): This should change colors with the theme
|
||||
Color color = _colorForPercentChange(percentChange);
|
||||
const double kSize = 40.0;
|
||||
var arrow = new CustomPaint(onPaint: (ui.Canvas canvas, Size size) {
|
||||
Paint paint = new Paint()..color = color;
|
||||
paint.strokeWidth = 1.0;
|
||||
const double padding = 2.0;
|
||||
assert(padding > paint.strokeWidth / 2.0); // make sure the circle remains inside the box
|
||||
double r = (kSize - padding) / 2.0; // radius of the circle
|
||||
double centerX = padding + r;
|
||||
double centerY = padding + r;
|
||||
|
||||
// Draw the arrow.
|
||||
double w = 8.0;
|
||||
double h = 5.0;
|
||||
double arrowY;
|
||||
if (percentChange < 0.0) {
|
||||
h = -h;
|
||||
arrowY = centerX + 1.0;
|
||||
} else {
|
||||
arrowY = centerX - 1.0;
|
||||
}
|
||||
Path path = new Path();
|
||||
path.moveTo(centerX, arrowY - h); // top of the arrow
|
||||
path.lineTo(centerX + w, arrowY + h);
|
||||
path.lineTo(centerX - w, arrowY + h);
|
||||
path.close();
|
||||
paint.style = ui.PaintingStyle.fill;
|
||||
canvas.drawPath(path, paint);
|
||||
|
||||
// Draw a circle that circumscribes the arrow.
|
||||
paint.style = ui.PaintingStyle.stroke;
|
||||
canvas.drawCircle(new Point(centerX, centerY), r, paint);
|
||||
});
|
||||
|
||||
return new Container(
|
||||
child: arrow,
|
||||
width: kSize,
|
||||
height: kSize,
|
||||
margin: const EdgeDims.symmetric(horizontal: 5.0)
|
||||
width: 40.0,
|
||||
height: 40.0,
|
||||
margin: const EdgeDims.symmetric(horizontal: 5.0),
|
||||
child: new CustomPaint(
|
||||
painter: new StockArrowPainter(
|
||||
// TODO(jackson): This should change colors with the theme
|
||||
color: _colorForPercentChange(percentChange),
|
||||
percentChange: percentChange
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,42 +9,49 @@ class ScaleApp extends StatefulComponent {
|
||||
ScaleAppState createState() => new ScaleAppState();
|
||||
}
|
||||
|
||||
class GesturesDemoPaintToken {
|
||||
GesturesDemoPaintToken(this.zoom, this.offset, this.swatch, this.forward, this.flag1, this.flag2, this.flag3, this.flag4);
|
||||
final Offset offset;
|
||||
class _GesturePainter extends CustomPainter {
|
||||
const _GesturePainter({
|
||||
this.zoom,
|
||||
this.offset,
|
||||
this.swatch,
|
||||
this.forward,
|
||||
this.scaleEnabled,
|
||||
this.tapEnabled,
|
||||
this.doubleTapEnabled,
|
||||
this.longPressEnabled
|
||||
});
|
||||
|
||||
final double zoom;
|
||||
final Offset offset;
|
||||
final Map<int, Color> swatch;
|
||||
final bool forward;
|
||||
final bool flag1;
|
||||
final bool flag2;
|
||||
final bool flag3;
|
||||
final bool flag4;
|
||||
bool operator ==(dynamic other) {
|
||||
if (identical(this, other))
|
||||
return true;
|
||||
if (other is! GesturesDemoPaintToken)
|
||||
return false;
|
||||
final GesturesDemoPaintToken typedOther = other;
|
||||
return offset == typedOther.offset &&
|
||||
zoom == typedOther.zoom &&
|
||||
identical(swatch, typedOther.swatch) &&
|
||||
forward == typedOther.forward &&
|
||||
flag1 == typedOther.flag1 &&
|
||||
flag2 == typedOther.flag2 &&
|
||||
flag3 == typedOther.flag3 &&
|
||||
flag4 == typedOther.flag4;
|
||||
final bool scaleEnabled;
|
||||
final bool tapEnabled;
|
||||
final bool doubleTapEnabled;
|
||||
final bool longPressEnabled;
|
||||
|
||||
void paint(PaintingCanvas canvas, Size size) {
|
||||
Point center = (size.center(Point.origin).toOffset() * zoom + offset).toPoint();
|
||||
double radius = size.width / 2.0 * zoom;
|
||||
Gradient gradient = new RadialGradient(
|
||||
center: center, radius: radius,
|
||||
colors: forward ? <Color>[swatch[50], swatch[900]]
|
||||
: <Color>[swatch[900], swatch[50]]
|
||||
);
|
||||
Paint paint = new Paint()
|
||||
..shader = gradient.createShader();
|
||||
canvas.drawCircle(center, radius, paint);
|
||||
}
|
||||
int get hashCode {
|
||||
int value = 373;
|
||||
value = 37 * value + offset.hashCode;
|
||||
value = 37 * value + zoom.hashCode;
|
||||
value = 37 * value + identityHashCode(swatch);
|
||||
value = 37 * value + forward.hashCode;
|
||||
value = 37 * value + flag1.hashCode;
|
||||
value = 37 * value + flag2.hashCode;
|
||||
value = 37 * value + flag3.hashCode;
|
||||
value = 37 * value + flag4.hashCode;
|
||||
return value;
|
||||
|
||||
bool shouldRepaint(_GesturePainter oldPainter) {
|
||||
return oldPainter.zoom != zoom
|
||||
|| oldPainter.offset != offset
|
||||
|| oldPainter.swatch != swatch
|
||||
|| oldPainter.forward != forward
|
||||
|| oldPainter.scaleEnabled != scaleEnabled
|
||||
|| oldPainter.tapEnabled != tapEnabled
|
||||
|| oldPainter.doubleTapEnabled != doubleTapEnabled
|
||||
|| oldPainter.longPressEnabled != longPressEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,18 +131,6 @@ class ScaleAppState extends State<ScaleApp> {
|
||||
});
|
||||
}
|
||||
|
||||
void paint(PaintingCanvas canvas, Size size) {
|
||||
Point center = (size.center(Point.origin).toOffset() * _zoom + _offset).toPoint();
|
||||
double radius = size.width / 2.0 * _zoom;
|
||||
Gradient gradient = new RadialGradient(
|
||||
center: center, radius: radius,
|
||||
colors: _forward ? <Color>[_swatch[50], _swatch[900]]
|
||||
: <Color>[_swatch[900], _swatch[50]]
|
||||
);
|
||||
Paint paint = new Paint()
|
||||
..shader = gradient.createShader();
|
||||
canvas.drawCircle(center, radius, paint);
|
||||
}
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return new Theme(
|
||||
@@ -151,10 +146,16 @@ class ScaleAppState extends State<ScaleApp> {
|
||||
onDoubleTap: _doubleTapEnabled ? _handleScaleReset : null,
|
||||
onLongPress: _longPressEnabled ? _handleDirectionChange : null,
|
||||
child: new CustomPaint(
|
||||
onPaint: paint,
|
||||
token: new GesturesDemoPaintToken(_zoom, _offset, _swatch, _forward,
|
||||
_scaleEnabled, _tapEnabled, _doubleTapEnabled,
|
||||
_longPressEnabled)
|
||||
painter: new _GesturePainter(
|
||||
zoom: _zoom,
|
||||
offset: _offset,
|
||||
swatch: _swatch,
|
||||
forward: _forward,
|
||||
scaleEnabled: _scaleEnabled,
|
||||
tapEnabled: _tapEnabled,
|
||||
doubleTapEnabled: _doubleTapEnabled,
|
||||
longPressEnabled: _longPressEnabled
|
||||
)
|
||||
)
|
||||
),
|
||||
new Positioned(
|
||||
|
||||
@@ -19,19 +19,16 @@ class CardModel {
|
||||
|
||||
enum MarkerType { topLeft, bottomRight, touch }
|
||||
|
||||
class Marker extends StatelessComponent {
|
||||
Marker({
|
||||
this.type: MarkerType.touch,
|
||||
this.position,
|
||||
this.size: 40.0,
|
||||
Key key
|
||||
}) : super(key: key);
|
||||
class _MarkerPainter extends CustomPainter {
|
||||
const _MarkerPainter({
|
||||
this.size,
|
||||
this.type
|
||||
});
|
||||
|
||||
final Point position;
|
||||
final double size;
|
||||
final MarkerType type;
|
||||
|
||||
void paintMarker(PaintingCanvas canvas, _) {
|
||||
void paint(PaintingCanvas canvas, _) {
|
||||
Paint paint = new Paint()..color = const Color(0x8000FF00);
|
||||
double r = size / 2.0;
|
||||
canvas.drawCircle(new Point(r, r), r, paint);
|
||||
@@ -50,6 +47,24 @@ class Marker extends StatelessComponent {
|
||||
}
|
||||
}
|
||||
|
||||
bool shouldRepaint(_MarkerPainter oldPainter) {
|
||||
return oldPainter.size != size
|
||||
|| oldPainter.type != type;
|
||||
}
|
||||
}
|
||||
|
||||
class Marker extends StatelessComponent {
|
||||
Marker({
|
||||
this.type: MarkerType.touch,
|
||||
this.position,
|
||||
this.size: 40.0,
|
||||
Key key
|
||||
}) : super(key: key);
|
||||
|
||||
final Point position;
|
||||
final double size;
|
||||
final MarkerType type;
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return new Positioned(
|
||||
left: position.x - size / 2.0,
|
||||
@@ -57,7 +72,12 @@ class Marker extends StatelessComponent {
|
||||
width: size,
|
||||
height: size,
|
||||
child: new IgnorePointer(
|
||||
child: new CustomPaint(onPaint: paintMarker)
|
||||
child: new CustomPaint(
|
||||
painter: new _MarkerPainter(
|
||||
size: size,
|
||||
type: type
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user