LinearGradient class

A 2D linear gradient.

This class is used by BoxDecoration to represent linear gradients. This abstracts out the arguments to the ui.Gradient.linear constructor from the dart:ui library.

A gradient has two anchor points, begin and end. The begin point corresponds to 0.0, and the end point corresponds to 1.0. These points are expressed in fractions, so that the same gradient can be reused with varying sized boxes without changing the parameters. (This contrasts with ui.Gradient.linear, whose arguments are expressed in logical pixels.)

The colors are described by a list of Color objects. There must be at least two colors. The stops list, if specified, must have the same length as colors. It specifies fractions of the vector from start to end, between 0.0 and 1.0, for each color. If it is null, a uniform distribution is assumed.

The region of the canvas before begin and after end is colored according to tileMode.

Typically this class is used with BoxDecoration, which does the painting. To use a LinearGradient to paint on a canvas directly, see createShader.

This sample draws a picture with a gradient sweeping through different colors, by having a Container display a BoxDecoration with a LinearGradient.

To create a local project with this code sample, run:
flutter create --sample=painting.LinearGradient.1 mysample

See also:



LinearGradient({AlignmentGeometry begin = Alignment.centerLeft, AlignmentGeometry end = Alignment.centerRight, required List<Color> colors, List<double>? stops, TileMode tileMode = TileMode.clamp, GradientTransform? transform})
Creates a linear gradient.


begin AlignmentGeometry
The offset at which stop 0.0 of the gradient is placed.
colors List<Color>
The colors the gradient should obtain at each of the stops.
end AlignmentGeometry
The offset at which stop 1.0 of the gradient is placed.
hashCode int
The hash code for this object.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stops List<double>?
A list of values from 0.0 to 1.0 that denote fractions along the gradient.
tileMode TileMode
How this gradient should tile the plane beyond in the region before begin and after end.
transform GradientTransform?
The transform, if any, to apply to the gradient.


createShader(Rect rect, {TextDirection? textDirection}) Shader
Creates a Shader for this gradient to fill the given rect.
lerpFrom(Gradient? a, double t) Gradient?
Linearly interpolates from another Gradient to this.
lerpTo(Gradient? b, double t) Gradient?
Linearly interpolates from this to another Gradient.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
scale(double factor) LinearGradient
Returns a new LinearGradient with its colors scaled by the given factor.
toString() String
A string representation of this object.


operator ==(Object other) bool
The equality operator.

Static Methods

lerp(LinearGradient? a, LinearGradient? b, double t) LinearGradient?
Linearly interpolate between two LinearGradients.