edge_insets.dart 27.9 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5
import 'dart:ui' as ui show lerpDouble, WindowPadding;
6

7 8
import 'package:flutter/foundation.dart';

9
import 'basic_types.dart';
10

11 12 13 14
/// Base class for [EdgeInsets] that allows for text-direction aware
/// resolution.
///
/// A property or argument of this type accepts classes created either with [new
15 16
/// EdgeInsets.fromLTRB] and its variants, or [new
/// EdgeInsetsDirectional.fromSTEB] and its variants.
17
///
18
/// To convert an [EdgeInsetsGeometry] object of indeterminate type into a
19 20 21 22 23
/// [EdgeInsets] object, call the [resolve] method.
///
/// See also:
///
///  * [Padding], a widget that describes margins using [EdgeInsetsGeometry].
24
@immutable
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
abstract class EdgeInsetsGeometry {
  /// Abstract const constructor. This constructor enables subclasses to provide
  /// const constructors so that they can be used in const expressions.
  const EdgeInsetsGeometry();

  double get _bottom;
  double get _end;
  double get _left;
  double get _right;
  double get _start;
  double get _top;

  /// Whether every dimension is non-negative.
  bool get isNonNegative {
    return _left >= 0.0
        && _right >= 0.0
        && _start >= 0.0
        && _end >= 0.0
        && _top >= 0.0
        && _bottom >= 0.0;
  }

47
  /// The total offset in the horizontal direction.
48
  double get horizontal => _left + _right + _start + _end;
49

50
  /// The total offset in the vertical direction.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  double get vertical => _top + _bottom;

  /// The total offset in the given direction.
  double along(Axis axis) {
    assert(axis != null);
    switch (axis) {
      case Axis.horizontal:
        return horizontal;
      case Axis.vertical:
        return vertical;
    }
    return null;
  }

  /// The size that this [EdgeInsets] would occupy with an empty interior.
66
  Size get collapsedSize => Size(horizontal, vertical);
67 68

  /// An [EdgeInsetsGeometry] with top and bottom, left and right, and start and end flipped.
69
  EdgeInsetsGeometry get flipped => _MixedEdgeInsets.fromLRSETB(_right, _left, _end, _start, _bottom, _top);
70 71 72 73 74 75 76 77

  /// Returns a new size that is bigger than the given size by the amount of
  /// inset in the horizontal and vertical directions.
  ///
  /// See also:
  ///
  ///  * [EdgeInsets.inflateRect], to inflate a [Rect] rather than a [Size] (for
  ///    [EdgeInsetsDirectional], requires first calling [resolve] to establish
78
  ///    how the start and end map to the left or right).
79 80
  ///  * [deflateSize], to deflate a [Size] rather than inflating it.
  Size inflateSize(Size size) {
81
    return Size(size.width + horizontal, size.height + vertical);
82 83 84 85 86 87 88 89 90 91 92 93
  }

  /// Returns a new size that is smaller than the given size by the amount of
  /// inset in the horizontal and vertical directions.
  ///
  /// If the argument is smaller than [collapsedSize], then the resulting size
  /// will have negative dimensions.
  ///
  /// See also:
  ///
  ///  * [EdgeInsets.deflateRect], to deflate a [Rect] rather than a [Size]. (for
  ///    [EdgeInsetsDirectional], requires first calling [resolve] to establish
94
  ///    how the start and end map to the left or right).
95 96
  ///  * [inflateSize], to inflate a [Size] rather than deflating it.
  Size deflateSize(Size size) {
97
    return Size(size.width - horizontal, size.height - vertical);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  }

  /// Returns the difference between two [EdgeInsetsGeometry] objects.
  ///
  /// If you know you are applying this to two [EdgeInsets] or two
  /// [EdgeInsetsDirectional] objects, consider using the binary infix `-`
  /// operator instead, which always returns an object of the same type as the
  /// operands, and is typed accordingly.
  ///
  /// If [subtract] is applied to two objects of the same type ([EdgeInsets] or
  /// [EdgeInsetsDirectional]), an object of that type will be returned (though
  /// this is not reflected in the type system). Otherwise, an object
  /// representing a combination of both is returned. That object can be turned
  /// into a concrete [EdgeInsets] using [resolve].
  ///
  /// This method returns the same result as [add] applied to the result of
  /// negating the argument (using the prefix unary `-` operator or multiplying
  /// the argument by -1.0 using the `*` operator).
  EdgeInsetsGeometry subtract(EdgeInsetsGeometry other) {
117
    return _MixedEdgeInsets.fromLRSETB(
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
      _left - other._left,
      _right - other._right,
      _start - other._start,
      _end - other._end,
      _top - other._top,
      _bottom - other._bottom,
    );
  }

  /// Returns the sum of two [EdgeInsetsGeometry] objects.
  ///
  /// If you know you are adding two [EdgeInsets] or two [EdgeInsetsDirectional]
  /// objects, consider using the `+` operator instead, which always returns an
  /// object of the same type as the operands, and is typed accordingly.
  ///
  /// If [add] is applied to two objects of the same type ([EdgeInsets] or
  /// [EdgeInsetsDirectional]), an object of that type will be returned (though
  /// this is not reflected in the type system). Otherwise, an object
  /// representing a combination of both is returned. That object can be turned
  /// into a concrete [EdgeInsets] using [resolve].
  EdgeInsetsGeometry add(EdgeInsetsGeometry other) {
139
    return _MixedEdgeInsets.fromLRSETB(
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
      _left + other._left,
      _right + other._right,
      _start + other._start,
      _end + other._end,
      _top + other._top,
      _bottom + other._bottom,
    );
  }

  /// Returns the [EdgeInsetsGeometry] object with each dimension negated.
  ///
  /// This is the same as multiplying the object by -1.0.
  ///
  /// This operator returns an object of the same type as the operand.
  EdgeInsetsGeometry operator -();

  /// Scales the [EdgeInsetsGeometry] object in each dimension by the given factor.
  ///
  /// This operator returns an object of the same type as the operand.
  EdgeInsetsGeometry operator *(double other);

  /// Divides the [EdgeInsetsGeometry] object in each dimension by the given factor.
  ///
  /// This operator returns an object of the same type as the operand.
  EdgeInsetsGeometry operator /(double other);

  /// Integer divides the [EdgeInsetsGeometry] object in each dimension by the given factor.
  ///
  /// This operator returns an object of the same type as the operand.
169 170 171
  ///
  /// This operator may have unexpected results when applied to a mixture of
  /// [EdgeInsets] and [EdgeInsetsDirectional] objects.
172 173 174 175 176
  EdgeInsetsGeometry operator ~/(double other);

  /// Computes the remainder in each dimension by the given factor.
  ///
  /// This operator returns an object of the same type as the operand.
177 178 179
  ///
  /// This operator may have unexpected results when applied to a mixture of
  /// [EdgeInsets] and [EdgeInsetsDirectional] objects.
180 181 182 183 184 185 186 187 188 189 190 191
  EdgeInsetsGeometry operator %(double other);

  /// Linearly interpolate between two [EdgeInsetsGeometry] objects.
  ///
  /// If either is null, this function interpolates from [EdgeInsets.zero], and
  /// the result is an object of the same type as the non-null argument.
  ///
  /// If [lerp] is applied to two objects of the same type ([EdgeInsets] or
  /// [EdgeInsetsDirectional]), an object of that type will be returned (though
  /// this is not reflected in the type system). Otherwise, an object
  /// representing a combination of both is returned. That object can be turned
  /// into a concrete [EdgeInsets] using [resolve].
192
  ///
193
  /// {@macro dart.ui.shadow.lerp}
194
  static EdgeInsetsGeometry lerp(EdgeInsetsGeometry a, EdgeInsetsGeometry b, double t) {
195
    assert(t != null);
196 197 198 199 200 201 202 203 204 205
    if (a == null && b == null)
      return null;
    if (a == null)
      return b * t;
    if (b == null)
      return a * (1.0 - t);
    if (a is EdgeInsets && b is EdgeInsets)
      return EdgeInsets.lerp(a, b, t);
    if (a is EdgeInsetsDirectional && b is EdgeInsetsDirectional)
      return EdgeInsetsDirectional.lerp(a, b, t);
206
    return _MixedEdgeInsets.fromLRSETB(
207 208 209 210 211
      ui.lerpDouble(a._left, b._left, t),
      ui.lerpDouble(a._right, b._right, t),
      ui.lerpDouble(a._start, b._start, t),
      ui.lerpDouble(a._end, b._end, t),
      ui.lerpDouble(a._top, b._top, t),
212
      ui.lerpDouble(a._bottom, b._bottom, t),
213 214 215
    );
  }

216
  /// Convert this instance into an [EdgeInsets], which uses literal coordinates
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  /// (i.e. the `left` coordinate being explicitly a distance from the left, and
  /// the `right` coordinate being explicitly a distance from the right).
  ///
  /// See also:
  ///
  ///  * [EdgeInsets], for which this is a no-op (returns itself).
  ///  * [EdgeInsetsDirectional], which flips the horizontal direction
  ///    based on the `direction` argument.
  EdgeInsets resolve(TextDirection direction);

  @override
  String toString() {
    if (_start == 0.0 && _end == 0.0) {
      if (_left == 0.0 && _right == 0.0 && _top == 0.0 && _bottom == 0.0)
        return 'EdgeInsets.zero';
      if (_left == _right && _right == _top && _top == _bottom)
        return 'EdgeInsets.all(${_left.toStringAsFixed(1)})';
      return 'EdgeInsets(${_left.toStringAsFixed(1)}, '
                        '${_top.toStringAsFixed(1)}, '
                        '${_right.toStringAsFixed(1)}, '
                        '${_bottom.toStringAsFixed(1)})';
    }
    if (_left == 0.0 && _right == 0.0) {
      return 'EdgeInsetsDirectional(${_start.toStringAsFixed(1)}, '
                                   '${_top.toStringAsFixed(1)}, '
                                   '${_end.toStringAsFixed(1)}, '
                                   '${_bottom.toStringAsFixed(1)})';
    }
    return 'EdgeInsets(${_left.toStringAsFixed(1)}, '
                      '${_top.toStringAsFixed(1)}, '
                      '${_right.toStringAsFixed(1)}, '
                      '${_bottom.toStringAsFixed(1)})'
           ' + '
           'EdgeInsetsDirectional(${_start.toStringAsFixed(1)}, '
                                 '0.0, '
                                 '${_end.toStringAsFixed(1)}, '
                                 '0.0)';
  }

  @override
  bool operator ==(dynamic other) {
    if (other is! EdgeInsetsGeometry)
      return false;
    final EdgeInsetsGeometry typedOther = other;
    return _left == typedOther._left
        && _right == typedOther._right
        && _start == typedOther._start
        && _end == typedOther._end
        && _top == typedOther._top
        && _bottom == typedOther._bottom;
  }

  @override
  int get hashCode => hashValues(_left, _right, _start, _end, _top, _bottom);
271 272
}

273 274 275 276
/// An immutable set of offsets in each of the four cardinal directions.
///
/// Typically used for an offset from each of the four sides of a box. For
/// example, the padding inside a box can be represented using this class.
277
///
278 279 280 281 282 283 284
/// The [EdgeInsets] class specifies offsets in terms of visual edges, left,
/// top, right, and bottom. These values are not affected by the
/// [TextDirection]. To support both left-to-right and right-to-left layouts,
/// consider using [EdgeInsetsDirectional], which is expressed in terms of
/// _start_, top, _end_, and bottom, where start and end are resolved in terms
/// of a [TextDirection] (typically obtained from the ambient [Directionality]).
///
285
/// {@tool sample}
286 287 288
///
/// Here are some examples of how to create [EdgeInsets] instances:
///
289 290
/// Typical eight-pixel margin on all sides:
///
291 292
/// ```dart
/// const EdgeInsets.all(8.0)
293
/// ```
294 295
/// {@end-tool}
/// {@tool sample}
296 297
///
/// Eight pixel margin above and below, no horizontal margins:
298
///
299
/// ```dart
300
/// const EdgeInsets.symmetric(vertical: 8.0)
301
/// ```
302 303
/// {@end-tool}
/// {@tool sample}
304
///
305 306 307
/// Left margin indent of 40 pixels:
///
/// ```dart
308 309
/// const EdgeInsets.only(left: 40.0)
/// ```
310
/// {@end-tool}
311 312 313
///
/// See also:
///
314 315 316 317 318
///  * [Padding], a widget that accepts [EdgeInsets] to describe its margins.
///  * [EdgeInsetsDirectional], which (for properties and arguments that accept
///    the type [EdgeInsetsGeometry]) allows the horizontal insets to be
///    specified in a [TextDirection]-aware manner.
class EdgeInsets extends EdgeInsetsGeometry {
319
  /// Creates insets from offsets from the left, top, right, and bottom.
320 321
  const EdgeInsets.fromLTRB(this.left, this.top, this.right, this.bottom);

322
  /// Creates insets where all the offsets are `value`.
323
  ///
324
  /// {@tool sample}
325
  ///
326 327
  /// Typical eight-pixel margin on all sides:
  ///
328 329 330
  /// ```dart
  /// const EdgeInsets.all(8.0)
  /// ```
331
  /// {@end-tool}
332
  const EdgeInsets.all(double value)
333 334 335 336
    : left = value,
      top = value,
      right = value,
      bottom = value;
337

338
  /// Creates insets with only the given values non-zero.
339
  ///
340
  /// {@tool sample}
341
  ///
342 343
  /// Left margin indent of 40 pixels:
  ///
344 345 346
  /// ```dart
  /// const EdgeInsets.only(left: 40.0)
  /// ```
347
  /// {@end-tool}
348
  const EdgeInsets.only({
349 350 351
    this.left = 0.0,
    this.top = 0.0,
    this.right = 0.0,
352
    this.bottom = 0.0,
353
  });
354

355
  /// Creates insets with symmetrical vertical and horizontal offsets.
356
  ///
357
  /// {@tool sample}
358
  ///
359 360
  /// Eight pixel margin above and below, no horizontal margins:
  ///
361 362 363
  /// ```dart
  /// const EdgeInsets.symmetric(vertical: 8.0)
  /// ```
364
  /// {@end-tool}
365 366 367 368 369 370 371
  const EdgeInsets.symmetric({
    double vertical = 0.0,
    double horizontal = 0.0,
  }) : left = horizontal,
       top = vertical,
       right = horizontal,
       bottom = vertical;
372

373
  /// Creates insets that match the given window padding.
374
  ///
375 376 377
  /// If you need the current system padding or view insets in the context of a
  /// widget, consider using [MediaQuery.of] to obtain these values rather than
  /// using the value from [dart:ui.window], so that you get notified of
378
  /// changes.
379 380 381 382 383
  EdgeInsets.fromWindowPadding(ui.WindowPadding padding, double devicePixelRatio)
    : left = padding.left / devicePixelRatio,
      top = padding.top / devicePixelRatio,
      right = padding.right / devicePixelRatio,
      bottom = padding.bottom / devicePixelRatio;
384

385
  /// An [EdgeInsets] with zero offsets in each direction.
386
  static const EdgeInsets zero = EdgeInsets.only();
387

388 389
  /// The offset from the left.
  final double left;
390

391 392 393
  @override
  double get _left => left;

394 395 396
  /// The offset from the top.
  final double top;

397 398 399
  @override
  double get _top => top;

400 401 402
  /// The offset from the right.
  final double right;

403 404 405
  @override
  double get _right => right;

406 407 408
  /// The offset from the bottom.
  final double bottom;

409 410 411 412 413 414 415 416 417
  @override
  double get _bottom => bottom;

  @override
  double get _start => 0.0;

  @override
  double get _end => 0.0;

418 419
  /// An Offset describing the vector from the top left of a rectangle to the
  /// top left of that rectangle inset by this object.
420
  Offset get topLeft => Offset(left, top);
421 422 423

  /// An Offset describing the vector from the top right of a rectangle to the
  /// top right of that rectangle inset by this object.
424
  Offset get topRight => Offset(-right, top);
425 426 427

  /// An Offset describing the vector from the bottom left of a rectangle to the
  /// bottom left of that rectangle inset by this object.
428
  Offset get bottomLeft => Offset(left, -bottom);
429 430 431

  /// An Offset describing the vector from the bottom right of a rectangle to the
  /// bottom right of that rectangle inset by this object.
432
  Offset get bottomRight => Offset(-right, -bottom);
433

434 435
  /// An [EdgeInsets] with top and bottom as well as left and right flipped.
  @override
436
  EdgeInsets get flipped => EdgeInsets.fromLTRB(right, bottom, left, top);
437

438 439 440 441 442
  /// Returns a new rect that is bigger than the given rect in each direction by
  /// the amount of inset in each direction. Specifically, the left edge of the
  /// rect is moved left by [left], the top edge of the rect is moved up by
  /// [top], the right edge of the rect is moved right by [right], and the
  /// bottom edge of the rect is moved down by [bottom].
443 444 445 446 447
  ///
  /// See also:
  ///
  ///  * [inflateSize], to inflate a [Size] rather than a [Rect].
  ///  * [deflateRect], to deflate a [Rect] rather than inflating it.
448
  Rect inflateRect(Rect rect) {
449
    return Rect.fromLTRB(rect.left - left, rect.top - top, rect.right + right, rect.bottom + bottom);
450 451
  }

452 453 454 455 456 457 458 459 460 461 462 463 464 465
  /// Returns a new rect that is smaller than the given rect in each direction by
  /// the amount of inset in each direction. Specifically, the left edge of the
  /// rect is moved right by [left], the top edge of the rect is moved down by
  /// [top], the right edge of the rect is moved left by [right], and the
  /// bottom edge of the rect is moved up by [bottom].
  ///
  /// If the argument's [Rect.size] is smaller than [collapsedSize], then the
  /// resulting rectangle will have negative dimensions.
  ///
  /// See also:
  ///
  ///  * [deflateSize], to deflate a [Size] rather than a [Rect].
  ///  * [inflateRect], to inflate a [Rect] rather than deflating it.
  Rect deflateRect(Rect rect) {
466
    return Rect.fromLTRB(rect.left + left, rect.top + top, rect.right - right, rect.bottom - bottom);
467 468
  }

469 470 471 472 473
  @override
  EdgeInsetsGeometry subtract(EdgeInsetsGeometry other) {
    if (other is EdgeInsets)
      return this - other;
    return super.subtract(other);
474 475
  }

476 477 478 479 480
  @override
  EdgeInsetsGeometry add(EdgeInsetsGeometry other) {
    if (other is EdgeInsets)
      return this + other;
    return super.add(other);
481 482
  }

483
  /// Returns the difference between two [EdgeInsets].
484
  EdgeInsets operator -(EdgeInsets other) {
485
    return EdgeInsets.fromLTRB(
486
      left - other.left,
487 488
      top - other.top,
      right - other.right,
489
      bottom - other.bottom,
490 491 492
    );
  }

493
  /// Returns the sum of two [EdgeInsets].
494
  EdgeInsets operator +(EdgeInsets other) {
495
    return EdgeInsets.fromLTRB(
496
      left + other.left,
497 498
      top + other.top,
      right + other.right,
499
      bottom + other.bottom,
500 501 502
    );
  }

503 504 505 506 507
  /// Returns the [EdgeInsets] object with each dimension negated.
  ///
  /// This is the same as multiplying the object by -1.0.
  @override
  EdgeInsets operator -() {
508
    return EdgeInsets.fromLTRB(
509 510 511 512 513 514 515 516 517
      -left,
      -top,
      -right,
      -bottom,
    );
  }

  /// Scales the [EdgeInsets] in each dimension by the given factor.
  @override
518
  EdgeInsets operator *(double other) {
519
    return EdgeInsets.fromLTRB(
520
      left * other,
521 522
      top * other,
      right * other,
523
      bottom * other,
524 525 526
    );
  }

527 528
  /// Divides the [EdgeInsets] in each dimension by the given factor.
  @override
529
  EdgeInsets operator /(double other) {
530
    return EdgeInsets.fromLTRB(
531
      left / other,
532 533
      top / other,
      right / other,
534
      bottom / other,
535 536 537
    );
  }

538 539
  /// Integer divides the [EdgeInsets] in each dimension by the given factor.
  @override
540
  EdgeInsets operator ~/(double other) {
541
    return EdgeInsets.fromLTRB(
542
      (left ~/ other).toDouble(),
543 544
      (top ~/ other).toDouble(),
      (right ~/ other).toDouble(),
545
      (bottom ~/ other).toDouble(),
546 547 548
    );
  }

549
  /// Computes the remainder in each dimension by the given factor.
550
  @override
551
  EdgeInsets operator %(double other) {
552
    return EdgeInsets.fromLTRB(
553
      left % other,
554 555
      top % other,
      right % other,
556
      bottom % other,
557 558 559
    );
  }

560
  /// Linearly interpolate between two [EdgeInsets].
561
  ///
562
  /// If either is null, this function interpolates from [EdgeInsets.zero].
563
  ///
564
  /// {@macro dart.ui.shadow.lerp}
565
  static EdgeInsets lerp(EdgeInsets a, EdgeInsets b, double t) {
566
    assert(t != null);
567 568 569 570 571 572
    if (a == null && b == null)
      return null;
    if (a == null)
      return b * t;
    if (b == null)
      return a * (1.0 - t);
573
    return EdgeInsets.fromLTRB(
574
      ui.lerpDouble(a.left, b.left, t),
575 576
      ui.lerpDouble(a.top, b.top, t),
      ui.lerpDouble(a.right, b.right, t),
577
      ui.lerpDouble(a.bottom, b.bottom, t),
578 579 580
    );
  }

581
  @override
582
  EdgeInsets resolve(TextDirection direction) => this;
583 584 585 586 587 588 589 590

  /// Creates a copy of this EdgeInsets but with the given fields replaced
  /// with the new values.
  EdgeInsets copyWith({
    double left,
    double top,
    double right,
    double bottom,
591
  }) {
592
    return EdgeInsets.only(
593 594 595 596 597 598
      left: left ?? this.left,
      top: top ?? this.top,
      right: right ?? this.right,
      bottom: bottom ?? this.bottom,
    );
  }
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
}

/// An immutable set of offsets in each of the four cardinal directions, but
/// whose horizontal components are dependent on the writing direction.
///
/// This can be used to indicate padding from the left in [TextDirection.ltr]
/// text and padding from the right in [TextDirection.rtl] text without having
/// to be aware of the current text direction.
///
/// See also:
///
///  * [EdgeInsets], a variant that uses physical labels (left and right instead
///    of start and end).
class EdgeInsetsDirectional extends EdgeInsetsGeometry {
  /// Creates insets from offsets from the start, top, end, and bottom.
  const EdgeInsetsDirectional.fromSTEB(this.start, this.top, this.end, this.bottom);

  /// Creates insets with only the given values non-zero.
  ///
618
  /// {@tool sample}
619 620 621 622 623 624
  ///
  /// A margin indent of 40 pixels on the leading side:
  ///
  /// ```dart
  /// const EdgeInsetsDirectional.only(start: 40.0)
  /// ```
625
  /// {@end-tool}
626
  const EdgeInsetsDirectional.only({
627 628 629
    this.start = 0.0,
    this.top = 0.0,
    this.end = 0.0,
630
    this.bottom = 0.0,
631 632
  });

633 634 635 636
  /// An [EdgeInsetsDirectional] with zero offsets in each direction.
  ///
  /// Consider using [EdgeInsets.zero] instead, since that object has the same
  /// effect, but will be cheaper to [resolve].
637
  static const EdgeInsetsDirectional zero = EdgeInsetsDirectional.only();
638

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
  /// The offset from the start side, the side from which the user will start
  /// reading text.
  ///
  /// This value is normalized into an [EdgeInsets.left] or [EdgeInsets.right]
  /// value by the [resolve] method.
  final double start;

  @override
  double get _start => start;

  /// The offset from the top.
  ///
  /// This value is passed through to [EdgeInsets.top] unmodified by the
  /// [resolve] method.
  final double top;

  @override
  double get _top => top;

  /// The offset from the end side, the side on which the user ends reading
  /// text.
  ///
  /// This value is normalized into an [EdgeInsets.left] or [EdgeInsets.right]
  /// value by the [resolve] method.
  final double end;

  @override
  double get _end => end;

  /// The offset from the bottom.
  ///
  /// This value is passed through to [EdgeInsets.bottom] unmodified by the
  /// [resolve] method.
  final double bottom;

  @override
  double get _bottom => bottom;

  @override
  double get _left => 0.0;

  @override
  double get _right => 0.0;

  @override
  bool get isNonNegative => start >= 0.0 && top >= 0.0 && end >= 0.0 && bottom >= 0.0;

  /// An [EdgeInsetsDirectional] with [top] and [bottom] as well as [start] and [end] flipped.
  @override
688
  EdgeInsetsDirectional get flipped => EdgeInsetsDirectional.fromSTEB(end, bottom, start, top);
689 690 691 692 693 694

  @override
  EdgeInsetsGeometry subtract(EdgeInsetsGeometry other) {
    if (other is EdgeInsetsDirectional)
      return this - other;
    return super.subtract(other);
695 696
  }

697
  @override
698 699 700 701 702 703 704 705
  EdgeInsetsGeometry add(EdgeInsetsGeometry other) {
    if (other is EdgeInsetsDirectional)
      return this + other;
    return super.add(other);
  }

  /// Returns the difference between two [EdgeInsetsDirectional] objects.
  EdgeInsetsDirectional operator -(EdgeInsetsDirectional other) {
706
    return EdgeInsetsDirectional.fromSTEB(
707 708 709 710 711 712 713 714 715
      start - other.start,
      top - other.top,
      end - other.end,
      bottom - other.bottom,
    );
  }

  /// Returns the sum of two [EdgeInsetsDirectional] objects.
  EdgeInsetsDirectional operator +(EdgeInsetsDirectional other) {
716
    return EdgeInsetsDirectional.fromSTEB(
717 718 719 720 721 722
      start + other.start,
      top + other.top,
      end + other.end,
      bottom + other.bottom,
    );
  }
723

724 725 726 727 728
  /// Returns the [EdgeInsetsDirectional] object with each dimension negated.
  ///
  /// This is the same as multiplying the object by -1.0.
  @override
  EdgeInsetsDirectional operator -() {
729
    return EdgeInsetsDirectional.fromSTEB(
730 731 732 733 734 735 736 737
      -start,
      -top,
      -end,
      -bottom,
    );
  }

  /// Scales the [EdgeInsetsDirectional] object in each dimension by the given factor.
738
  @override
739
  EdgeInsetsDirectional operator *(double other) {
740
    return EdgeInsetsDirectional.fromSTEB(
741 742 743 744 745 746 747 748 749 750
      start * other,
      top * other,
      end * other,
      bottom * other,
    );
  }

  /// Divides the [EdgeInsetsDirectional] object in each dimension by the given factor.
  @override
  EdgeInsetsDirectional operator /(double other) {
751
    return EdgeInsetsDirectional.fromSTEB(
752 753 754 755 756 757 758 759 760 761
      start / other,
      top / other,
      end / other,
      bottom / other,
    );
  }

  /// Integer divides the [EdgeInsetsDirectional] object in each dimension by the given factor.
  @override
  EdgeInsetsDirectional operator ~/(double other) {
762
    return EdgeInsetsDirectional.fromSTEB(
763 764 765 766 767 768 769 770 771 772
      (start ~/ other).toDouble(),
      (top ~/ other).toDouble(),
      (end ~/ other).toDouble(),
      (bottom ~/ other).toDouble(),
    );
  }

  /// Computes the remainder in each dimension by the given factor.
  @override
  EdgeInsetsDirectional operator %(double other) {
773
    return EdgeInsetsDirectional.fromSTEB(
774 775 776 777 778 779 780 781 782 783 784 785 786 787
      start % other,
      top % other,
      end % other,
      bottom % other,
    );
  }

  /// Linearly interpolate between two [EdgeInsetsDirectional].
  ///
  /// If either is null, this function interpolates from [EdgeInsetsDirectional.zero].
  ///
  /// To interpolate between two [EdgeInsetsGeometry] objects of arbitrary type
  /// (either [EdgeInsets] or [EdgeInsetsDirectional]), consider the
  /// [EdgeInsetsGeometry.lerp] static method.
788
  ///
789
  /// {@macro dart.ui.shadow.lerp}
790
  static EdgeInsetsDirectional lerp(EdgeInsetsDirectional a, EdgeInsetsDirectional b, double t) {
791
    assert(t != null);
792 793 794 795 796 797
    if (a == null && b == null)
      return null;
    if (a == null)
      return b * t;
    if (b == null)
      return a * (1.0 - t);
798
    return EdgeInsetsDirectional.fromSTEB(
799 800 801 802 803 804 805 806 807 808 809 810
      ui.lerpDouble(a.start, b.start, t),
      ui.lerpDouble(a.top, b.top, t),
      ui.lerpDouble(a.end, b.end, t),
      ui.lerpDouble(a.bottom, b.bottom, t),
    );
  }

  @override
  EdgeInsets resolve(TextDirection direction) {
    assert(direction != null);
    switch (direction) {
      case TextDirection.rtl:
811
        return EdgeInsets.fromLTRB(end, top, start, bottom);
812
      case TextDirection.ltr:
813
        return EdgeInsets.fromLTRB(start, top, end, bottom);
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
    }
    return null;
  }
}

class _MixedEdgeInsets extends EdgeInsetsGeometry {
  const _MixedEdgeInsets.fromLRSETB(this._left, this._right, this._start, this._end, this._top, this._bottom);

  @override
  final double _left;

  @override
  final double _right;

  @override
  final double _start;

  @override
  final double _end;

  @override
  final double _top;

  @override
  final double _bottom;

  @override
  bool get isNonNegative {
    return _left >= 0.0
        && _right >= 0.0
        && _start >= 0.0
        && _end >= 0.0
        && _top >= 0.0
        && _bottom >= 0.0;
  }

  @override
  _MixedEdgeInsets operator -() {
852
    return _MixedEdgeInsets.fromLRSETB(
853 854 855 856 857 858 859 860 861 862 863
      -_left,
      -_right,
      -_start,
      -_end,
      -_top,
      -_bottom,
    );
  }

  @override
  _MixedEdgeInsets operator *(double other) {
864
    return _MixedEdgeInsets.fromLRSETB(
865 866 867 868 869 870 871 872 873 874 875
      _left * other,
      _right * other,
      _start * other,
      _end * other,
      _top * other,
      _bottom * other,
    );
  }

  @override
  _MixedEdgeInsets operator /(double other) {
876
    return _MixedEdgeInsets.fromLRSETB(
877 878 879 880 881 882 883 884 885 886 887
      _left / other,
      _right / other,
      _start / other,
      _end / other,
      _top / other,
      _bottom / other,
    );
  }

  @override
  _MixedEdgeInsets operator ~/(double other) {
888
    return _MixedEdgeInsets.fromLRSETB(
889 890 891 892 893 894 895 896 897 898 899
      (_left ~/ other).toDouble(),
      (_right ~/ other).toDouble(),
      (_start ~/ other).toDouble(),
      (_end ~/ other).toDouble(),
      (_top ~/ other).toDouble(),
      (_bottom ~/ other).toDouble(),
    );
  }

  @override
  _MixedEdgeInsets operator %(double other) {
900
    return _MixedEdgeInsets.fromLRSETB(
901 902 903 904 905 906 907 908 909 910 911 912 913
      _left % other,
      _right % other,
      _start % other,
      _end % other,
      _top % other,
      _bottom % other,
    );
  }

  @override
  EdgeInsets resolve(TextDirection direction) {
    assert(direction != null);
    switch (direction) {
914
      case TextDirection.rtl:
915
        return EdgeInsets.fromLTRB(_end + _left, _top, _start + _right, _bottom);
916
      case TextDirection.ltr:
917
        return EdgeInsets.fromLTRB(_start + _left, _top, _end + _right, _bottom);
918 919 920
    }
    return null;
  }
921
}