banner_theme.dart 5.77 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 18 19 20 21 22 23 24 25 26 27
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';

import 'theme.dart';

/// Defines the visual properties of [MaterialBanner] widgets.
///
/// Descendant widgets obtain the current [MaterialBannerThemeData] object using
/// `MaterialBannerTheme.of(context)`. Instances of [MaterialBannerThemeData]
/// can be customized with [MaterialBannerThemeData.copyWith].
///
/// Typically a [MaterialBannerThemeData] is specified as part of the overall
/// [Theme] with [ThemeData.bannerTheme].
///
/// All [MaterialBannerThemeData] properties are `null` by default. When null,
/// the [MaterialBanner] will provide its own defaults.
///
/// See also:
///
///  * [ThemeData], which describes the overall theme information for the
///    application.
28
@immutable
29
class MaterialBannerThemeData with Diagnosticable {
30 31 32 33 34 35

  /// Creates a theme that can be used for [MaterialBannerTheme] or
  /// [ThemeData.bannerTheme].
  const MaterialBannerThemeData({
    this.backgroundColor,
    this.contentTextStyle,
36
    this.elevation,
37 38 39 40 41
    this.padding,
    this.leadingPadding,
  });

  /// The background color of a [MaterialBanner].
42
  final Color? backgroundColor;
43 44 45

  /// Used to configure the [DefaultTextStyle] for the [MaterialBanner.content]
  /// widget.
46
  final TextStyle? contentTextStyle;
47

48 49 50 51 52
  /// Default value for [MaterialBanner.elevation].
  //
  // If null, MaterialBanner uses a default of 0.0.
  final double? elevation;

53
  /// The amount of space by which to inset [MaterialBanner.content].
54
  final EdgeInsetsGeometry? padding;
55 56

  /// The amount of space by which to inset [MaterialBanner.leading].
57
  final EdgeInsetsGeometry? leadingPadding;
58 59 60 61

  /// Creates a copy of this object with the given fields replaced with the
  /// new values.
  MaterialBannerThemeData copyWith({
62 63
    Color? backgroundColor,
    TextStyle? contentTextStyle,
64
    double? elevation,
65 66
    EdgeInsetsGeometry? padding,
    EdgeInsetsGeometry? leadingPadding,
67 68 69 70
  }) {
    return MaterialBannerThemeData(
      backgroundColor: backgroundColor ?? this.backgroundColor,
      contentTextStyle: contentTextStyle ?? this.contentTextStyle,
71
      elevation: elevation ?? this.elevation,
72 73 74 75 76 77 78 79 80 81
      padding: padding ?? this.padding,
      leadingPadding: leadingPadding ?? this.leadingPadding,
    );
  }

  /// Linearly interpolate between two Banner themes.
  ///
  /// The argument `t` must not be null.
  ///
  /// {@macro dart.ui.shadow.lerp}
82
  static MaterialBannerThemeData lerp(MaterialBannerThemeData? a, MaterialBannerThemeData? b, double t) {
83 84 85 86
    assert(t != null);
    return MaterialBannerThemeData(
      backgroundColor: Color.lerp(a?.backgroundColor, b?.backgroundColor, t),
      contentTextStyle: TextStyle.lerp(a?.contentTextStyle, b?.contentTextStyle, t),
87
      elevation: lerpDouble(a?.elevation, b?.elevation, t),
88 89
      padding: EdgeInsetsGeometry.lerp(a?.padding, b?.padding, t),
      leadingPadding: EdgeInsetsGeometry.lerp(a?.leadingPadding, b?.leadingPadding, t),
90 91 92 93 94 95 96 97
    );
  }

  @override
  int get hashCode {
    return hashValues(
      backgroundColor,
      contentTextStyle,
98
      elevation,
99 100 101 102 103 104
      padding,
      leadingPadding,
    );
  }

  @override
105
  bool operator ==(Object other) {
106 107 108 109
    if (identical(this, other))
      return true;
    if (other.runtimeType != runtimeType)
      return false;
110 111 112
    return other is MaterialBannerThemeData
        && other.backgroundColor == backgroundColor
        && other.contentTextStyle == contentTextStyle
113
        && other.elevation == elevation
114 115
        && other.padding == padding
        && other.leadingPadding == leadingPadding;
116 117 118 119 120 121 122
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(ColorProperty('backgroundColor', backgroundColor, defaultValue: null));
    properties.add(DiagnosticsProperty<TextStyle>('contentTextStyle', contentTextStyle, defaultValue: null));
123
    properties.add(DiagnosticsProperty<double>('elevation', elevation, defaultValue: null));
124 125 126 127 128 129 130 131 132 133
    properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('padding', padding, defaultValue: null));
    properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('leadingPadding', leadingPadding, defaultValue: null));
  }
}

/// An inherited widget that defines the configuration for
/// [MaterialBanner]s in this widget's subtree.
///
/// Values specified here are used for [MaterialBanner] properties that are not
/// given an explicit non-null value.
134
class MaterialBannerTheme extends InheritedTheme {
135 136 137
  /// Creates a banner theme that controls the configurations for
  /// [MaterialBanner]s in its widget subtree.
  const MaterialBannerTheme({
138
    Key? key,
139
    this.data,
140
    required Widget child,
141 142 143
  }) : super(key: key, child: child);

  /// The properties for descendant [MaterialBanner] widgets.
144
  final MaterialBannerThemeData? data;
145 146 147 148 149 150 151 152 153 154 155 156 157

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

162 163
  @override
  Widget wrap(BuildContext context, Widget child) {
164
    return MaterialBannerTheme(data: data, child: child);
165 166
  }

167 168 169
  @override
  bool updateShouldNotify(MaterialBannerTheme oldWidget) => data != oldWidget.data;
}