app_bar_theme.dart 6.16 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 29
// 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 'text_theme.dart';
import 'theme.dart';

/// Defines default property values for descendant [AppBar] widgets.
///
/// Descendant widgets obtain the current [AppBarTheme] object using
/// `AppBarTheme.of(context)`. Instances of [AppBarTheme] can be customized
/// with [AppBarTheme.copyWith].
///
/// Typically an [AppBarTheme] is specified as part of the overall [Theme] with
/// [ThemeData.appBarTheme].
///
/// All [AppBarTheme] properties are `null` by default. When null, the [AppBar]
/// will use the values from [ThemeData] if they exist, otherwise it will
/// provide its own defaults.
///
/// See also:
///
///  * [ThemeData], which describes the overall theme information for the
///    application.
30
@immutable
31
class AppBarTheme with Diagnosticable {
32
  /// Creates a theme that can be used for [ThemeData.appBarTheme].
33 34 35 36
  const AppBarTheme({
    this.brightness,
    this.color,
    this.elevation,
37
    this.shadowColor,
38
    this.iconTheme,
39
    this.actionsIconTheme,
40
    this.textTheme,
41
    this.centerTitle,
42
    this.titleSpacing,
43 44 45 46 47
  });

  /// Default value for [AppBar.brightness].
  ///
  /// If null, [AppBar] uses [ThemeData.primaryColorBrightness].
48
  final Brightness? brightness;
49

50
  /// Default value for [AppBar.backgroundColor].
51 52
  ///
  /// If null, [AppBar] uses [ThemeData.primaryColor].
53
  final Color? color;
54 55 56 57

  /// Default value for [AppBar.elevation].
  ///
  /// If null, [AppBar] uses a default value of 4.0.
58
  final double? elevation;
59

60 61 62
  /// Default value for [AppBar.shadowColor].
  ///
  /// If null, [AppBar] uses a default value of fully opaque black.
63
  final Color? shadowColor;
64

65 66 67
  /// Default value for [AppBar.iconTheme].
  ///
  /// If null, [AppBar] uses [ThemeData.primaryIconTheme].
68
  final IconThemeData? iconTheme;
69

70 71 72
  /// Default value for [AppBar.actionsIconTheme].
  ///
  /// If null, [AppBar] uses [ThemeData.primaryIconTheme].
73
  final IconThemeData? actionsIconTheme;
74

75 76 77
  /// Default value for [AppBar.textTheme].
  ///
  /// If null, [AppBar] uses [ThemeData.primaryTextTheme].
78
  final TextTheme? textTheme;
79

80 81 82
  /// Default value for [AppBar.centerTitle].
  ///
  /// If null, the value is adapted to current [TargetPlatform].
83
  final bool? centerTitle;
84

85 86 87 88 89
  /// Default value for [AppBar.titleSpacing].
  ///
  /// If null, [AppBar] uses default value of [NavigationToolbar.kMiddleSpacing].
  final double? titleSpacing;

90 91 92
  /// Creates a copy of this object with the given fields replaced with the
  /// new values.
  AppBarTheme copyWith({
93 94 95 96 97 98 99 100
    IconThemeData? actionsIconTheme,
    Brightness? brightness,
    Color? color,
    double? elevation,
    Color? shadowColor,
    IconThemeData? iconTheme,
    TextTheme? textTheme,
    bool? centerTitle,
101
    double? titleSpacing,
102 103 104 105 106
  }) {
    return AppBarTheme(
      brightness: brightness ?? this.brightness,
      color: color ?? this.color,
      elevation: elevation ?? this.elevation,
107
      shadowColor: shadowColor ?? this.shadowColor,
108
      iconTheme: iconTheme ?? this.iconTheme,
109
      actionsIconTheme: actionsIconTheme ?? this.actionsIconTheme,
110
      textTheme: textTheme ?? this.textTheme,
111
      centerTitle: centerTitle ?? this.centerTitle,
112
      titleSpacing: titleSpacing ?? this.titleSpacing,
113 114 115 116 117
    );
  }

  /// The [ThemeData.appBarTheme] property of the ambient [Theme].
  static AppBarTheme of(BuildContext context) {
118
    return Theme.of(context)!.appBarTheme;
119 120 121 122 123 124 125
  }

  /// Linearly interpolate between two AppBar themes.
  ///
  /// The argument `t` must not be null.
  ///
  /// {@macro dart.ui.shadow.lerp}
126
  static AppBarTheme lerp(AppBarTheme? a, AppBarTheme? b, double t) {
127 128 129 130 131
    assert(t != null);
    return AppBarTheme(
      brightness: t < 0.5 ? a?.brightness : b?.brightness,
      color: Color.lerp(a?.color, b?.color, t),
      elevation: lerpDouble(a?.elevation, b?.elevation, t),
132
      shadowColor: Color.lerp(a?.shadowColor, b?.shadowColor, t),
133
      iconTheme: IconThemeData.lerp(a?.iconTheme, b?.iconTheme, t),
134
      actionsIconTheme: IconThemeData.lerp(a?.actionsIconTheme, b?.actionsIconTheme, t),
135
      textTheme: TextTheme.lerp(a?.textTheme, b?.textTheme, t),
136
      centerTitle: t < 0.5 ? a?.centerTitle : b?.centerTitle,
137
      titleSpacing: lerpDouble(a?.titleSpacing, b?.titleSpacing, t),
138 139 140 141 142 143 144 145 146
    );
  }

  @override
  int get hashCode {
    return hashValues(
      brightness,
      color,
      elevation,
147
      shadowColor,
148
      iconTheme,
149
      actionsIconTheme,
150
      textTheme,
151
      centerTitle,
152
      titleSpacing,
153 154 155 156
    );
  }

  @override
157
  bool operator ==(Object other) {
158 159 160 161
    if (identical(this, other))
      return true;
    if (other.runtimeType != runtimeType)
      return false;
162 163 164 165
    return other is AppBarTheme
        && other.brightness == brightness
        && other.color == color
        && other.elevation == elevation
166
        && other.shadowColor == shadowColor
167 168
        && other.iconTheme == iconTheme
        && other.actionsIconTheme == actionsIconTheme
169
        && other.textTheme == textTheme
170 171
        && other.centerTitle == centerTitle
        && other.titleSpacing == titleSpacing;
172 173 174 175 176 177
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<Brightness>('brightness', brightness, defaultValue: null));
178
    properties.add(ColorProperty('color', color, defaultValue: null));
179
    properties.add(DiagnosticsProperty<double>('elevation', elevation, defaultValue: null));
180
    properties.add(ColorProperty('shadowColor', shadowColor, defaultValue: null));
181
    properties.add(DiagnosticsProperty<IconThemeData>('iconTheme', iconTheme, defaultValue: null));
182
    properties.add(DiagnosticsProperty<IconThemeData>('actionsIconTheme', actionsIconTheme, defaultValue: null));
183
    properties.add(DiagnosticsProperty<TextTheme>('textTheme', textTheme, defaultValue: null));
184
    properties.add(DiagnosticsProperty<bool>('centerTitle', centerTitle, defaultValue: null));
185
    properties.add(DiagnosticsProperty<double>('titleSpacing', titleSpacing, defaultValue: null));
186 187
  }
}