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:
Adam Barth
2015-11-20 00:22:49 -08:00
parent ea6292e1a8
commit 72329cf422
15 changed files with 461 additions and 264 deletions

View File

@@ -13,12 +13,12 @@ void main() {
RenderBox root = new RenderViewport(
child: new RenderOffStage(
child: new RenderCustomPaint(
painter: new TestCallbackPainter(
onPaint: () { painted = true; }
),
child: child = new RenderConstrainedBox(
additionalConstraints: new BoxConstraints.tightFor(height: 10.0, width: 10.0)
),
onPaint: (PaintingCanvas canvas, Size size) {
painted = true;
}
)
)
)
);

View File

@@ -12,10 +12,12 @@ void main() {
root = new RenderPositionedBox(
child: new RenderCustomPaint(
child: child = text = new RenderParagraph(new PlainTextSpan('Hello World')),
onPaint: (PaintingCanvas canvas, Size size) {
baseline1 = child.getDistanceToBaseline(TextBaseline.alphabetic);
height1 = text.size.height;
}
painter: new TestCallbackPainter(
onPaint: () {
baseline1 = child.getDistanceToBaseline(TextBaseline.alphabetic);
height1 = text.size.height;
}
)
)
);
layout(root, phase: EnginePhase.paint);
@@ -26,10 +28,12 @@ void main() {
child: text = new RenderParagraph(new PlainTextSpan('Hello World')),
maxHeight: height1 / 2.0
),
onPaint: (PaintingCanvas canvas, Size size) {
baseline2 = child.getDistanceToBaseline(TextBaseline.alphabetic);
height2 = text.size.height;
}
painter: new TestCallbackPainter(
onPaint: () {
baseline2 = child.getDistanceToBaseline(TextBaseline.alphabetic);
height2 = text.size.height;
}
)
)
);
layout(root, phase: EnginePhase.paint);

View File

@@ -53,3 +53,15 @@ void pumpFrame({ EnginePhase phase: EnginePhase.layout }) {
return;
renderView.compositeFrame();
}
class TestCallbackPainter extends CustomPainter {
const TestCallbackPainter({ this.onPaint });
final VoidCallback onPaint;
void paint(Canvas canvas, Size size) {
onPaint();
}
bool shouldRepaint(TestCallbackPainter oldPainter) => true;
}

View File

@@ -7,6 +7,7 @@ import 'package:flutter/widgets.dart';
import 'package:test/test.dart';
import 'widget_tester.dart';
import '../rendering/rendering_tester.dart';
void main() {
test('Can construct an empty Stack', () {
@@ -150,7 +151,12 @@ void main() {
Widget buildFrame(int index) {
itemsPainted = <int>[];
List<Widget> items = new List<Widget>.generate(itemCount, (i) {
return new CustomPaint(child: new Text('$i'), onPaint: (_, __) { itemsPainted.add(i); });
return new CustomPaint(
child: new Text('$i'),
painter: new TestCallbackPainter(
onPaint: () { itemsPainted.add(i); }
)
);
});
return new Center(child: new IndexedStack(items, index: index));
}