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:
@@ -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;
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user