dialog_theme.dart 6.15 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 6
import 'dart:ui' show lerpDouble;

7 8 9 10 11 12 13 14 15 16 17
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';

import 'theme.dart';

/// Defines a theme for [Dialog] widgets.
///
/// Descendant widgets obtain the current [DialogTheme] object using
/// `DialogTheme.of(context)`. Instances of [DialogTheme] can be customized with
/// [DialogTheme.copyWith].
///
18
/// [titleTextStyle] and [contentTextStyle] are used in [AlertDialog]s and [SimpleDialog]s.
19
///
20 21
/// See also:
///
22 23 24
///  * [Dialog], a dialog that can be customized using this [DialogTheme].
///  * [AlertDialog], a dialog that can be customized using this [DialogTheme].
///  * [SimpleDialog], a dialog that can be customized using this [DialogTheme].
25 26
///  * [ThemeData], which describes the overall theme information for the
///    application.
27
@immutable
28
class DialogTheme with Diagnosticable {
29
  /// Creates a dialog theme that can be used for [ThemeData.dialogTheme].
30 31 32
  const DialogTheme({
    this.backgroundColor,
    this.elevation,
33 34
    this.shadowColor,
    this.surfaceTintColor,
35
    this.shape,
36
    this.alignment,
37
    this.iconColor,
38 39
    this.titleTextStyle,
    this.contentTextStyle,
40
    this.actionsPadding,
41
  });
42

43
  /// Overrides the default value for [Dialog.backgroundColor].
44
  final Color? backgroundColor;
45

46
  /// Overrides the default value for [Dialog.elevation].
47
  final double? elevation;
48

49 50 51 52 53 54
  /// Overrides the default value for [Dialog.shadowColor].
  final Color? shadowColor;

  /// Overrides the default value for [Dialog.surfaceTintColor].
  final Color? surfaceTintColor;

55
  /// Overrides the default value for [Dialog.shape].
56
  final ShapeBorder? shape;
57

58
  /// Overrides the default value for [Dialog.alignment].
59 60
  final AlignmentGeometry? alignment;

61 62
  /// Overrides the default value for [DefaultTextStyle] for [SimpleDialog.title] and
  /// [AlertDialog.title].
63
  final TextStyle? titleTextStyle;
64

65 66
  /// Overrides the default value for [DefaultTextStyle] for [SimpleDialog.children] and
  /// [AlertDialog.content].
67
  final TextStyle? contentTextStyle;
68

69 70 71
  /// Overrides the default value for [AlertDialog.actionsPadding].
  final EdgeInsetsGeometry? actionsPadding;

72 73 74
  /// Used to configure the [IconTheme] for the [AlertDialog.icon] widget.
  final Color? iconColor;

75 76
  /// Creates a copy of this object but with the given fields replaced with the
  /// new values.
77
  DialogTheme copyWith({
78 79
    Color? backgroundColor,
    double? elevation,
80 81
    Color? shadowColor,
    Color? surfaceTintColor,
82
    ShapeBorder? shape,
83
    AlignmentGeometry? alignment,
84
    Color? iconColor,
85 86
    TextStyle? titleTextStyle,
    TextStyle? contentTextStyle,
87
    EdgeInsetsGeometry? actionsPadding,
88
  }) {
89 90 91
    return DialogTheme(
      backgroundColor: backgroundColor ?? this.backgroundColor,
      elevation: elevation ?? this.elevation,
92 93
      shadowColor: shadowColor ?? this.shadowColor,
      surfaceTintColor: surfaceTintColor ?? this.surfaceTintColor,
94
      shape: shape ?? this.shape,
95
      alignment: alignment ?? this.alignment,
96
      iconColor: iconColor ?? this.iconColor,
97 98
      titleTextStyle: titleTextStyle ?? this.titleTextStyle,
      contentTextStyle: contentTextStyle ?? this.contentTextStyle,
99
      actionsPadding: actionsPadding ?? this.actionsPadding,
100
    );
101 102 103 104
  }

  /// The data from the closest [DialogTheme] instance given the build context.
  static DialogTheme of(BuildContext context) {
105
    return Theme.of(context).dialogTheme;
106 107 108 109 110 111 112
  }

  /// Linearly interpolate between two dialog themes.
  ///
  /// The arguments must not be null.
  ///
  /// {@macro dart.ui.shadow.lerp}
113
  static DialogTheme lerp(DialogTheme? a, DialogTheme? b, double t) {
114 115
    assert(t != null);
    return DialogTheme(
116 117
      backgroundColor: Color.lerp(a?.backgroundColor, b?.backgroundColor, t),
      elevation: lerpDouble(a?.elevation, b?.elevation, t),
118 119
      shadowColor: Color.lerp(a?.shadowColor, b?.shadowColor, t),
      surfaceTintColor: Color.lerp(a?.surfaceTintColor, b?.surfaceTintColor, t),
120
      shape: ShapeBorder.lerp(a?.shape, b?.shape, t),
121
      alignment: AlignmentGeometry.lerp(a?.alignment, b?.alignment, t),
122
      iconColor: Color.lerp(a?.iconColor, b?.iconColor, t),
123
      titleTextStyle: TextStyle.lerp(a?.titleTextStyle, b?.titleTextStyle, t),
124
      contentTextStyle: TextStyle.lerp(a?.contentTextStyle, b?.contentTextStyle, t),
125
      actionsPadding: EdgeInsetsGeometry.lerp(a?.actionsPadding, b?.actionsPadding, t),
126 127 128 129 130 131 132
    );
  }

  @override
  int get hashCode => shape.hashCode;

  @override
133
  bool operator ==(Object other) {
134
    if (identical(this, other)) {
135
      return true;
136 137
    }
    if (other.runtimeType != runtimeType) {
138
      return false;
139
    }
140 141 142
    return other is DialogTheme
        && other.backgroundColor == backgroundColor
        && other.elevation == elevation
143 144
        && other.shadowColor == shadowColor
        && other.surfaceTintColor == surfaceTintColor
145
        && other.shape == shape
146
        && other.alignment == alignment
147
        && other.iconColor == iconColor
148
        && other.titleTextStyle == titleTextStyle
149 150
        && other.contentTextStyle == contentTextStyle
        && other.actionsPadding == actionsPadding;
151 152 153 154 155
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
156
    properties.add(ColorProperty('backgroundColor', backgroundColor));
157
    properties.add(DoubleProperty('elevation', elevation));
158 159
    properties.add(ColorProperty('shadowColor', shadowColor));
    properties.add(ColorProperty('surfaceTintColor', surfaceTintColor));
160 161
    properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
    properties.add(DiagnosticsProperty<AlignmentGeometry>('alignment', alignment, defaultValue: null));
162
    properties.add(ColorProperty('iconColor', iconColor));
163 164
    properties.add(DiagnosticsProperty<TextStyle>('titleTextStyle', titleTextStyle, defaultValue: null));
    properties.add(DiagnosticsProperty<TextStyle>('contentTextStyle', contentTextStyle, defaultValue: null));
165
    properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('actionsPadding', actionsPadding, defaultValue: null));
166 167
  }
}