divider_theme.dart 5.44 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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui' show lerpDouble;

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';

import 'theme.dart';

/// Defines the visual properties of [Divider], [VerticalDivider], dividers
/// between [ListTile]s, and dividers between rows in [DataTable]s.
///
/// Descendant widgets obtain the current [DividerThemeData] object using
/// `DividerTheme.of(context)`. Instances of [DividerThemeData]
/// can be customized with [DividerThemeData.copyWith].
///
/// Typically a [DividerThemeData] is specified as part of the overall
/// [Theme] with [ThemeData.dividerTheme].
///
/// All [DividerThemeData] properties are `null` by default. When null,
/// the widgets will provide their own defaults.
///
/// See also:
///
///  * [ThemeData], which describes the overall theme information for the
///    application.
29
@immutable
30
class DividerThemeData with Diagnosticable {
31 32 33 34 35 36 37 38 39 40 41 42 43

  /// Creates a theme that can be used for [DividerTheme] or
  /// [ThemeData.dividerTheme].
  const DividerThemeData({
    this.color,
    this.space,
    this.thickness,
    this.indent,
    this.endIndent,
  });

  /// The color of [Divider]s and [VerticalDivider]s, also
  /// used between [ListTile]s, between rows in [DataTable]s, and so forth.
44
  final Color? color;
45 46 47 48 49

  /// The [Divider]'s width or the [VerticalDivider]'s height.
  ///
  /// This represents the amount of horizontal or vertical space the divider
  /// takes up.
50
  final double? space;
51 52

  /// The thickness of the line drawn within the divider.
53
  final double? thickness;
54 55 56

  /// The amount of empty space at the leading edge of [Divider] or top edge of
  /// [VerticalDivider].
57
  final double? indent;
58 59 60

  /// The amount of empty space at the trailing edge of [Divider] or bottom edge
  /// of [VerticalDivider].
61
  final double? endIndent;
62 63 64 65

  /// Creates a copy of this object with the given fields replaced with the
  /// new values.
  DividerThemeData copyWith({
66 67 68 69 70
    Color? color,
    double? space,
    double? thickness,
    double? indent,
    double? endIndent,
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  }) {
    return DividerThemeData(
      color: color ?? this.color,
      space: space ?? this.space,
      thickness: thickness ?? this.thickness,
      indent: indent ?? this.indent,
      endIndent: endIndent ?? this.endIndent,
    );
  }

  /// Linearly interpolate between two Divider themes.
  ///
  /// The argument `t` must not be null.
  ///
  /// {@macro dart.ui.shadow.lerp}
86
  static DividerThemeData lerp(DividerThemeData? a, DividerThemeData? b, double t) {
87 88 89 90 91 92 93 94 95 96 97
    assert(t != null);
    return DividerThemeData(
      color: Color.lerp(a?.color, b?.color, t),
      space: lerpDouble(a?.space, b?.space, t),
      thickness: lerpDouble(a?.thickness, b?.thickness, t),
      indent: lerpDouble(a?.indent, b?.indent, t),
      endIndent: lerpDouble(a?.endIndent, b?.endIndent, t),
    );
  }

  @override
98 99 100 101 102 103 104
  int get hashCode => Object.hash(
    color,
    space,
    thickness,
    indent,
    endIndent,
  );
105 106

  @override
107
  bool operator ==(Object other) {
108
    if (identical(this, other)) {
109
      return true;
110 111
    }
    if (other.runtimeType != runtimeType) {
112
      return false;
113
    }
114 115 116 117 118 119
    return other is DividerThemeData
        && other.color == color
        && other.space == space
        && other.thickness == thickness
        && other.indent == indent
        && other.endIndent == endIndent;
120 121 122 123 124 125 126 127 128 129 130 131 132 133
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(ColorProperty('color', color, defaultValue: null));
    properties.add(DoubleProperty('space', space, defaultValue: null));
    properties.add(DoubleProperty('thickness', thickness, defaultValue: null));
    properties.add(DoubleProperty('indent', indent, defaultValue: null));
    properties.add(DoubleProperty('endIndent', endIndent, defaultValue: null));
  }
}

/// An inherited widget that defines the configuration for
Dan Field's avatar
Dan Field committed
134
/// [Divider]s, [VerticalDivider]s, dividers between [ListTile]s, and dividers
135
/// between rows in [DataTable]s in this widget's subtree.
136
class DividerTheme extends InheritedTheme {
137
  /// Creates a divider theme that controls the configurations for
Dan Field's avatar
Dan Field committed
138
  /// [Divider]s, [VerticalDivider]s, dividers between [ListTile]s, and dividers
139 140
  /// between rows in [DataTable]s in its widget subtree.
  const DividerTheme({
141
    super.key,
142
    required this.data,
143 144
    required super.child,
  }) : assert(data != null);
145

Dan Field's avatar
Dan Field committed
146
  /// The properties for descendant [Divider]s, [VerticalDivider]s, dividers
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  /// between [ListTile]s, and dividers between rows in [DataTable]s.
  final DividerThemeData data;

  /// The closest instance of this class's [data] value that encloses the given
  /// context.
  ///
  /// If there is no ancestor, it returns [ThemeData.dividerTheme]. Applications
  /// can assume that the returned value will not be null.
  ///
  /// Typical usage is as follows:
  ///
  /// ```dart
  /// DividerThemeData theme = DividerTheme.of(context);
  /// ```
  static DividerThemeData of(BuildContext context) {
162
    final DividerTheme? dividerTheme = context.dependOnInheritedWidgetOfExactType<DividerTheme>();
163
    return dividerTheme?.data ?? Theme.of(context).dividerTheme;
164 165
  }

166 167
  @override
  Widget wrap(BuildContext context, Widget child) {
168
    return DividerTheme(data: data, child: child);
169 170
  }

171 172 173
  @override
  bool updateShouldNotify(DividerTheme oldWidget) => data != oldWidget.data;
}