input_border.dart 17.8 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;
import 'dart:ui' show lerpDouble;

import 'package:flutter/widgets.dart';

/// Defines the appearance of an [InputDecorator]'s border.
///
/// An input decorator's border is specified by [InputDecoration.border].
///
/// The border is drawn relative to the input decorator's "container" which
/// is the optionally filled area above the decorator's helper, error,
/// and counter.
///
/// Input border's are decorated with a line whose weight and color are defined
/// by [borderSide]. The input decorator's renderer animates the input border's
/// appearance in response to state changes, like gaining or losing the focus,
/// by creating new copies of its input border with [copyWith].
///
/// See also:
///
///  * [UnderlineInputBorder], the default [InputDecorator] border which
///    draws a horizontal line at the bottom of the input decorator's container.
///  * [OutlineInputBorder], an [InputDecorator] border which draws a
///    rounded rectangle around the input decorator's container.
///  * [InputDecoration], which is used to configure an [InputDecorator].
abstract class InputBorder extends ShapeBorder {
  /// Creates a border for an [InputDecorator].
  ///
  /// The [borderSide] parameter must not be null. Applications typically do
  /// not specify a [borderSide] parameter because the input decorator
  /// substitutes its own, using [copyWith], based on the current theme and
  /// [InputDecorator.isFocused].
  const InputBorder({
38
    this.borderSide = BorderSide.none,
39 40
  }) : assert(borderSide != null);

41 42 43
  /// No input border.
  ///
  /// Use this value with [InputDecoration.border] to specify that no border
44
  /// should be drawn. The [InputDecoration.collapsed] constructor sets
45 46 47
  /// its border to this value.
  static const InputBorder none = _NoInputBorder();

48 49 50 51 52 53 54
  /// Defines the border line's color and weight.
  ///
  /// The [InputDecorator] creates copies of its input border, using [copyWith],
  /// based on the current theme and [InputDecorator.isFocused].
  final BorderSide borderSide;

  /// Creates a copy of this input border with the specified `borderSide`.
55
  InputBorder copyWith({ BorderSide? borderSide });
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

  /// True if this border will enclose the [InputDecorator]'s container.
  ///
  /// This property affects the alignment of container's contents. For example
  /// when an input decorator is configured with an [OutlineInputBorder] its
  /// label is centered with its container.
  bool get isOutline;

  /// Paint this input border on [canvas].
  ///
  /// The [rect] parameter bounds the [InputDecorator]'s container.
  ///
  /// The additional `gap` parameters reflect the state of the [InputDecorator]'s
  /// floating label. When an input decorator gains the focus, its label
  /// animates upwards, to make room for the input child. The [gapStart] and
  /// [gapExtent] parameters define a floating label width interval, and
  /// [gapPercentage] defines the animation's progress (0.0 to 1.0).
  @override
74 75 76
  void paint(
    Canvas canvas,
    Rect rect, {
77
    double? gapStart,
78 79
    double gapExtent = 0.0,
    double gapPercentage = 0.0,
80
    TextDirection? textDirection,
81 82 83
  });
}

84 85 86 87 88
// Used to create the InputBorder.none singleton.
class _NoInputBorder extends InputBorder {
  const _NoInputBorder() : super(borderSide: BorderSide.none);

  @override
89
  _NoInputBorder copyWith({ BorderSide? borderSide }) => const _NoInputBorder();
90 91 92 93 94 95 96 97 98 99 100

  @override
  bool get isOutline => false;

  @override
  EdgeInsetsGeometry get dimensions => EdgeInsets.zero;

  @override
  _NoInputBorder scale(double t) => const _NoInputBorder();

  @override
101
  Path getInnerPath(Rect rect, { TextDirection? textDirection }) {
102
    return Path()..addRect(rect);
103 104 105
  }

  @override
106
  Path getOuterPath(Rect rect, { TextDirection? textDirection }) {
107
    return Path()..addRect(rect);
108 109 110
  }

  @override
111 112 113
  void paint(
    Canvas canvas,
    Rect rect, {
114
    double? gapStart,
115 116
    double gapExtent = 0.0,
    double gapPercentage = 0.0,
117
    TextDirection? textDirection,
118 119 120 121 122
  }) {
    // Do not paint.
  }
}

123 124
/// Draws a horizontal line at the bottom of an [InputDecorator]'s container and
/// defines the container's shape.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
///
/// The input decorator's "container" is the optionally filled area above the
/// decorator's helper, error, and counter.
///
/// See also:
///
///  * [OutlineInputBorder], an [InputDecorator] border which draws a
///    rounded rectangle around the input decorator's container.
///  * [InputDecoration], which is used to configure an [InputDecorator].
class UnderlineInputBorder extends InputBorder {
  /// Creates an underline border for an [InputDecorator].
  ///
  /// The [borderSide] parameter defaults to [BorderSide.none] (it must not be
  /// null). Applications typically do not specify a [borderSide] parameter
  /// because the input decorator substitutes its own, using [copyWith], based
  /// on the current theme and [InputDecorator.isFocused].
141 142 143 144
  ///
  /// The [borderRadius] parameter defaults to a value where the top left
  /// and right corners have a circular radius of 4.0. The [borderRadius]
  /// parameter must not be null.
145
  const UnderlineInputBorder({
146
    BorderSide borderSide = const BorderSide(),
147
    this.borderRadius = const BorderRadius.only(
148 149
      topLeft: Radius.circular(4.0),
      topRight: Radius.circular(4.0),
150 151 152 153 154 155 156 157 158 159 160 161 162 163
    ),
  }) : assert(borderRadius != null),
       super(borderSide: borderSide);

  /// The radii of the border's rounded rectangle corners.
  ///
  /// When this border is used with a filled input decorator, see
  /// [InputDecoration.filled], the border radius defines the shape
  /// of the background fill as well as the bottom left and right
  /// edges of the underline itself.
  ///
  /// By default the top right and top left corners have a circular radius
  /// of 4.0.
  final BorderRadius borderRadius;
164 165 166 167 168

  @override
  bool get isOutline => false;

  @override
169
  UnderlineInputBorder copyWith({ BorderSide? borderSide, BorderRadius? borderRadius }) {
170
    return UnderlineInputBorder(
171 172 173
      borderSide: borderSide ?? this.borderSide,
      borderRadius: borderRadius ?? this.borderRadius,
    );
174 175 176 177
  }

  @override
  EdgeInsetsGeometry get dimensions {
178
    return EdgeInsets.only(bottom: borderSide.width);
179 180 181 182
  }

  @override
  UnderlineInputBorder scale(double t) {
183
    return UnderlineInputBorder(borderSide: borderSide.scale(t));
184 185 186
  }

  @override
187
  Path getInnerPath(Rect rect, { TextDirection? textDirection }) {
188 189
    return Path()
      ..addRect(Rect.fromLTWH(rect.left, rect.top, rect.width, math.max(0.0, rect.height - borderSide.width)));
190 191 192
  }

  @override
193
  Path getOuterPath(Rect rect, { TextDirection? textDirection }) {
194
    return Path()..addRRect(borderRadius.resolve(textDirection).toRRect(rect));
195 196 197
  }

  @override
198
  ShapeBorder? lerpFrom(ShapeBorder? a, double t) {
199
    if (a is UnderlineInputBorder) {
200
      return UnderlineInputBorder(
201
        borderSide: BorderSide.lerp(a.borderSide, borderSide, t),
202
        borderRadius: BorderRadius.lerp(a.borderRadius, borderRadius, t)!,
203 204 205 206 207 208
      );
    }
    return super.lerpFrom(a, t);
  }

  @override
209
  ShapeBorder? lerpTo(ShapeBorder? b, double t) {
210
    if (b is UnderlineInputBorder) {
211
      return UnderlineInputBorder(
212
        borderSide: BorderSide.lerp(borderSide, b.borderSide, t),
213
        borderRadius: BorderRadius.lerp(borderRadius, b.borderRadius, t)!,
214 215 216 217 218 219 220 221 222 223
      );
    }
    return super.lerpTo(b, t);
  }

  /// Draw a horizontal line at the bottom of [rect].
  ///
  /// The [borderSide] defines the line's color and weight. The `textDirection`
  /// `gap` and `textDirection` parameters are ignored.
  @override
224 225 226
  void paint(
    Canvas canvas,
    Rect rect, {
227
    double? gapStart,
228 229
    double gapExtent = 0.0,
    double gapPercentage = 0.0,
230
    TextDirection? textDirection,
231
  }) {
232 233
    if (borderRadius.bottomLeft != Radius.zero || borderRadius.bottomRight != Radius.zero)
      canvas.clipPath(getOuterPath(rect, textDirection: textDirection));
234 235 236 237
    canvas.drawLine(rect.bottomLeft, rect.bottomRight, borderSide.toPaint());
  }

  @override
238
  bool operator ==(Object other) {
239 240
    if (identical(this, other))
      return true;
241
    if (other.runtimeType != runtimeType)
242
      return false;
243 244
    return other is InputBorder
        && other.borderSide == borderSide;
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
  }

  @override
  int get hashCode => borderSide.hashCode;
}

/// Draws a rounded rectangle around an [InputDecorator]'s container.
///
/// When the input decorator's label is floating, for example because its
/// input child has the focus, the label appears in a gap in the border outline.
///
/// The input decorator's "container" is the optionally filled area above the
/// decorator's helper, error, and counter.
///
/// See also:
///
///  * [UnderlineInputBorder], the default [InputDecorator] border which
///    draws a horizontal line at the bottom of the input decorator's container.
///  * [InputDecoration], which is used to configure an [InputDecorator].
class OutlineInputBorder extends InputBorder {
  /// Creates a rounded rectangle outline border for an [InputDecorator].
  ///
267 268 269 270 271 272 273
  /// If the [borderSide] parameter is [BorderSide.none], it will not draw a
  /// border. However, it will still define a shape (which you can see if
  /// [InputDecoration.filled] is true).
  ///
  /// If an application does not specify a [borderSide] parameter of
  /// value [BorderSide.none], the input decorator substitutes its own, using
  /// [copyWith], based on the current theme and [InputDecorator.isFocused].
274
  ///
275 276 277 278
  /// The [borderRadius] parameter defaults to a value where all four
  /// corners have a circular radius of 4.0. The [borderRadius] parameter
  /// must not be null and the corner radii must be circular, i.e. their
  /// [Radius.x] and [Radius.y] values must be the same.
279 280
  ///
  /// See also:
281
  ///
282 283 284
  ///  * [InputDecoration.floatingLabelBehavior], which should be set to
  ///    [FloatingLabelBehavior.never] when the [borderSide] is
  ///    [BorderSide.none]. If let as [FloatingLabelBehavior.auto], the label
285 286
  ///    will extend beyond the container as if the border were still being
  ///    drawn.
287
  const OutlineInputBorder({
288
    BorderSide borderSide = const BorderSide(),
289
    this.borderRadius = const BorderRadius.all(Radius.circular(4.0)),
290
    this.gapPadding = 4.0,
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  }) : assert(borderRadius != null),
       assert(gapPadding != null && gapPadding >= 0.0),
       super(borderSide: borderSide);

  // The label text's gap can extend into the corners (even both the top left
  // and the top right corner). To avoid the more complicated problem of finding
  // how far the gap penetrates into an elliptical corner, just require them
  // to be circular.
  //
  // This can't be checked by the constructor because const constructor.
  static bool _cornersAreCircular(BorderRadius borderRadius) {
    return borderRadius.topLeft.x == borderRadius.topLeft.y
        && borderRadius.bottomLeft.x == borderRadius.bottomLeft.y
        && borderRadius.topRight.x == borderRadius.topRight.y
        && borderRadius.bottomRight.x == borderRadius.bottomRight.y;
  }

  /// Horizontal padding on either side of the border's
  /// [InputDecoration.labelText] width gap.
  ///
  /// This value is used by the [paint] method to compute the actual gap width.
  final double gapPadding;

  /// The radii of the border's rounded rectangle corners.
  ///
  /// The corner radii must be circular, i.e. their [Radius.x] and [Radius.y]
  /// values must be the same.
  final BorderRadius borderRadius;

  @override
  bool get isOutline => true;

  @override
  OutlineInputBorder copyWith({
325 326 327
    BorderSide? borderSide,
    BorderRadius? borderRadius,
    double? gapPadding,
328
  }) {
329
    return OutlineInputBorder(
330 331 332 333 334 335 336 337
      borderSide: borderSide ?? this.borderSide,
      borderRadius: borderRadius ?? this.borderRadius,
      gapPadding: gapPadding ?? this.gapPadding,
    );
  }

  @override
  EdgeInsetsGeometry get dimensions {
338
    return EdgeInsets.all(borderSide.width);
339 340 341 342
  }

  @override
  OutlineInputBorder scale(double t) {
343
    return OutlineInputBorder(
344 345 346 347 348 349 350
      borderSide: borderSide.scale(t),
      borderRadius: borderRadius * t,
      gapPadding: gapPadding * t,
    );
  }

  @override
351
  ShapeBorder? lerpFrom(ShapeBorder? a, double t) {
352 353
    if (a is OutlineInputBorder) {
      final OutlineInputBorder outline = a;
354
      return OutlineInputBorder(
355
        borderRadius: BorderRadius.lerp(outline.borderRadius, borderRadius, t)!,
356 357 358 359 360 361 362 363
        borderSide: BorderSide.lerp(outline.borderSide, borderSide, t),
        gapPadding: outline.gapPadding,
      );
    }
    return super.lerpFrom(a, t);
  }

  @override
364
  ShapeBorder? lerpTo(ShapeBorder? b, double t) {
365 366
    if (b is OutlineInputBorder) {
      final OutlineInputBorder outline = b;
367
      return OutlineInputBorder(
368
        borderRadius: BorderRadius.lerp(borderRadius, outline.borderRadius, t)!,
369 370 371 372 373 374 375 376
        borderSide: BorderSide.lerp(borderSide, outline.borderSide, t),
        gapPadding: outline.gapPadding,
      );
    }
    return super.lerpTo(b, t);
  }

  @override
377
  Path getInnerPath(Rect rect, { TextDirection? textDirection }) {
378
    return Path()
379 380 381 382
      ..addRRect(borderRadius.resolve(textDirection).toRRect(rect).deflate(borderSide.width));
  }

  @override
383
  Path getOuterPath(Rect rect, { TextDirection? textDirection }) {
384
    return Path()
385 386 387 388
      ..addRRect(borderRadius.resolve(textDirection).toRRect(rect));
  }

  Path _gapBorderPath(Canvas canvas, RRect center, double start, double extent) {
389 390 391 392 393
    // When the corner radii on any side add up to be greater than the
    // given height, each radius has to be scaled to not exceed the
    // size of the width/height of the RRect.
    final RRect scaledRRect = center.scaleRadii();

394
    final Rect tlCorner = Rect.fromLTWH(
395 396 397 398
      scaledRRect.left,
      scaledRRect.top,
      scaledRRect.tlRadiusX * 2.0,
      scaledRRect.tlRadiusY * 2.0,
399
    );
400
    final Rect trCorner = Rect.fromLTWH(
401 402 403 404
      scaledRRect.right - scaledRRect.trRadiusX * 2.0,
      scaledRRect.top,
      scaledRRect.trRadiusX * 2.0,
      scaledRRect.trRadiusY * 2.0,
405
    );
406
    final Rect brCorner = Rect.fromLTWH(
407 408 409 410
      scaledRRect.right - scaledRRect.brRadiusX * 2.0,
      scaledRRect.bottom - scaledRRect.brRadiusY * 2.0,
      scaledRRect.brRadiusX * 2.0,
      scaledRRect.brRadiusY * 2.0,
411
    );
412
    final Rect blCorner = Rect.fromLTWH(
413 414 415 416
      scaledRRect.left,
      scaledRRect.bottom - scaledRRect.blRadiusY * 2.0,
      scaledRRect.blRadiusX * 2.0,
      scaledRRect.blRadiusX * 2.0,
417 418
    );

419
    const double cornerArcSweep = math.pi / 2.0;
420 421
    final double tlCornerArcSweep = start < scaledRRect.tlRadiusX
      ? math.asin((start / scaledRRect.tlRadiusX).clamp(-1.0, 1.0))
422
      : math.pi / 2.0;
423

424
    final Path path = Path()
425
      ..addArc(tlCorner, math.pi, tlCornerArcSweep)
426
      ..moveTo(scaledRRect.left + scaledRRect.tlRadiusX, scaledRRect.top);
427

428 429
    if (start > scaledRRect.tlRadiusX)
      path.lineTo(scaledRRect.left + start, scaledRRect.top);
430

431 432
    const double trCornerArcStart = (3 * math.pi) / 2.0;
    const double trCornerArcSweep = cornerArcSweep;
433
    if (start + extent < scaledRRect.width - scaledRRect.trRadiusX) {
434 435
      path
        ..relativeMoveTo(extent, 0.0)
436
        ..lineTo(scaledRRect.right - scaledRRect.trRadiusX, scaledRRect.top)
437
        ..addArc(trCorner, trCornerArcStart, trCornerArcSweep);
438 439 440
    } else if (start + extent < scaledRRect.width) {
      final double dx = scaledRRect.width - (start + extent);
      final double sweep = math.acos(dx / scaledRRect.trRadiusX);
441 442 443 444
      path.addArc(trCorner, trCornerArcStart + sweep, trCornerArcSweep - sweep);
    }

    return path
445 446
      ..moveTo(scaledRRect.right, scaledRRect.top + scaledRRect.trRadiusY)
      ..lineTo(scaledRRect.right, scaledRRect.bottom - scaledRRect.brRadiusY)
447
      ..addArc(brCorner, 0.0, cornerArcSweep)
448
      ..lineTo(scaledRRect.left + scaledRRect.blRadiusX, scaledRRect.bottom)
449
      ..addArc(blCorner, math.pi / 2.0, cornerArcSweep)
450
      ..lineTo(scaledRRect.left, scaledRRect.top + scaledRRect.tlRadiusY);
451 452 453 454 455 456 457 458 459 460 461
  }

  /// Draw a rounded rectangle around [rect] using [borderRadius].
  ///
  /// The [borderSide] defines the line's color and weight.
  ///
  /// The top side of the rounded rectangle may be interrupted by a single gap
  /// if [gapExtent] is non-null. In that case the gap begins at
  /// `gapStart - gapPadding` (assuming that the [textDirection] is [TextDirection.ltr]).
  /// The gap's width is `(gapPadding + gapExtent + gapPadding) * gapPercentage`.
  @override
462 463 464
  void paint(
    Canvas canvas,
    Rect rect, {
465
    double? gapStart,
466 467
    double gapExtent = 0.0,
    double gapPercentage = 0.0,
468
    TextDirection? textDirection,
469 470 471 472 473 474 475 476 477 478 479
  }) {
    assert(gapExtent != null);
    assert(gapPercentage >= 0.0 && gapPercentage <= 1.0);
    assert(_cornersAreCircular(borderRadius));

    final Paint paint = borderSide.toPaint();
    final RRect outer = borderRadius.toRRect(rect);
    final RRect center = outer.deflate(borderSide.width / 2.0);
    if (gapStart == null || gapExtent <= 0.0 || gapPercentage == 0.0) {
      canvas.drawRRect(center, paint);
    } else {
480 481
      final double extent = lerpDouble(0.0, gapExtent + gapPadding * 2.0, gapPercentage)!;
      switch (textDirection!) {
482
        case TextDirection.rtl:
483
          final Path path = _gapBorderPath(canvas, center, math.max(0.0, gapStart + gapPadding - extent), extent);
484 485
          canvas.drawPath(path, paint);
          break;
486 487

        case TextDirection.ltr:
488
          final Path path = _gapBorderPath(canvas, center, math.max(0.0, gapStart - gapPadding), extent);
489 490 491 492 493 494 495
          canvas.drawPath(path, paint);
          break;
      }
    }
  }

  @override
496
  bool operator ==(Object other) {
497 498
    if (identical(this, other))
      return true;
499
    if (other.runtimeType != runtimeType)
500
      return false;
501 502 503 504
    return other is OutlineInputBorder
        && other.borderSide == borderSide
        && other.borderRadius == borderRadius
        && other.gapPadding == gapPadding;
505 506 507 508 509
  }

  @override
  int get hashCode => hashValues(borderSide, borderRadius, gapPadding);
}