Commit 982f511c authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Make Border more general (#12619)

parent f2d3b99b
......@@ -36,6 +36,7 @@ export 'src/painting/gradient.dart';
export 'src/painting/images.dart';
export 'src/painting/matrix_utils.dart';
export 'src/painting/rounded_rectangle_border.dart';
export 'src/painting/shape_decoration.dart';
export 'src/painting/text_painter.dart';
export 'src/painting/text_span.dart';
export 'src/painting/text_style.dart';
......@@ -57,8 +57,26 @@ abstract class BoxBorder extends ShapeBorder {
/// const constructors so that they can be used in const expressions.
const BoxBorder();
/// The top side of this border.
///
/// This getter is available on both [Border] and [BorderDirectional]. If
/// [isUniform] is true, then this is the same style as all the other sides.
BorderSide get top;
/// The bottom side of this border.
BorderSide get bottom;
/// Whether all four sides of the border are identical. Uniform borders are
/// typically more efficient to paint.
///
/// A uniform border by definition has no text direction dependency and
/// therefore could be expressed as a [Border], even if it is currently a
/// [BorderDirectional]. A uniform border can also be expressed as a
/// [RoundedRectangleBorder].
bool get isUniform;
// We override this to tighten the return value, so that callers can assume
// that we'll return a BoxBorder.
// that we'll return a [BoxBorder].
@override
BoxBorder add(ShapeBorder other, { bool reversed: false }) => null;
......@@ -315,13 +333,13 @@ class Border extends BoxBorder {
);
}
/// The top side of this border.
@override
final BorderSide top;
/// The right side of this border.
final BorderSide right;
/// The bottom side of this border.
@override
final BorderSide bottom;
/// The left side of this border.
......@@ -332,8 +350,7 @@ class Border extends BoxBorder {
return new EdgeInsets.fromLTRB(left.width, top.width, right.width, bottom.width);
}
/// Whether all four sides of the border are identical. Uniform borders are
/// typically more efficient to paint.
@override
bool get isUniform {
final Color topColor = top.color;
if (right.color != topColor ||
......@@ -572,7 +589,7 @@ class BorderDirectional extends BoxBorder {
);
}
/// The top side of this border.
@override
final BorderSide top;
/// The start side of this border.
......@@ -595,7 +612,7 @@ class BorderDirectional extends BoxBorder {
/// * [TextDirection], which is used to describe the reading direction.
final BorderSide end;
/// The bottom side of this border.
@override
final BorderSide bottom;
@override
......@@ -603,8 +620,7 @@ class BorderDirectional extends BoxBorder {
return new EdgeInsetsDirectional.fromSTEB(start.width, top.width, end.width, bottom.width);
}
/// Whether all four sides of the border are identical. Uniform borders are
/// typically more efficient to paint.
@override
bool get isUniform {
final Color topColor = top.color;
if (start.color != topColor ||
......
......@@ -206,7 +206,7 @@ class BoxDecoration extends Decoration {
/// * [Decoration.lerp], which can interpolate between any two types of
/// [Decoration]s, not just [BoxDecoration]s.
/// * [lerpFrom] and [lerpTo], which are used to implement [Decoration.lerp]
/// and which use [BoxDecoration.lerp] when interpolating two
/// and which use [BoxDecoration.lerp] when interpolating two
/// [BoxDecoration]s or a [BoxDecoration] to or from null.
static BoxDecoration lerp(BoxDecoration a, BoxDecoration b, double t) {
if (a == null && b == null)
......
......@@ -25,7 +25,7 @@ class CircleBorder extends ShapeBorder {
/// Create a circle border.
///
/// The [side] argument must not be null.
const CircleBorder(this.side) : assert(side != null);
const CircleBorder([ this.side = BorderSide.none ]) : assert(side != null);
/// The style of this border.
final BorderSide side;
......
......@@ -28,7 +28,7 @@ class RoundedRectangleBorder extends ShapeBorder {
/// Creates a rounded rectangle border.
///
/// The arguments must not be null.
RoundedRectangleBorder({
const RoundedRectangleBorder({
this.side: BorderSide.none,
this.borderRadius: BorderRadius.zero,
}) : assert(side != null),
......
This diff is collapsed.
// Copyright 2017 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/painting.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('ShapeDecoration constructor', () {
final Color colorR = const Color(0xffff0000);
final Color colorG = const Color(0xff00ff00);
final Gradient gradient = new LinearGradient(colors: <Color>[colorR, colorG]);
expect(const ShapeDecoration(shape: const Border()), const ShapeDecoration(shape: const Border()));
expect(() => new ShapeDecoration(color: colorR, gradient: gradient, shape: const Border()), throwsAssertionError);
expect(() => new ShapeDecoration(color: colorR, shape: null), throwsAssertionError);
expect(
new ShapeDecoration.fromBoxDecoration(const BoxDecoration(shape: BoxShape.circle)),
const ShapeDecoration(shape: const CircleBorder(BorderSide.none)),
);
expect(
new ShapeDecoration.fromBoxDecoration(new BoxDecoration(shape: BoxShape.rectangle, borderRadius: new BorderRadiusDirectional.circular(100.0))),
new ShapeDecoration(shape: new RoundedRectangleBorder(borderRadius: new BorderRadiusDirectional.circular(100.0))),
);
expect(
new ShapeDecoration.fromBoxDecoration(new BoxDecoration(shape: BoxShape.circle, border: new Border.all(color: colorG))),
new ShapeDecoration(shape: new CircleBorder(new BorderSide(color: colorG))),
);
expect(
new ShapeDecoration.fromBoxDecoration(new BoxDecoration(shape: BoxShape.rectangle, border: new Border.all(color: colorR))),
new ShapeDecoration(shape: new Border.all(color: colorR)),
);
expect(
new ShapeDecoration.fromBoxDecoration(const BoxDecoration(shape: BoxShape.rectangle, border: const BorderDirectional(start: const BorderSide()))),
const ShapeDecoration(shape: const BorderDirectional(start: const BorderSide())),
);
});
test('ShapeDecoration.lerp and hit test', () {
final Decoration a = const ShapeDecoration(shape: const CircleBorder());
final Decoration b = const ShapeDecoration(shape: const RoundedRectangleBorder());
expect(Decoration.lerp(a, b, 0.0), a);
expect(Decoration.lerp(a, b, 1.0), b);
const Size size = const Size(200.0, 100.0); // at t=0.5, width will be 150 (x=25 to x=175).
expect(a.hitTest(size, const Offset(20.0, 50.0)), isFalse);
expect(Decoration.lerp(a, b, 0.1).hitTest(size, const Offset(20.0, 50.0)), isFalse);
expect(Decoration.lerp(a, b, 0.5).hitTest(size, const Offset(20.0, 50.0)), isFalse);
expect(Decoration.lerp(a, b, 0.9).hitTest(size, const Offset(20.0, 50.0)), isTrue);
expect(b.hitTest(size, const Offset(20.0, 50.0)), isTrue);
});
}
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