table_border.dart 9.61 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart' hide Border;

/// Border specification for [Table] widgets.
///
/// This is like [Border], with the addition of two sides: the inner horizontal
/// borders between rows and the inner vertical borders between columns.
///
/// The sides are represented by [BorderSide] objects.
@immutable
class TableBorder {
  /// Creates a border for a table.
  ///
  /// All the sides of the border default to [BorderSide.none].
  const TableBorder({
20 21 22 23 24 25
    this.top = BorderSide.none,
    this.right = BorderSide.none,
    this.bottom = BorderSide.none,
    this.left = BorderSide.none,
    this.horizontalInside = BorderSide.none,
    this.verticalInside = BorderSide.none,
26
    this.borderRadius = BorderRadius.zero,
27 28 29 30 31 32
  });

  /// A uniform border with all sides the same color and width.
  ///
  /// The sides default to black solid borders, one logical pixel wide.
  factory TableBorder.all({
33 34 35
    Color color = const Color(0xFF000000),
    double width = 1.0,
    BorderStyle style = BorderStyle.solid,
36
    BorderRadius borderRadius = BorderRadius.zero,
37
  }) {
38
    final BorderSide side = BorderSide(color: color, width: width, style: style);
39
    return TableBorder(top: side, right: side, bottom: side, left: side, horizontalInside: side, verticalInside: side, borderRadius: borderRadius);
40 41 42 43 44
  }

  /// Creates a border for a table where all the interior sides use the same
  /// styling and all the exterior sides use the same styling.
  factory TableBorder.symmetric({
45 46
    BorderSide inside = BorderSide.none,
    BorderSide outside = BorderSide.none,
47
  }) {
48
    return TableBorder(
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
      top: outside,
      right: outside,
      bottom: outside,
      left: outside,
      horizontalInside: inside,
      verticalInside: inside,
    );
  }

  /// The top side of this border.
  final BorderSide top;

  /// The right side of this border.
  final BorderSide right;

  /// The bottom side of this border.
  final BorderSide bottom;

  /// The left side of this border.
  final BorderSide left;

  /// The horizontal interior sides of this border.
  final BorderSide horizontalInside;

  /// The vertical interior sides of this border.
  final BorderSide verticalInside;

76
  /// The [BorderRadius] to use when painting the corners of this border.
77 78
  ///
  /// It is also applied to [DataTable]'s [Material].
79 80
  final BorderRadius borderRadius;

81 82 83 84 85
  /// The widths of the sides of this border represented as an [EdgeInsets].
  ///
  /// This can be used, for example, with a [Padding] widget to inset a box by
  /// the size of these borders.
  EdgeInsets get dimensions {
86
    return EdgeInsets.fromLTRB(left.width, top.width, right.width, bottom.width);
87 88
  }

89 90
  /// Whether all the sides of the border (outside and inside) are identical.
  /// Uniform borders are typically more efficient to paint.
91 92 93 94 95 96 97
  bool get isUniform {

    final Color topColor = top.color;
    if (right.color != topColor ||
        bottom.color != topColor ||
        left.color != topColor ||
        horizontalInside.color != topColor ||
98
        verticalInside.color != topColor) {
99
      return false;
100
    }
101 102 103 104 105 106

    final double topWidth = top.width;
    if (right.width != topWidth ||
        bottom.width != topWidth ||
        left.width != topWidth ||
        horizontalInside.width != topWidth ||
107
        verticalInside.width != topWidth) {
108
      return false;
109
    }
110 111 112 113 114 115

    final BorderStyle topStyle = top.style;
    if (right.style != topStyle ||
        bottom.style != topStyle ||
        left.style != topStyle ||
        horizontalInside.style != topStyle ||
116
        verticalInside.style != topStyle) {
117
      return false;
118
    }
119 120 121 122

    return true;
  }

123 124 125 126
  /// Creates a copy of this border but with the widths scaled by the factor `t`.
  ///
  /// The `t` argument represents the multiplicand, or the position on the
  /// timeline for an interpolation from nothing to `this`, with 0.0 meaning
127
  /// that the object returned should be the nil variant of this object, 1.0
128 129 130 131 132 133 134 135 136 137
  /// meaning that no change should be applied, returning `this` (or something
  /// equivalent to `this`), and other values meaning that the object should be
  /// multiplied by `t`. Negative values are treated like zero.
  ///
  /// Values for `t` are usually obtained from an [Animation<double>], such as
  /// an [AnimationController].
  ///
  /// See also:
  ///
  ///  * [BorderSide.scale], which is used to implement this method.
138
  TableBorder scale(double t) {
139
    return TableBorder(
140 141 142 143 144 145
      top: top.scale(t),
      right: right.scale(t),
      bottom: bottom.scale(t),
      left: left.scale(t),
      horizontalInside: horizontalInside.scale(t),
      verticalInside: verticalInside.scale(t),
146 147 148 149 150 151 152
    );
  }

  /// Linearly interpolate between two table borders.
  ///
  /// If a border is null, it is treated as having only [BorderSide.none]
  /// borders.
153
  ///
154
  /// {@macro dart.ui.shadow.lerp}
155
  static TableBorder? lerp(TableBorder? a, TableBorder? b, double t) {
156 157
    if (identical(a, b)) {
      return a;
158 159
    }
    if (a == null) {
160
      return b!.scale(t);
161 162
    }
    if (b == null) {
163
      return a.scale(1.0 - t);
164
    }
165
    return TableBorder(
166 167 168 169 170
      top: BorderSide.lerp(a.top, b.top, t),
      right: BorderSide.lerp(a.right, b.right, t),
      bottom: BorderSide.lerp(a.bottom, b.bottom, t),
      left: BorderSide.lerp(a.left, b.left, t),
      horizontalInside: BorderSide.lerp(a.horizontalInside, b.horizontalInside, t),
171
      verticalInside: BorderSide.lerp(a.verticalInside, b.verticalInside, t),
172 173 174 175 176 177 178 179
    );
  }

  /// Paints the border around the given [Rect] on the given [Canvas], with the
  /// given rows and columns.
  ///
  /// Uniform borders are more efficient to paint than more complex borders.
  ///
180 181 182 183 184
  /// The `rows` argument specifies the vertical positions between the rows,
  /// relative to the given rectangle. For example, if the table contained two
  /// rows of height 100.0 each, then `rows` would contain a single value,
  /// 100.0, which is the vertical position between the two rows (relative to
  /// the top edge of `rect`).
185
  ///
186 187 188 189 190
  /// The `columns` argument specifies the horizontal positions between the
  /// columns, relative to the given rectangle. For example, if the table
  /// contained two columns of height 100.0 each, then `columns` would contain a
  /// single value, 100.0, which is the vertical position between the two
  /// columns (relative to the left edge of `rect`).
191
  ///
192 193 194
  /// The [verticalInside] border is only drawn if there are at least two
  /// columns. The [horizontalInside] border is only drawn if there are at least
  /// two rows. The horizontal borders are drawn after the vertical borders.
195 196
  ///
  /// The outer borders (in the order [top], [right], [bottom], [left], with
197
  /// [left] above the others) are painted after the inner borders.
198 199 200
  ///
  /// The paint order is particularly notable in the case of
  /// partially-transparent borders.
201 202 203
  void paint(
    Canvas canvas,
    Rect rect, {
204 205
    required Iterable<double> rows,
    required Iterable<double> columns,
206 207 208 209
  }) {
    // properties can't be null

    // arguments can't be null
210 211
    assert(rows.isEmpty || (rows.first >= 0.0 && rows.last <= rect.height));
    assert(columns.isEmpty || (columns.first >= 0.0 && columns.last <= rect.width));
212

213
    if (columns.isNotEmpty || rows.isNotEmpty) {
214 215
      final Paint paint = Paint();
      final Path path = Path();
216

217 218 219 220 221 222 223 224
      if (columns.isNotEmpty) {
        switch (verticalInside.style) {
          case BorderStyle.solid:
            paint
              ..color = verticalInside.color
              ..strokeWidth = verticalInside.width
              ..style = PaintingStyle.stroke;
            path.reset();
225
            for (final double x in columns) {
226 227 228 229 230 231
              path.moveTo(rect.left + x, rect.top);
              path.lineTo(rect.left + x, rect.bottom);
            }
            canvas.drawPath(path, paint);
          case BorderStyle.none:
            break;
232
        }
233
      }
234

235 236 237 238 239 240 241 242
      if (rows.isNotEmpty) {
        switch (horizontalInside.style) {
          case BorderStyle.solid:
            paint
              ..color = horizontalInside.color
              ..strokeWidth = horizontalInside.width
              ..style = PaintingStyle.stroke;
            path.reset();
243
            for (final double y in rows) {
244 245 246 247 248 249
              path.moveTo(rect.left, rect.top + y);
              path.lineTo(rect.right, rect.top + y);
            }
            canvas.drawPath(path, paint);
          case BorderStyle.none:
            break;
250
        }
251
      }
252
    }
253
    if (!isUniform || borderRadius == BorderRadius.zero) {
254
      paintBorder(canvas, rect, top: top, right: right, bottom: bottom, left: left);
255
    } else {
256
      final RRect outer = borderRadius.toRRect(rect);
257
      final RRect inner = outer.deflate(top.width);
258 259 260
      final Paint paint = Paint()..color = top.color;
      canvas.drawDRRect(outer, inner, paint);
    }
261 262 263
  }

  @override
264
  bool operator ==(Object other) {
265
    if (identical(this, other)) {
266
      return true;
267 268
    }
    if (other.runtimeType != runtimeType) {
269
      return false;
270
    }
271 272 273 274 275 276
    return other is TableBorder
        && other.top == top
        && other.right == right
        && other.bottom == bottom
        && other.left == left
        && other.horizontalInside == horizontalInside
277 278
        && other.verticalInside == verticalInside
        && other.borderRadius == borderRadius;
279 280 281
  }

  @override
282
  int get hashCode => Object.hash(top, right, bottom, left, horizontalInside, verticalInside, borderRadius);
283 284

  @override
285
  String toString() => 'TableBorder($top, $right, $bottom, $left, $horizontalInside, $verticalInside, $borderRadius)';
286
}