divider_theme.dart 5.51 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
// 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';

12 13 14
// Examples can assume:
// late BuildContext context;

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/// 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.
32
@immutable
33
class DividerThemeData with Diagnosticable {
34 35 36 37 38 39 40 41 42 43 44 45 46

  /// 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.
47
  final Color? color;
48 49 50 51 52

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

  /// The thickness of the line drawn within the divider.
56
  final double? thickness;
57 58 59

  /// The amount of empty space at the leading edge of [Divider] or top edge of
  /// [VerticalDivider].
60
  final double? indent;
61 62 63

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

  /// Creates a copy of this object with the given fields replaced with the
  /// new values.
  DividerThemeData copyWith({
69 70 71 72 73
    Color? color,
    double? space,
    double? thickness,
    double? indent,
    double? endIndent,
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  }) {
    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}
89
  static DividerThemeData lerp(DividerThemeData? a, DividerThemeData? b, double t) {
90 91 92
    if (identical(a, b) && a != null) {
      return a;
    }
93 94 95 96 97 98 99 100 101 102
    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
103 104 105 106 107 108 109
  int get hashCode => Object.hash(
    color,
    space,
    thickness,
    indent,
    endIndent,
  );
110 111

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

  @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
139
/// [Divider]s, [VerticalDivider]s, dividers between [ListTile]s, and dividers
140
/// between rows in [DataTable]s in this widget's subtree.
141
class DividerTheme extends InheritedTheme {
142
  /// Creates a divider theme that controls the configurations for
Dan Field's avatar
Dan Field committed
143
  /// [Divider]s, [VerticalDivider]s, dividers between [ListTile]s, and dividers
144 145
  /// between rows in [DataTable]s in its widget subtree.
  const DividerTheme({
146
    super.key,
147
    required this.data,
148
    required super.child,
149
  });
150

Dan Field's avatar
Dan Field committed
151
  /// The properties for descendant [Divider]s, [VerticalDivider]s, dividers
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  /// 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) {
167
    final DividerTheme? dividerTheme = context.dependOnInheritedWidgetOfExactType<DividerTheme>();
168
    return dividerTheme?.data ?? Theme.of(context).dividerTheme;
169 170
  }

171 172
  @override
  Widget wrap(BuildContext context, Widget child) {
173
    return DividerTheme(data: data, child: child);
174 175
  }

176 177 178
  @override
  bool updateShouldNotify(DividerTheme oldWidget) => data != oldWidget.data;
}