edge_insets.dart 30.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// 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 ViewPadding, lerpDouble;
6

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

9
import 'basic_types.dart';
10

11 12 13
/// Base class for [EdgeInsets] that allows for text-direction aware
/// resolution.
///
14 15
/// A property or argument of this type accepts classes created either with [
/// EdgeInsets.fromLTRB] and its variants, or [
16
/// 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
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;

37 38 39 40 41 42 43 44 45 46 47 48
  /// An [EdgeInsetsGeometry] with infinite offsets in each direction.
  ///
  /// Can be used as an infinite upper bound for [clamp].
  static const EdgeInsetsGeometry infinity = _MixedEdgeInsets.fromLRSETB(
    double.infinity,
    double.infinity,
    double.infinity,
    double.infinity,
    double.infinity,
    double.infinity,
  );

49 50 51 52 53 54 55 56 57 58
  /// 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;
  }

59
  /// The total offset in the horizontal direction.
60
  double get horizontal => _left + _right + _start + _end;
61

62
  /// The total offset in the vertical direction.
63 64 65 66 67 68 69 70 71 72 73 74 75
  double get vertical => _top + _bottom;

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

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

  /// An [EdgeInsetsGeometry] with top and bottom, left and right, and start and end flipped.
79
  EdgeInsetsGeometry get flipped => _MixedEdgeInsets.fromLRSETB(_right, _left, _end, _start, _bottom, _top);
80 81 82 83 84 85 86 87

  /// 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
88
  ///    how the start and end map to the left or right).
89 90
  ///  * [deflateSize], to deflate a [Size] rather than inflating it.
  Size inflateSize(Size size) {
91
    return Size(size.width + horizontal, size.height + vertical);
92 93 94 95 96 97 98 99 100 101 102 103
  }

  /// 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
104
  ///    how the start and end map to the left or right).
105 106
  ///  * [inflateSize], to inflate a [Size] rather than deflating it.
  Size deflateSize(Size size) {
107
    return Size(size.width - horizontal, size.height - vertical);
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  }

  /// 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) {
127
    return _MixedEdgeInsets.fromLRSETB(
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
      _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) {
149
    return _MixedEdgeInsets.fromLRSETB(
150 151 152 153 154 155 156 157 158
      _left + other._left,
      _right + other._right,
      _start + other._start,
      _end + other._end,
      _top + other._top,
      _bottom + other._bottom,
    );
  }

nt4f04uNd's avatar
nt4f04uNd committed
159
  /// Returns a new [EdgeInsetsGeometry] object with all values greater than
160 161 162
  /// or equal to `min`, and less than or equal to `max`.
  EdgeInsetsGeometry clamp(EdgeInsetsGeometry min, EdgeInsetsGeometry max) {
    return _MixedEdgeInsets.fromLRSETB(
163 164 165 166 167 168
      clampDouble(_left, min._left, max._left),
      clampDouble(_right, min._right, max._right),
      clampDouble(_start, min._start, max._start),
      clampDouble(_end, min._end, max._end),
      clampDouble(_top, min._top, max._top),
      clampDouble(_bottom, min._bottom, max._bottom),
169 170 171
    );
  }

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  /// 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.
192 193 194
  ///
  /// This operator may have unexpected results when applied to a mixture of
  /// [EdgeInsets] and [EdgeInsetsDirectional] objects.
195 196 197 198 199
  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.
200 201 202
  ///
  /// This operator may have unexpected results when applied to a mixture of
  /// [EdgeInsets] and [EdgeInsetsDirectional] objects.
203 204 205 206 207 208 209 210 211 212 213 214
  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].
215
  ///
216
  /// {@macro dart.ui.shadow.lerp}
217
  static EdgeInsetsGeometry? lerp(EdgeInsetsGeometry? a, EdgeInsetsGeometry? b, double t) {
218 219
    if (identical(a, b)) {
      return a;
220 221
    }
    if (a == null) {
222
      return b! * t;
223 224
    }
    if (b == null) {
225
      return a * (1.0 - t);
226 227
    }
    if (a is EdgeInsets && b is EdgeInsets) {
228
      return EdgeInsets.lerp(a, b, t);
229 230
    }
    if (a is EdgeInsetsDirectional && b is EdgeInsetsDirectional) {
231
      return EdgeInsetsDirectional.lerp(a, b, t);
232
    }
233
    return _MixedEdgeInsets.fromLRSETB(
234 235 236 237 238 239
      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)!,
      ui.lerpDouble(a._bottom, b._bottom, t)!,
240 241 242
    );
  }

243
  /// Convert this instance into an [EdgeInsets], which uses literal coordinates
244 245 246 247 248 249 250 251
  /// (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.
252
  EdgeInsets resolve(TextDirection? direction);
253 254 255 256

  @override
  String toString() {
    if (_start == 0.0 && _end == 0.0) {
257
      if (_left == 0.0 && _right == 0.0 && _top == 0.0 && _bottom == 0.0) {
258
        return 'EdgeInsets.zero';
259 260
      }
      if (_left == _right && _right == _top && _top == _bottom) {
261
        return 'EdgeInsets.all(${_left.toStringAsFixed(1)})';
262
      }
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
      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
286
  bool operator ==(Object other) {
287 288 289 290 291 292 293
    return other is EdgeInsetsGeometry
        && other._left == _left
        && other._right == _right
        && other._start == _start
        && other._end == _end
        && other._top == _top
        && other._bottom == _bottom;
294 295 296
  }

  @override
297
  int get hashCode => Object.hash(_left, _right, _start, _end, _top, _bottom);
298 299
}

300 301 302 303
/// 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.
304
///
305 306 307 308 309 310 311
/// 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]).
///
312
/// {@tool snippet}
313 314 315
///
/// Here are some examples of how to create [EdgeInsets] instances:
///
316 317
/// Typical eight-pixel margin on all sides:
///
318 319
/// ```dart
/// const EdgeInsets.all(8.0)
320
/// ```
321
/// {@end-tool}
322
/// {@tool snippet}
323 324
///
/// Eight pixel margin above and below, no horizontal margins:
325
///
326
/// ```dart
327
/// const EdgeInsets.symmetric(vertical: 8.0)
328
/// ```
329
/// {@end-tool}
330
/// {@tool snippet}
331
///
332 333 334
/// Left margin indent of 40 pixels:
///
/// ```dart
335 336
/// const EdgeInsets.only(left: 40.0)
/// ```
337
/// {@end-tool}
338 339 340
///
/// See also:
///
341 342 343 344 345
///  * [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 {
346
  /// Creates insets from offsets from the left, top, right, and bottom.
347 348
  const EdgeInsets.fromLTRB(this.left, this.top, this.right, this.bottom);

349
  /// Creates insets where all the offsets are `value`.
350
  ///
351
  /// {@tool snippet}
352
  ///
353 354
  /// Typical eight-pixel margin on all sides:
  ///
355 356 357
  /// ```dart
  /// const EdgeInsets.all(8.0)
  /// ```
358
  /// {@end-tool}
359
  const EdgeInsets.all(double value)
360 361 362 363
    : left = value,
      top = value,
      right = value,
      bottom = value;
364

365
  /// Creates insets with only the given values non-zero.
366
  ///
367
  /// {@tool snippet}
368
  ///
369 370
  /// Left margin indent of 40 pixels:
  ///
371 372 373
  /// ```dart
  /// const EdgeInsets.only(left: 40.0)
  /// ```
374
  /// {@end-tool}
375
  const EdgeInsets.only({
376 377 378
    this.left = 0.0,
    this.top = 0.0,
    this.right = 0.0,
379
    this.bottom = 0.0,
380
  });
381

382
  /// Creates insets with symmetrical vertical and horizontal offsets.
383
  ///
384
  /// {@tool snippet}
385
  ///
386 387
  /// Eight pixel margin above and below, no horizontal margins:
  ///
388 389 390
  /// ```dart
  /// const EdgeInsets.symmetric(vertical: 8.0)
  /// ```
391
  /// {@end-tool}
392 393 394 395 396 397 398
  const EdgeInsets.symmetric({
    double vertical = 0.0,
    double horizontal = 0.0,
  }) : left = horizontal,
       top = vertical,
       right = horizontal,
       bottom = vertical;
399

400
  /// Creates insets that match the given view padding.
401
  ///
402
  /// If you need the current system padding or view insets in the context of a
403
  /// widget, consider using [MediaQuery.paddingOf] to obtain these values rather than
404
  /// using the value from a [FlutterView] directly, so that you get notified of
405
  /// changes.
406
  EdgeInsets.fromViewPadding(ui.ViewPadding padding, double devicePixelRatio)
407 408 409 410
    : left = padding.left / devicePixelRatio,
      top = padding.top / devicePixelRatio,
      right = padding.right / devicePixelRatio,
      bottom = padding.bottom / devicePixelRatio;
411

412 413 414 415 416 417 418 419 420
  /// Deprecated. Will be removed in a future version of Flutter.
  ///
  /// Use [EdgeInsets.fromViewPadding] instead.
  @Deprecated(
    'Use EdgeInsets.fromViewPadding instead. '
    'This feature was deprecated after v3.8.0-14.0.pre.',
  )
  factory EdgeInsets.fromWindowPadding(ui.ViewPadding padding, double devicePixelRatio) = EdgeInsets.fromViewPadding;

421
  /// An [EdgeInsets] with zero offsets in each direction.
422
  static const EdgeInsets zero = EdgeInsets.only();
423

424 425
  /// The offset from the left.
  final double left;
426

427 428 429
  @override
  double get _left => left;

430 431 432
  /// The offset from the top.
  final double top;

433 434 435
  @override
  double get _top => top;

436 437 438
  /// The offset from the right.
  final double right;

439 440 441
  @override
  double get _right => right;

442 443 444
  /// The offset from the bottom.
  final double bottom;

445 446 447 448 449 450 451 452 453
  @override
  double get _bottom => bottom;

  @override
  double get _start => 0.0;

  @override
  double get _end => 0.0;

454 455
  /// An Offset describing the vector from the top left of a rectangle to the
  /// top left of that rectangle inset by this object.
456
  Offset get topLeft => Offset(left, top);
457 458 459

  /// An Offset describing the vector from the top right of a rectangle to the
  /// top right of that rectangle inset by this object.
460
  Offset get topRight => Offset(-right, top);
461 462 463

  /// An Offset describing the vector from the bottom left of a rectangle to the
  /// bottom left of that rectangle inset by this object.
464
  Offset get bottomLeft => Offset(left, -bottom);
465 466 467

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

470 471
  /// An [EdgeInsets] with top and bottom as well as left and right flipped.
  @override
472
  EdgeInsets get flipped => EdgeInsets.fromLTRB(right, bottom, left, top);
473

474 475 476 477 478
  /// 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].
479 480 481 482 483
  ///
  /// See also:
  ///
  ///  * [inflateSize], to inflate a [Size] rather than a [Rect].
  ///  * [deflateRect], to deflate a [Rect] rather than inflating it.
484
  Rect inflateRect(Rect rect) {
485
    return Rect.fromLTRB(rect.left - left, rect.top - top, rect.right + right, rect.bottom + bottom);
486 487
  }

488 489 490 491 492 493 494 495 496 497 498 499 500 501
  /// 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) {
502
    return Rect.fromLTRB(rect.left + left, rect.top + top, rect.right - right, rect.bottom - bottom);
503 504
  }

505 506
  @override
  EdgeInsetsGeometry subtract(EdgeInsetsGeometry other) {
507
    if (other is EdgeInsets) {
508
      return this - other;
509
    }
510
    return super.subtract(other);
511 512
  }

513 514
  @override
  EdgeInsetsGeometry add(EdgeInsetsGeometry other) {
515
    if (other is EdgeInsets) {
516
      return this + other;
517
    }
518
    return super.add(other);
519 520
  }

521 522 523
  @override
  EdgeInsetsGeometry clamp(EdgeInsetsGeometry min, EdgeInsetsGeometry max) {
    return EdgeInsets.fromLTRB(
524 525 526 527
      clampDouble(_left, min._left, max._left),
      clampDouble(_top, min._top, max._top),
      clampDouble(_right, min._right, max._right),
      clampDouble(_bottom, min._bottom, max._bottom),
528 529 530
    );
  }

531
  /// Returns the difference between two [EdgeInsets].
532
  EdgeInsets operator -(EdgeInsets other) {
533
    return EdgeInsets.fromLTRB(
534
      left - other.left,
535 536
      top - other.top,
      right - other.right,
537
      bottom - other.bottom,
538 539 540
    );
  }

541
  /// Returns the sum of two [EdgeInsets].
542
  EdgeInsets operator +(EdgeInsets other) {
543
    return EdgeInsets.fromLTRB(
544
      left + other.left,
545 546
      top + other.top,
      right + other.right,
547
      bottom + other.bottom,
548 549 550
    );
  }

551 552 553 554 555
  /// Returns the [EdgeInsets] object with each dimension negated.
  ///
  /// This is the same as multiplying the object by -1.0.
  @override
  EdgeInsets operator -() {
556
    return EdgeInsets.fromLTRB(
557 558 559 560 561 562 563 564 565
      -left,
      -top,
      -right,
      -bottom,
    );
  }

  /// Scales the [EdgeInsets] in each dimension by the given factor.
  @override
566
  EdgeInsets operator *(double other) {
567
    return EdgeInsets.fromLTRB(
568
      left * other,
569 570
      top * other,
      right * other,
571
      bottom * other,
572 573 574
    );
  }

575 576
  /// Divides the [EdgeInsets] in each dimension by the given factor.
  @override
577
  EdgeInsets operator /(double other) {
578
    return EdgeInsets.fromLTRB(
579
      left / other,
580 581
      top / other,
      right / other,
582
      bottom / other,
583 584 585
    );
  }

586 587
  /// Integer divides the [EdgeInsets] in each dimension by the given factor.
  @override
588
  EdgeInsets operator ~/(double other) {
589
    return EdgeInsets.fromLTRB(
590
      (left ~/ other).toDouble(),
591 592
      (top ~/ other).toDouble(),
      (right ~/ other).toDouble(),
593
      (bottom ~/ other).toDouble(),
594 595 596
    );
  }

597
  /// Computes the remainder in each dimension by the given factor.
598
  @override
599
  EdgeInsets operator %(double other) {
600
    return EdgeInsets.fromLTRB(
601
      left % other,
602 603
      top % other,
      right % other,
604
      bottom % other,
605 606 607
    );
  }

608
  /// Linearly interpolate between two [EdgeInsets].
609
  ///
610
  /// If either is null, this function interpolates from [EdgeInsets.zero].
611
  ///
612
  /// {@macro dart.ui.shadow.lerp}
613
  static EdgeInsets? lerp(EdgeInsets? a, EdgeInsets? b, double t) {
614 615
    if (identical(a, b)) {
      return a;
616 617
    }
    if (a == null) {
618
      return b! * t;
619 620
    }
    if (b == null) {
621
      return a * (1.0 - t);
622
    }
623
    return EdgeInsets.fromLTRB(
624 625 626 627
      ui.lerpDouble(a.left, b.left, t)!,
      ui.lerpDouble(a.top, b.top, t)!,
      ui.lerpDouble(a.right, b.right, t)!,
      ui.lerpDouble(a.bottom, b.bottom, t)!,
628 629 630
    );
  }

631
  @override
632
  EdgeInsets resolve(TextDirection? direction) => this;
633 634 635 636

  /// Creates a copy of this EdgeInsets but with the given fields replaced
  /// with the new values.
  EdgeInsets copyWith({
637 638 639 640
    double? left,
    double? top,
    double? right,
    double? bottom,
641
  }) {
642
    return EdgeInsets.only(
643 644 645 646 647 648
      left: left ?? this.left,
      top: top ?? this.top,
      right: right ?? this.right,
      bottom: bottom ?? this.bottom,
    );
  }
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
}

/// 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.
  ///
668
  /// {@tool snippet}
669 670 671 672 673 674
  ///
  /// A margin indent of 40 pixels on the leading side:
  ///
  /// ```dart
  /// const EdgeInsetsDirectional.only(start: 40.0)
  /// ```
675
  /// {@end-tool}
676
  const EdgeInsetsDirectional.only({
677 678 679
    this.start = 0.0,
    this.top = 0.0,
    this.end = 0.0,
680
    this.bottom = 0.0,
681 682
  });

683 684 685 686
  /// Creates insets with symmetric vertical and horizontal offsets.
  ///
  /// This is equivalent to [EdgeInsets.symmetric], since the inset is the same
  /// with either [TextDirection]. This constructor is just a convenience for
687
  /// type compatibility.
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
  ///
  /// {@tool snippet}
  /// Eight pixel margin above and below, no horizontal margins:
  ///
  /// ```dart
  /// const EdgeInsetsDirectional.symmetric(vertical: 8.0)
  /// ```
  /// {@end-tool}
  const EdgeInsetsDirectional.symmetric({
    double horizontal = 0.0,
    double vertical = 0.0,
  })  : start = horizontal,
        end = horizontal,
        top = vertical,
        bottom = vertical;

704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
  /// Creates insets where all the offsets are `value`.
  ///
  /// {@tool snippet}
  ///
  /// Typical eight-pixel margin on all sides:
  ///
  /// ```dart
  /// const EdgeInsetsDirectional.all(8.0)
  /// ```
  /// {@end-tool}
  const EdgeInsetsDirectional.all(double value)
    : start = value,
      top = value,
      end = value,
      bottom = value;

720 721 722 723
  /// 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].
724
  static const EdgeInsetsDirectional zero = EdgeInsetsDirectional.only();
725

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
  /// 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
775
  EdgeInsetsDirectional get flipped => EdgeInsetsDirectional.fromSTEB(end, bottom, start, top);
776 777 778

  @override
  EdgeInsetsGeometry subtract(EdgeInsetsGeometry other) {
779
    if (other is EdgeInsetsDirectional) {
780
      return this - other;
781
    }
782
    return super.subtract(other);
783 784
  }

785
  @override
786
  EdgeInsetsGeometry add(EdgeInsetsGeometry other) {
787
    if (other is EdgeInsetsDirectional) {
788
      return this + other;
789
    }
790 791 792 793 794
    return super.add(other);
  }

  /// Returns the difference between two [EdgeInsetsDirectional] objects.
  EdgeInsetsDirectional operator -(EdgeInsetsDirectional other) {
795
    return EdgeInsetsDirectional.fromSTEB(
796 797 798 799 800 801 802 803 804
      start - other.start,
      top - other.top,
      end - other.end,
      bottom - other.bottom,
    );
  }

  /// Returns the sum of two [EdgeInsetsDirectional] objects.
  EdgeInsetsDirectional operator +(EdgeInsetsDirectional other) {
805
    return EdgeInsetsDirectional.fromSTEB(
806 807 808 809 810 811
      start + other.start,
      top + other.top,
      end + other.end,
      bottom + other.bottom,
    );
  }
812

813 814 815 816 817
  /// Returns the [EdgeInsetsDirectional] object with each dimension negated.
  ///
  /// This is the same as multiplying the object by -1.0.
  @override
  EdgeInsetsDirectional operator -() {
818
    return EdgeInsetsDirectional.fromSTEB(
819 820 821 822 823 824 825 826
      -start,
      -top,
      -end,
      -bottom,
    );
  }

  /// Scales the [EdgeInsetsDirectional] object in each dimension by the given factor.
827
  @override
828
  EdgeInsetsDirectional operator *(double other) {
829
    return EdgeInsetsDirectional.fromSTEB(
830 831 832 833 834 835 836 837 838 839
      start * other,
      top * other,
      end * other,
      bottom * other,
    );
  }

  /// Divides the [EdgeInsetsDirectional] object in each dimension by the given factor.
  @override
  EdgeInsetsDirectional operator /(double other) {
840
    return EdgeInsetsDirectional.fromSTEB(
841 842 843 844 845 846 847 848 849 850
      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) {
851
    return EdgeInsetsDirectional.fromSTEB(
852 853 854 855 856 857 858 859 860 861
      (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) {
862
    return EdgeInsetsDirectional.fromSTEB(
863 864 865 866 867 868 869 870 871 872 873 874 875 876
      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.
877
  ///
878
  /// {@macro dart.ui.shadow.lerp}
879
  static EdgeInsetsDirectional? lerp(EdgeInsetsDirectional? a, EdgeInsetsDirectional? b, double t) {
880 881
    if (identical(a, b)) {
      return a;
882 883
    }
    if (a == null) {
884
      return b! * t;
885 886
    }
    if (b == null) {
887
      return a * (1.0 - t);
888
    }
889
    return EdgeInsetsDirectional.fromSTEB(
890 891 892 893
      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)!,
894 895 896 897
    );
  }

  @override
898
  EdgeInsets resolve(TextDirection? direction) {
899
    assert(direction != null);
900
    switch (direction!) {
901
      case TextDirection.rtl:
902
        return EdgeInsets.fromLTRB(end, top, start, bottom);
903
      case TextDirection.ltr:
904
        return EdgeInsets.fromLTRB(start, top, end, bottom);
905 906
    }
  }
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922

  /// Creates a copy of this EdgeInsetsDirectional but with the given
  /// fields replaced with the new values.
  EdgeInsetsDirectional copyWith({
    double? start,
    double? top,
    double? end,
    double? bottom,
  }) {
    return EdgeInsetsDirectional.only(
      start: start ?? this.start,
      top: top ?? this.top,
      end: end ?? this.end,
      bottom: bottom ?? this.bottom,
    );
  }
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
}

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 -() {
958
    return _MixedEdgeInsets.fromLRSETB(
959 960 961 962 963 964 965 966 967 968 969
      -_left,
      -_right,
      -_start,
      -_end,
      -_top,
      -_bottom,
    );
  }

  @override
  _MixedEdgeInsets operator *(double other) {
970
    return _MixedEdgeInsets.fromLRSETB(
971 972 973 974 975 976 977 978 979 980 981
      _left * other,
      _right * other,
      _start * other,
      _end * other,
      _top * other,
      _bottom * other,
    );
  }

  @override
  _MixedEdgeInsets operator /(double other) {
982
    return _MixedEdgeInsets.fromLRSETB(
983 984 985 986 987 988 989 990 991 992 993
      _left / other,
      _right / other,
      _start / other,
      _end / other,
      _top / other,
      _bottom / other,
    );
  }

  @override
  _MixedEdgeInsets operator ~/(double other) {
994
    return _MixedEdgeInsets.fromLRSETB(
995 996 997 998 999 1000 1001 1002 1003 1004 1005
      (_left ~/ other).toDouble(),
      (_right ~/ other).toDouble(),
      (_start ~/ other).toDouble(),
      (_end ~/ other).toDouble(),
      (_top ~/ other).toDouble(),
      (_bottom ~/ other).toDouble(),
    );
  }

  @override
  _MixedEdgeInsets operator %(double other) {
1006
    return _MixedEdgeInsets.fromLRSETB(
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
      _left % other,
      _right % other,
      _start % other,
      _end % other,
      _top % other,
      _bottom % other,
    );
  }

  @override
1017
  EdgeInsets resolve(TextDirection? direction) {
1018
    assert(direction != null);
1019
    switch (direction!) {
1020
      case TextDirection.rtl:
1021
        return EdgeInsets.fromLTRB(_end + _left, _top, _start + _right, _bottom);
1022
      case TextDirection.ltr:
1023
        return EdgeInsets.fromLTRB(_start + _left, _top, _end + _right, _bottom);
1024 1025
    }
  }
1026
}