Fix gradient stop out of range error (flutter/engine#26518)

This commit is contained in:
Ferhat
2021-06-01 13:58:46 -07:00
committed by GitHub
parent 607b2ce7f3
commit dc2c32bbc7
2 changed files with 38 additions and 1 deletions

View File

@@ -387,8 +387,9 @@ void _addColorStopsToCanvasGradient(html.CanvasGradient gradient,
gradient.addColorStop(1 - offset, colorToCssString(colors[1])!);
} else {
for (int i = 0; i < colors.length; i++) {
final double colorStop = colorStops[i].clamp(0.0, 1.0);
gradient.addColorStop(
colorStops[i] * scale + offset, colorToCssString(colors[i])!);
colorStop * scale + offset, colorToCssString(colors[i])!);
}
}
if (isDecal) {

View File

@@ -345,6 +345,42 @@ void testMain() async {
await _checkScreenshot(canvas, 'linear_gradient_rect_shifted');
});
/// Regression test for https://github.com/flutter/flutter/issues/82748.
test('Paints gradient with gradient stop outside range', () async {
final RecordingCanvas canvas =
RecordingCanvas(const Rect.fromLTRB(0, 0, 400, 300));
canvas.save();
final Paint borderPaint = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1
..color = Color(0xFF000000);
List<Color> colors = <Color>[
Color(0xFF000000),
Color(0xFFFF3C38)];
List<double> stops = <double>[0.0, 10.0];
EngineGradient linearGradient = GradientLinear(Offset(50, 50),
Offset(200,130),
colors, stops, TileMode.clamp,
Matrix4.identity().storage);
const double kBoxWidth = 150;
const double kBoxHeight = 80;
// Gradient with default center.
Rect rectBounds = Rect.fromLTWH(10, 20, kBoxWidth, kBoxHeight);
canvas.drawRect(rectBounds,
Paint()..shader = engineLinearGradientToShader(linearGradient, rectBounds));
canvas.drawRect(rectBounds, borderPaint);
canvas.restore();
final EngineCanvas engineCanvas = BitmapCanvas(screenRect,
RenderStrategy());
canvas.endRecording();
canvas.apply(engineCanvas, screenRect);
});
test('Paints clamped, rotated and shifted linear gradient', () async {
final RecordingCanvas canvas =
RecordingCanvas(const Rect.fromLTRB(0, 0, 400, 300));