Commit 4a04de5e authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Revert "LinearGradient and RadialGradient RTL" (#12209)

* Revert "Pin args to 0.13.7 to avoid version conflict (#12208)"

This reverts commit 63802b3d.

* Revert "LinearGradient and RadialGradient RTL (#12204)"

This reverts commit daedbc84.
parent 63802b3d
...@@ -25,11 +25,7 @@ abstract class Gradient { ...@@ -25,11 +25,7 @@ abstract class Gradient {
const Gradient(); const Gradient();
/// Creates a [Shader] for this gradient to fill the given rect. /// Creates a [Shader] for this gradient to fill the given rect.
/// Shader createShader(Rect rect);
/// If the gradient's configuration is text-direction-dependent, for example
/// it uses [FractionalOffsetDirection] objects instead of [FractionalOffset]
/// objects, then the `textDirection` argument must not be null.
Shader createShader(Rect rect, { TextDirection textDirection });
} }
/// A 2D linear gradient. /// A 2D linear gradient.
...@@ -95,37 +91,21 @@ class LinearGradient extends Gradient { ...@@ -95,37 +91,21 @@ class LinearGradient extends Gradient {
assert(colors != null), assert(colors != null),
assert(tileMode != null); assert(tileMode != null);
/// The offset at which stop 0.0 of the gradient is placed. /// The offset from coordinate (0.0,0.0) at which stop 0.0 of the
/// /// gradient is placed, in a coordinate space that maps the top left
/// If this is a [FractionalOffset], then it is expressed as a vector from /// of the paint box at (0.0,0.0) and the bottom right at (1.0,1.0).
/// coordinate (0.0,0.0), in a coordinate space that maps the top left of the
/// paint box at (0.0,0.0) and the bottom right at (1.0,1.0).
/// ///
/// For example, a begin offset of (0.0,0.5) is half way down the /// For example, a begin offset of (0.0,0.5) is half way down the
/// left side of the box. /// left side of the box.
/// final FractionalOffset begin;
/// It can also be a [FractionalOffsetDirectional], in which case it is
/// expressed as a vector from the top start corner, where the start is the
/// left in left-to-right contexts and the right in right-to-left contexts. If
/// a text-direction-dependent value is provided here, then the [createShader]
/// method will need to be given a [TextDirection].
final FractionalOffsetGeometry begin;
/// The offset at which stop 1.0 of the gradient is placed. /// The offset from coordinate (0.0,0.0) at which stop 1.0 of the
/// gradient is placed, in a coordinate space that maps the top left
/// of the paint box at (0.0,0.0) and the bottom right at (1.0,1.0).
/// ///
/// If this is a [FractionalOffset], then it is expressed as a vector from /// For example, an end offset of (1.0,0.5) is half way down the
/// coordinate (0.0,0.0), in a coordinate space that maps the top left of the
/// paint box at (0.0,0.0) and the bottom right at (1.0,1.0).
///
/// For example, a begin offset of (1.0,0.5) is half way down the
/// right side of the box. /// right side of the box.
/// final FractionalOffset end;
/// It can also be a [FractionalOffsetDirectional], in which case it is
/// expressed as a vector from the top start corner, where the start is the
/// left in left-to-right contexts and the right in right-to-left contexts. If
/// a text-direction-dependent value is provided here, then the [createShader]
/// method will need to be given a [TextDirection].
final FractionalOffsetGeometry end;
/// The colors the gradient should obtain at each of the stops. /// The colors the gradient should obtain at each of the stops.
/// ///
...@@ -164,20 +144,16 @@ class LinearGradient extends Gradient { ...@@ -164,20 +144,16 @@ class LinearGradient extends Gradient {
final TileMode tileMode; final TileMode tileMode;
@override @override
Shader createShader(Rect rect, { TextDirection textDirection }) { Shader createShader(Rect rect) {
return new ui.Gradient.linear( return new ui.Gradient.linear(
begin.resolve(textDirection).withinRect(rect), begin.withinRect(rect),
end.resolve(textDirection).withinRect(rect), end.withinRect(rect),
colors, stops, tileMode, colors, stops, tileMode,
); );
} }
/// Returns a new [LinearGradient] with its properties (in particular the /// Returns a new [LinearGradient] with its properties scaled by the given
/// colors) scaled by the given factor. /// factor.
///
/// If the factor is 1.0 or greater, then the gradient is returned unmodified.
/// If the factor is 0.0 or less, then the gradient is fully transparent.
/// Values in between scale the opacity of the colors.
LinearGradient scale(double factor) { LinearGradient scale(double factor) {
return new LinearGradient( return new LinearGradient(
begin: begin, begin: begin,
...@@ -192,7 +168,7 @@ class LinearGradient extends Gradient { ...@@ -192,7 +168,7 @@ class LinearGradient extends Gradient {
/// ///
/// If either gradient is null, this function linearly interpolates from a /// If either gradient is null, this function linearly interpolates from a
/// a gradient that matches the other gradient in [begin], [end], [stops] and /// a gradient that matches the other gradient in [begin], [end], [stops] and
/// [tileMode] and with the same [colors] but transparent (using [scale]). /// [tileMode] and with the same [colors] but transparent.
/// ///
/// If neither gradient is null, they must have the same number of [colors]. /// If neither gradient is null, they must have the same number of [colors].
static LinearGradient lerp(LinearGradient a, LinearGradient b, double t) { static LinearGradient lerp(LinearGradient a, LinearGradient b, double t) {
...@@ -202,7 +178,11 @@ class LinearGradient extends Gradient { ...@@ -202,7 +178,11 @@ class LinearGradient extends Gradient {
return b.scale(t); return b.scale(t);
if (b == null) if (b == null)
return a.scale(1.0 - t); return a.scale(1.0 - t);
assert(a.colors.length == b.colors.length, 'Cannot interpolate between two gradients with a different number of colors.'); // Interpolation is only possible when the lengths of colors and stops are
// the same or stops is null for one.
// TODO(xster): lerp unsimilar LinearGradients in the future by scaling
// lists of LinearGradients.
assert(a.colors.length == b.colors.length);
assert(a.stops == null || b.stops == null || a.stops.length == b.stops.length); assert(a.stops == null || b.stops == null || a.stops.length == b.stops.length);
final List<Color> interpolatedColors = <Color>[]; final List<Color> interpolatedColors = <Color>[];
for (int i = 0; i < a.colors.length; i += 1) for (int i = 0; i < a.colors.length; i += 1)
...@@ -215,8 +195,8 @@ class LinearGradient extends Gradient { ...@@ -215,8 +195,8 @@ class LinearGradient extends Gradient {
interpolatedStops = a.stops ?? b.stops; interpolatedStops = a.stops ?? b.stops;
} }
return new LinearGradient( return new LinearGradient(
begin: FractionalOffsetGeometry.lerp(a.begin, b.begin, t), begin: FractionalOffset.lerp(a.begin, b.begin, t),
end: FractionalOffsetGeometry.lerp(a.end, b.end, t), end: FractionalOffset.lerp(a.end, b.end, t),
colors: interpolatedColors, colors: interpolatedColors,
stops: interpolatedStops, stops: interpolatedStops,
tileMode: t < 0.5 ? a.tileMode : b.tileMode, tileMode: t < 0.5 ? a.tileMode : b.tileMode,
...@@ -260,7 +240,7 @@ class LinearGradient extends Gradient { ...@@ -260,7 +240,7 @@ class LinearGradient extends Gradient {
@override @override
String toString() { String toString() {
return '$runtimeType($begin, $end, $colors, $stops, $tileMode)'; return 'LinearGradient($begin, $end, $colors, $stops, $tileMode)';
} }
} }
...@@ -339,17 +319,7 @@ class RadialGradient extends Gradient { ...@@ -339,17 +319,7 @@ class RadialGradient extends Gradient {
/// ///
/// For example, an offset of (0.5,0.5) will place the radial /// For example, an offset of (0.5,0.5) will place the radial
/// gradient in the center of the box. /// gradient in the center of the box.
/// final FractionalOffset center;
/// If this is a [FractionalOffset], then it is expressed as a vector from
/// coordinate (0.0,0.0), in a coordinate space that maps the top left of the
/// paint box at (0.0,0.0) and the bottom right at (1.0,1.0).
///
/// It can also be a [FractionalOffsetDirectional], in which case it is
/// expressed as a vector from the top start corner, where the start is the
/// left in left-to-right contexts and the right in right-to-left contexts. If
/// a text-direction-dependent value is provided here, then the [createShader]
/// method will need to be given a [TextDirection].
final FractionalOffsetGeometry center;
/// The radius of the gradient, as a fraction of the shortest side /// The radius of the gradient, as a fraction of the shortest side
/// of the paint box. /// of the paint box.
...@@ -398,11 +368,11 @@ class RadialGradient extends Gradient { ...@@ -398,11 +368,11 @@ class RadialGradient extends Gradient {
final TileMode tileMode; final TileMode tileMode;
@override @override
Shader createShader(Rect rect, { TextDirection textDirection }) { Shader createShader(Rect rect) {
return new ui.Gradient.radial( return new ui.Gradient.radial(
center.resolve(textDirection).withinRect(rect), center.withinRect(rect),
radius * rect.shortestSide, radius * rect.shortestSide,
colors, stops, tileMode, colors, stops, tileMode
); );
} }
...@@ -443,6 +413,6 @@ class RadialGradient extends Gradient { ...@@ -443,6 +413,6 @@ class RadialGradient extends Gradient {
@override @override
String toString() { String toString() {
return '$runtimeType($center, $radius, $colors, $stops, $tileMode)'; return 'RadialGradient($center, $radius, $colors, $stops, $tileMode)';
} }
} }
...@@ -112,4 +112,74 @@ void main() { ...@@ -112,4 +112,74 @@ void main() {
test('BoxShadow toString test', () { test('BoxShadow toString test', () {
expect(const BoxShadow(blurRadius: 4.0).toString(), equals('BoxShadow(Color(0xff000000), Offset(0.0, 0.0), 4.0, 0.0)')); expect(const BoxShadow(blurRadius: 4.0).toString(), equals('BoxShadow(Color(0xff000000), Offset(0.0, 0.0), 4.0, 0.0)'));
}); });
test('LinearGradient scale test', () {
final LinearGradient testGradient = const LinearGradient(
begin: FractionalOffset.bottomRight,
end: const FractionalOffset(0.7, 1.0),
colors: const <Color>[
const Color(0x00FFFFFF),
const Color(0x11777777),
const Color(0x44444444),
],
);
final LinearGradient actual = LinearGradient.lerp(null, testGradient, 0.25);
expect(actual, const LinearGradient(
begin: FractionalOffset.bottomRight,
end: const FractionalOffset(0.7, 1.0),
colors: const <Color>[
const Color(0x00FFFFFF),
const Color(0x04777777),
const Color(0x11444444),
],
));
});
test('LinearGradient lerp test', () {
final LinearGradient testGradient1 = const LinearGradient(
begin: FractionalOffset.topLeft,
end: FractionalOffset.bottomLeft,
colors: const <Color>[
const Color(0x33333333),
const Color(0x66666666),
],
);
final LinearGradient testGradient2 = const LinearGradient(
begin: FractionalOffset.topRight,
end: FractionalOffset.topLeft,
colors: const <Color>[
const Color(0x44444444),
const Color(0x88888888),
],
);
final LinearGradient actual =
LinearGradient.lerp(testGradient1, testGradient2, 0.5);
expect(actual, const LinearGradient(
begin: const FractionalOffset(0.5, 0.0),
end: const FractionalOffset(0.0, 0.5),
colors: const <Color>[
const Color(0x3B3B3B3B),
const Color(0x77777777),
],
));
});
test('LinearGradient toString', () {
expect(
const LinearGradient(
begin: FractionalOffset.topLeft,
end: FractionalOffset.bottomLeft,
colors: const <Color>[
const Color(0x33333333),
const Color(0x66666666),
],
).toString(),
equals(
'LinearGradient(FractionalOffset.topLeft, FractionalOffset.bottomLeft, [Color(0x33333333), Color(0x66666666)], null, TileMode.clamp)',
),
);
});
} }
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/painting.dart';
void main() {
test('LinearGradient scale test', () {
final LinearGradient testGradient = const LinearGradient(
begin: FractionalOffset.bottomRight,
end: const FractionalOffset(0.7, 1.0),
colors: const <Color>[
const Color(0x00FFFFFF),
const Color(0x11777777),
const Color(0x44444444),
],
);
final LinearGradient actual = LinearGradient.lerp(null, testGradient, 0.25);
expect(actual, const LinearGradient(
begin: FractionalOffset.bottomRight,
end: const FractionalOffset(0.7, 1.0),
colors: const <Color>[
const Color(0x00FFFFFF),
const Color(0x04777777),
const Color(0x11444444),
],
));
});
test('LinearGradient lerp test', () {
final LinearGradient testGradient1 = const LinearGradient(
begin: FractionalOffset.topLeft,
end: FractionalOffset.bottomLeft,
colors: const <Color>[
const Color(0x33333333),
const Color(0x66666666),
],
);
final LinearGradient testGradient2 = const LinearGradient(
begin: FractionalOffset.topRight,
end: FractionalOffset.topLeft,
colors: const <Color>[
const Color(0x44444444),
const Color(0x88888888),
],
);
final LinearGradient actual = LinearGradient.lerp(testGradient1, testGradient2, 0.5);
expect(actual, const LinearGradient(
begin: const FractionalOffset(0.5, 0.0),
end: const FractionalOffset(0.0, 0.5),
colors: const <Color>[
const Color(0x3B3B3B3B),
const Color(0x77777777),
],
));
});
test('LinearGradient toString', () {
expect(
const LinearGradient(
begin: FractionalOffset.topLeft,
end: FractionalOffset.bottomLeft,
colors: const <Color>[
const Color(0x33333333),
const Color(0x66666666),
],
).toString(),
equals(
'LinearGradient(FractionalOffset.topLeft, FractionalOffset.bottomLeft, [Color(0x33333333), Color(0x66666666)], null, TileMode.clamp)',
),
);
});
test('LinearGradient with FractionalOffsetDirectional', () {
expect(
() {
return const LinearGradient(
begin: FractionalOffsetDirectional.topStart,
colors: const <Color>[ const Color(0xFFFFFFFF), const Color(0xFFFFFFFF) ]
).createShader(new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0));
},
throwsAssertionError,
);
expect(
() {
return const LinearGradient(
begin: FractionalOffsetDirectional.topStart,
colors: const <Color>[ const Color(0xFFFFFFFF), const Color(0xFFFFFFFF) ]
).createShader(new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0), textDirection: TextDirection.rtl);
},
returnsNormally,
);
expect(
() {
return const LinearGradient(
begin: FractionalOffsetDirectional.topStart,
colors: const <Color>[ const Color(0xFFFFFFFF), const Color(0xFFFFFFFF) ]
).createShader(new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0), textDirection: TextDirection.ltr);
},
returnsNormally,
);
expect(
() {
return const LinearGradient(
begin: FractionalOffset.topLeft,
colors: const <Color>[ const Color(0xFFFFFFFF), const Color(0xFFFFFFFF) ]
).createShader(new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0));
},
returnsNormally,
);
});
test('RadialGradient with FractionalOffsetDirectional', () {
expect(
() {
return const RadialGradient(
center: FractionalOffsetDirectional.topStart,
colors: const <Color>[ const Color(0xFFFFFFFF), const Color(0xFFFFFFFF) ]
).createShader(new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0));
},
throwsAssertionError,
);
expect(
() {
return const RadialGradient(
center: FractionalOffsetDirectional.topStart,
colors: const <Color>[ const Color(0xFFFFFFFF), const Color(0xFFFFFFFF) ]
).createShader(new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0), textDirection: TextDirection.rtl);
},
returnsNormally,
);
expect(
() {
return const RadialGradient(
center: FractionalOffsetDirectional.topStart,
colors: const <Color>[ const Color(0xFFFFFFFF), const Color(0xFFFFFFFF) ]
).createShader(new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0), textDirection: TextDirection.ltr);
},
returnsNormally,
);
expect(
() {
return const RadialGradient(
center: FractionalOffset.topLeft,
colors: const <Color>[ const Color(0xFFFFFFFF), const Color(0xFFFFFFFF) ]
).createShader(new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0));
},
returnsNormally,
);
});
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment