Skip to content

Commit 502a4ae

Browse files
committed
Use FractionalOffset for gradients
These were using Offsets, but they're really FractionalOffsets. Fixes flutter#2318
1 parent 0eea48c commit 502a4ae

File tree

5 files changed

+18
-21
lines changed

5 files changed

+18
-21
lines changed

dev/manual_tests/card_collection.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ class CardCollectionState extends State<CardCollection> {
397397

398398
Shader _createShader(Rect bounds) {
399399
return new LinearGradient(
400-
begin: const Offset(0.0, 0.0),
401-
end: const Offset(0.0, 1.0),
400+
begin: const FractionalOffset(0.0, 0.0),
401+
end: const FractionalOffset(0.0, 1.0),
402402
colors: <Color>[const Color(0x00FFFFFF), const Color(0xFFFFFFFF)],
403403
stops: <double>[0.1, 0.35]
404404
)

examples/material_gallery/lib/demo/weather_demo.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ class GradientNode extends NodeWithSize {
285285

286286
Rect rect = Point.origin & size;
287287
Paint gradientPaint = new Paint()..shader = new LinearGradient(
288-
begin: const Offset(0.0, 0.0),
289-
end: const Offset(0.0, 1.0),
288+
begin: const FractionalOffset(0.0, 0.0),
289+
end: const FractionalOffset(0.0, 1.0),
290290
colors: <Color>[colorTop, colorBottom],
291291
stops: <double>[0.0, 1.0]
292292
).createShader(rect);

packages/flutter/lib/src/painting/box_painter.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,6 @@ class BoxShadow {
244244
String toString() => 'BoxShadow($color, $offset, $blurRadius, $spreadRadius)';
245245
}
246246

247-
// TODO(ianh): We should probably expose something that does this on Rect.
248-
// https://github.com/flutter/flutter/issues/2318
249-
Point _offsetToPoint(Offset offset, Rect rect) {
250-
return new Point(rect.left + offset.dx * rect.width, rect.top + offset.dy * rect.height);
251-
}
252-
253247
/// A 2D gradient.
254248
abstract class Gradient {
255249
const Gradient();
@@ -259,8 +253,8 @@ abstract class Gradient {
259253
/// A 2D linear gradient.
260254
class LinearGradient extends Gradient {
261255
const LinearGradient({
262-
this.begin: const Offset(0.0, 0.5),
263-
this.end: const Offset(1.0, 0.5),
256+
this.begin: const FractionalOffset(0.0, 0.5),
257+
this.end: const FractionalOffset(1.0, 0.5),
264258
this.colors,
265259
this.stops,
266260
this.tileMode: TileMode.clamp
@@ -272,15 +266,15 @@ class LinearGradient extends Gradient {
272266
///
273267
/// For example, a begin offset of (0.0,0.5) is half way down the
274268
/// left side of the box.
275-
final Offset begin;
269+
final FractionalOffset begin;
276270

277271
/// The offset from coordinate (0.0,0.0) at which stop 1.0 of the
278272
/// gradient is placed, in a coordinate space that maps the top left
279273
/// of the paint box at (0.0,0.0) and the bottom right at (1.0,1.0).
280274
///
281275
/// For example, an end offset of (1.0,0.5) is half way down the
282276
/// right side of the box.
283-
final Offset end;
277+
final FractionalOffset end;
284278

285279
/// The colors the gradient should obtain at each of the stops.
286280
///
@@ -299,7 +293,7 @@ class LinearGradient extends Gradient {
299293
@override
300294
Shader createShader(Rect rect) {
301295
return new ui.Gradient.linear(
302-
<Point>[_offsetToPoint(begin, rect), _offsetToPoint(end, rect)],
296+
<Point>[begin.withinRect(rect), end.withinRect(rect)],
303297
colors, stops, tileMode
304298
);
305299
}
@@ -348,7 +342,7 @@ class LinearGradient extends Gradient {
348342
/// A 2D radial gradient.
349343
class RadialGradient extends Gradient {
350344
const RadialGradient({
351-
this.center: const Offset(0.5, 0.5),
345+
this.center: const FractionalOffset(0.5, 0.5),
352346
this.radius: 0.5,
353347
this.colors,
354348
this.stops,
@@ -360,7 +354,7 @@ class RadialGradient extends Gradient {
360354
///
361355
/// For example, an offset of (0.5,0.5) will place the radial
362356
/// gradient in the center of the box.
363-
final Offset center;
357+
final FractionalOffset center;
364358

365359
/// The radius of the gradient, as a fraction of the shortest side
366360
/// of the paint box.
@@ -389,7 +383,7 @@ class RadialGradient extends Gradient {
389383
@override
390384
Shader createShader(Rect rect) {
391385
return new ui.Gradient.radial(
392-
_offsetToPoint(center, rect),
386+
center.withinRect(rect),
393387
radius * rect.shortestSide,
394388
colors, stops, tileMode
395389
);
@@ -645,6 +639,9 @@ class FractionalOffset {
645639
Offset alongSize(Size other) {
646640
return new Offset(dx * other.width, dy * other.height);
647641
}
642+
Point withinRect(Rect rect) {
643+
return new Point(rect.left + dx * rect.width, rect.top + dy * rect.height);
644+
}
648645

649646
@override
650647
bool operator ==(dynamic other) {

packages/flutter/test/rendering/box_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void main() {
1414
decoration: new BoxDecoration(
1515
backgroundColor: const Color(0xFF00FF00),
1616
gradient: new RadialGradient(
17-
center: Offset.zero, radius: 1.8,
17+
center: FractionalOffset.zero, radius: 1.8,
1818
colors: <Color>[Colors.yellow[500], Colors.blue[500]]),
1919
boxShadow: elevationToShadow[3])
2020
);

packages/flutter/test/widget/shader_mask_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import 'package:test/test.dart';
1010

1111
Shader createShader(Rect bounds) {
1212
return new LinearGradient(
13-
begin: const Offset(0.0, 0.0),
14-
end: const Offset(0.0, 1.0),
13+
begin: const FractionalOffset(0.0, 0.0),
14+
end: const FractionalOffset(0.0, 1.0),
1515
colors: <Color>[const Color(0x00FFFFFF), const Color(0xFFFFFFFF)],
1616
stops: <double>[0.1, 0.35]
1717
)

0 commit comments

Comments
 (0)