diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/shaders/shader.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/shaders/shader.dart index c5f374b262..1554a8b620 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/shaders/shader.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/shaders/shader.dart @@ -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) { diff --git a/engine/src/flutter/lib/web_ui/test/golden_tests/engine/gradient_golden_test.dart b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/gradient_golden_test.dart index ba513ff1fa..57662aa768 100644 --- a/engine/src/flutter/lib/web_ui/test/golden_tests/engine/gradient_golden_test.dart +++ b/engine/src/flutter/lib/web_ui/test/golden_tests/engine/gradient_golden_test.dart @@ -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 colors = [ + Color(0xFF000000), + Color(0xFFFF3C38)]; + List stops = [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));