Skip to content

Commit

Permalink
Feat: Add withOpacity to gradient (flutter#150670)
Browse files Browse the repository at this point in the history
Feat: Add withOpacity to gradient because sometimes it's required to call withOpacity directly on gradient

Resolves flutter#150501 

This adds a `withOpacity` feature to a gradient, given user has given colors this will override opacity to given opacity for them.
  • Loading branch information
rkishan516 authored Jul 3, 2024
1 parent 56835d6 commit fbd6890
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/flutter/lib/src/painting/gradient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ abstract class Gradient {
/// Typically this is the same as interpolating from null (with [lerp]).
Gradient scale(double factor);

/// Returns a new [Gradient] with each color set to the given opacity.
Gradient withOpacity(double opacity);

/// Linearly interpolates from another [Gradient] to `this`.
///
/// When implementing this method in subclasses, return null if this class
Expand Down Expand Up @@ -556,6 +559,19 @@ class LinearGradient extends Gradient {

return '${objectRuntimeType(this, 'LinearGradient')}(${description.join(', ')})';
}

@override
LinearGradient withOpacity(double opacity) {
return LinearGradient(
begin: begin,
end: end,
colors: <Color>[
for (final Color color in colors) color.withOpacity(opacity)
],
stops: stops,
tileMode: tileMode,
);
}
}

/// A 2D radial gradient.
Expand Down Expand Up @@ -843,6 +859,21 @@ class RadialGradient extends Gradient {

return '${objectRuntimeType(this, 'RadialGradient')}(${description.join(', ')})';
}

@override
RadialGradient withOpacity(double opacity) {
return RadialGradient(
center: center,
radius: radius,
colors: <Color>[
for (final Color color in colors) color.withOpacity(opacity)
],
stops: stops,
tileMode: tileMode,
focal: focal,
focalRadius: focalRadius,
);
}
}

/// A 2D sweep gradient.
Expand Down Expand Up @@ -1106,4 +1137,18 @@ class SweepGradient extends Gradient {

return '${objectRuntimeType(this, 'SweepGradient')}(${description.join(', ')})';
}

@override
SweepGradient withOpacity(double opacity) {
return SweepGradient(
center: center,
startAngle: startAngle,
endAngle: endAngle,
colors: <Color>[
for (final Color color in colors) color.withOpacity(opacity)
],
stops: stops,
tileMode: tileMode,
);
}
}
72 changes: 72 additions & 0 deletions packages/flutter/test/painting/gradient_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,29 @@ void main() {
);
});

test('LinearGradient withOpacity test', () {
const LinearGradient testGradient = LinearGradient(
begin: Alignment.bottomRight,
end: Alignment.topCenter,
colors: <Color>[
Color(0xFFFFFFFF),
Color(0xAF777777),
Color(0x44444444),
],
);
final LinearGradient actual = testGradient.withOpacity(0.5);

expect(actual, const LinearGradient(
begin: Alignment.bottomRight,
end: Alignment.topCenter,
colors: <Color>[
Color(0x80FFFFFF),
Color(0x80777777),
Color(0x80444444),
],
),);
});

test('RadialGradient with AlignmentDirectional', () {
expect(
() {
Expand Down Expand Up @@ -596,6 +619,33 @@ void main() {
));
});


test('RadialGradient withOpacity test', () {
const RadialGradient testGradient = RadialGradient(
center: Alignment.topLeft,
focal: Alignment.centerLeft,
radius: 20.0,
focalRadius: 10.0,
colors: <Color>[
Color(0xFFFFFFFF),
Color(0xAF777777),
Color(0x44444444),
],
);
final RadialGradient actual = testGradient.withOpacity(0.5);

expect(actual, const RadialGradient(
center: Alignment.topLeft,
focal: Alignment.centerLeft,
radius: 20.0,
focalRadius: 10.0,
colors: <Color>[
Color(0x80FFFFFF),
Color(0x80777777),
Color(0x80444444),
],
));
});
test('SweepGradient lerp test', () {
const SweepGradient testGradient1 = SweepGradient(
center: Alignment.topLeft,
Expand Down Expand Up @@ -806,6 +856,28 @@ void main() {
));
});

test('SweepGradient withOpacity test', () {
const SweepGradient testGradient = SweepGradient(
center: Alignment.topLeft,
endAngle: math.pi / 2,
colors: <Color>[
Color(0xFFFFFFFF),
Color(0xAF777777),
Color(0x44444444),
],
);
final SweepGradient actual = testGradient.withOpacity(0.5);

expect(actual, const SweepGradient(
center: Alignment.topLeft,
endAngle: math.pi / 2,
colors: <Color>[
Color(0x80FFFFFF),
Color(0x80777777),
Color(0x80444444),
],
));
});
test('Gradient lerp test (with RadialGradient)', () {
const RadialGradient testGradient1 = RadialGradient(
center: Alignment.topLeft,
Expand Down

0 comments on commit fbd6890

Please sign in to comment.