What are Gradients?
Gradients are smooth transitions between two or more colors. In Flutter, gradients are used to add depth and visual interest to UI elements. Flutter provides three types of gradients: LinearGradient (colors transition along a line), RadialGradient (colors transition from a center point outward), and SweepGradient (colors transition around a center point). Gradients are typically applied via BoxDecoration to a Container or DecoratedBox, but can also be used for text, custom painters, and more.
LinearGradient – A Straight Line Transition
LinearGradient creates a gradient along a straight line. You specify a list of colors and optional stops, plus a begin and end Alignment (or FractionalOffset) to define the direction.
You can also use Alignment constants like Alignment.centerLeft, or create custom offsets with Alignment(x, y). For more control, you can use FractionalOffset which ranges from 0.0 to 1.0 in both axes.
RadialGradient – Circular Transition
RadialGradient radiates from a center point. The gradient begins at center and spreads outward to the radius. You can also define focal (a secondary focus point) for a more dramatic effect.
SweepGradient – Angular Transition
SweepGradient creates a gradient that rotates around a center point, like a color wheel. It's great for loading indicators or circular progress.
Applying Gradients to Text
To apply a gradient to text, you can use ShaderMask or RichText with a TextSpan and TextStyle that includes a foreground paint. The ShaderMask approach is simpler for entire text blocks.
Gradients on CustomPaint
For custom shapes, you can use the CustomPaint widget and create a CustomPainter that uses a Paint with a shader created from the gradient. This gives you full control over the drawing area.
Common Mistakes
- Using
Colorslist with inconsistent stops: If you provide stops, you must provide one stop per color, and they must be in increasing order (0.0 to 1.0).
- Using
- Forgetting
BoxDecoration: Gradients can only be applied viaBoxDecoration(for containers) or directly viaPaintin custom painting; you cannot setgradientdirectly on aContainerwithout decoration.
- Forgetting
- Mixing
AlignmentandFractionalOffset: They are different types; use the appropriate one for the property (e.g.,beginexpectsAlignment).
- Mixing
- Not specifying
colorswith at least two colors: A gradient needs at least two colors; otherwise it's just a solid color.
- Not specifying
- Applying gradient to text without
ShaderMask: Directly settinggradientonTextStyleis not supported; useShaderMaskorRichText.
- Applying gradient to text without
Best Practices
- For smooth transitions, use colors with similar brightness or a gradient from a color scheme library.
- Use
constfor gradient definitions that don't change to improve performance.
- Use
- Precompute gradients that are reused across multiple widgets to avoid unnecessary allocations.
- When applying gradients to text, ensure the text remains readable – use high contrast or darken the gradient beneath.
- Test gradients on different background colors to ensure visibility.
Key Takeaways
- Use
LinearGradientfor straight line transitions.
- Use
- Use
RadialGradientfor circular transitions.
- Use
- Use
SweepGradientfor angular transitions (like color wheels).
- Use
- Gradients are applied through
BoxDecoration(for containers) or throughPaintshaders (for custom drawing).
- Gradients are applied through
- For text, wrap the text in
ShaderMaskwith a gradient shader.
- For text, wrap the text in
- Provide stops for fine control over color placement.