dialog_theme.dart 4.39 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
// @dart = 2.8

7 8
import 'dart:ui' show lerpDouble;

9 10 11 12 13 14 15 16 17 18 19 20 21 22
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].
///
/// When Shape is `null`, the dialog defaults to a [RoundedRectangleBorder] with
/// a border radius of 2.0 on all corners.
///
Dan Field's avatar
Dan Field committed
23
/// [titleTextStyle] and [contentTextStyle] are used in [AlertDialog]s.
24
/// If null, they default to [TextTheme.headline6] and [TextTheme.subtitle1],
25 26
/// respectively.
///
27 28 29 30 31
/// See also:
///
///  * [Dialog], a material dialog that can be customized using this [DialogTheme].
///  * [ThemeData], which describes the overall theme information for the
///    application.
32
@immutable
33
class DialogTheme with Diagnosticable {
34
  /// Creates a dialog theme that can be used for [ThemeData.dialogTheme].
35 36 37 38 39 40 41
  const DialogTheme({
    this.backgroundColor,
    this.elevation,
    this.shape,
    this.titleTextStyle,
    this.contentTextStyle,
  });
42 43

  /// Default value for [Dialog.backgroundColor].
44 45 46
  ///
  /// If null, [ThemeData.dialogBackgroundColor] is used, if that's null,
  /// defaults to [Colors.white].
47 48 49 50 51 52
  final Color backgroundColor;

  /// Default value for [Dialog.elevation].
  ///
  /// If null, the [Dialog] elevation defaults to `24.0`.
  final double elevation;
53 54 55 56

  /// Default value for [Dialog.shape].
  final ShapeBorder shape;

57 58
  /// Used to configure the [DefaultTextStyle] for the [AlertDialog.title] widget.
  ///
59
  /// If null, defaults to [ThemeData.textTheme.headline6].
60 61 62 63
  final TextStyle titleTextStyle;

  /// Used to configure the [DefaultTextStyle] for the [AlertDialog.content] widget.
  ///
64
  /// If null, defaults to [ThemeData.textTheme.subtitle1].
65 66
  final TextStyle contentTextStyle;

67 68
  /// Creates a copy of this object but with the given fields replaced with the
  /// new values.
69 70 71 72 73 74 75
  DialogTheme copyWith({
    Color backgroundColor,
    double elevation,
    ShapeBorder shape,
    TextStyle titleTextStyle,
    TextStyle contentTextStyle,
  }) {
76 77 78 79
    return DialogTheme(
      backgroundColor: backgroundColor ?? this.backgroundColor,
      elevation: elevation ?? this.elevation,
      shape: shape ?? this.shape,
80 81
      titleTextStyle: titleTextStyle ?? this.titleTextStyle,
      contentTextStyle: contentTextStyle ?? this.contentTextStyle,
82
    );
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  }

  /// The data from the closest [DialogTheme] instance given the build context.
  static DialogTheme of(BuildContext context) {
    return Theme.of(context).dialogTheme;
  }

  /// Linearly interpolate between two dialog themes.
  ///
  /// The arguments must not be null.
  ///
  /// {@macro dart.ui.shadow.lerp}
  static DialogTheme lerp(DialogTheme a, DialogTheme b, double t) {
    assert(t != null);
    return DialogTheme(
98 99 100
      backgroundColor: Color.lerp(a?.backgroundColor, b?.backgroundColor, t),
      elevation: lerpDouble(a?.elevation, b?.elevation, t),
      shape: ShapeBorder.lerp(a?.shape, b?.shape, t),
101
      titleTextStyle: TextStyle.lerp(a?.titleTextStyle, b?.titleTextStyle, t),
102
      contentTextStyle: TextStyle.lerp(a?.contentTextStyle, b?.contentTextStyle, t),
103 104 105 106 107 108 109
    );
  }

  @override
  int get hashCode => shape.hashCode;

  @override
110
  bool operator ==(Object other) {
111 112 113 114
    if (identical(this, other))
      return true;
    if (other.runtimeType != runtimeType)
      return false;
115 116 117 118 119 120
    return other is DialogTheme
        && other.backgroundColor == backgroundColor
        && other.elevation == elevation
        && other.shape == shape
        && other.titleTextStyle == titleTextStyle
        && other.contentTextStyle == contentTextStyle;
121 122 123 124 125
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
126
    properties.add(ColorProperty('backgroundColor', backgroundColor));
127
    properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
128
    properties.add(DoubleProperty('elevation', elevation));
129 130
    properties.add(DiagnosticsProperty<TextStyle>('titleTextStyle', titleTextStyle, defaultValue: null));
    properties.add(DiagnosticsProperty<TextStyle>('contentTextStyle', contentTextStyle, defaultValue: null));
131 132
  }
}