Sub topic 10
Filling Shapes with Gradients
This chapter assumes you know Dart classes, basic Flutter widget trees, CustomPainter, Canvas, Paint, and Flutter's Size type. A gradient is a smooth color transition. In Flutter, a gradient creates a shader, and a shader tells Paint how to calculate the color at each pixel.
Creating a gradient shader
Assign the result of Gradient.createShader to Paint.shader. The shader needs a rectangle that defines the gradient's coordinate space. A common choice is the full painter area:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: GradientExample()));
}
class GradientExample extends StatelessWidget {
const GradientExample({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: CustomPaint(
size: Size(280, 160),
painter: GradientCardPainter(),
),
),
);
}
}
class GradientCardPainter extends CustomPainter {
const GradientCardPainter();
@override
void paint(Canvas canvas, Size size) {
final bounds = Offset.zero & size;
final paint = Paint()
..shader = const LinearGradient(
colors: [Colors.purple, Colors.orange],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
).createShader(bounds);
canvas.drawRRect(
RRect.fromRectAndRadius(
bounds,
const Radius.circular(20),
),
paint,
);
}
@override
bool shouldRepaint(covariant GradientCardPainter oldDelegate) {
return false;
}
}Offset.zero & size creates a rectangle from (0, 0) to the painter's width and height. The gradient uses that rectangle to map Alignment.topLeft and Alignment.bottomRight to actual points.
The Paint still controls how the shape is drawn. Its shader supplies colors, while properties such as style, strokeWidth, and strokeCap control the drawing style.
Choosing a gradient type
Flutter provides several gradient classes:
LinearGradientchanges color along a straight line.RadialGradientspreads color outward from a center point.SweepGradientchanges color around a circle, like a color wheel.
Each gradient accepts multiple colors. You can control where each color appears with stops. Stops are values from 0.0 to 1.0:
final paint = Paint()
..shader = const RadialGradient(
colors: [Colors.white, Colors.blue, Colors.black],
stops: [0.0, 0.45, 1.0],
).createShader(Offset.zero & size);The first stop is the gradient's center, and the last stop is its outer edge. If you omit stops, Flutter distributes the colors evenly.
Applying gradients to paths and strokes
A shader works with more than rectangles. You can pass the same Paint to drawCircle, drawPath, or drawArc. For a gradient outline, use PaintingStyle.stroke:
final stroke = Paint()
..shader = const LinearGradient(
colors: [Colors.cyan, Colors.indigo],
).createShader(Offset.zero & size)
..style = PaintingStyle.stroke
..strokeWidth = 8;
canvas.drawCircle(size.center(Offset.zero), 50, stroke);The shader fills only the pixels covered by the shape. If the shape extends outside the shader rectangle, the result may not match your intended layout. Recalculate the shader from the current size in every paint call.
> callout tip > Do not create a shader with a fixed rectangle when the painter can change size. Use the Size supplied to paint, so the gradient remains aligned after layout changes.
Exercise
Exercise
Why should a painter usually pass its current bounds to createShader instead of using a fixed rectangle?
Show answer
The current bounds keep the gradient aligned and scaled with the painter when its size changes.