flutter/packages/unit/test/rendering/rendering_tester.dart
Adam Barth 72329cf422 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.
2015-11-20 08:20:59 -08:00

68 lines
1.5 KiB
Dart

import 'package:flutter/rendering.dart';
const Size _kTestViewSize = const Size(800.0, 600.0);
class TestRenderView extends RenderView {
TestRenderView() {
attach();
rootConstraints = new ViewConstraints(size: _kTestViewSize);
scheduleInitialLayout();
scheduleInitialPaint(new TransformLayer(transform: new Matrix4.identity()));
}
}
enum EnginePhase {
layout,
paint,
composite
}
RenderView _renderView;
RenderView get renderView => _renderView;
void layout(RenderBox box, { BoxConstraints constraints, EnginePhase phase: EnginePhase.layout }) {
assert(box != null); // if you want to just repump the last box, call pumpFrame().
if (renderView == null)
_renderView = new TestRenderView();
if (renderView.child != null)
renderView.child = null;
if (constraints != null) {
box = new RenderPositionedBox(
child: new RenderConstrainedBox(
additionalConstraints: constraints,
child: box
)
);
}
renderView.child = box;
pumpFrame(phase: phase);
}
void pumpFrame({ EnginePhase phase: EnginePhase.layout }) {
RenderObject.flushLayout();
if (phase == EnginePhase.layout)
return;
renderView.updateCompositingBits();
RenderObject.flushPaint();
if (phase == EnginePhase.paint)
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;
}