popup_menu_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 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// 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';

/// Defines the visual properties of the routes used to display popup menus
/// as well as [PopupMenuItem] and [PopupMenuDivider] widgets.
///
/// Descendant widgets obtain the current [PopupMenuThemeData] object
/// using `PopupMenuTheme.of(context)`. Instances of
/// [PopupMenuThemeData] can be customized with
/// [PopupMenuThemeData.copyWith].
///
/// Typically, a [PopupMenuThemeData] is specified as part of the
/// overall [Theme] with [ThemeData.popupMenuTheme]. Otherwise,
/// [PopupMenuTheme] can be used to configure its own widget subtree.
///
/// All [PopupMenuThemeData] properties are `null` by default.
/// If any of these properties are null, the popup menu will provide its
/// own defaults.
///
/// See also:
///
///  * [ThemeData], which describes the overall theme information for the
///    application.
32
@immutable
33
class PopupMenuThemeData with Diagnosticable {
34 35 36 37 38 39
  /// Creates the set of properties used to configure [PopupMenuTheme].
  const PopupMenuThemeData({
    this.color,
    this.shape,
    this.elevation,
    this.textStyle,
40
    this.enableFeedback,
41 42 43
  });

  /// The background color of the popup menu.
44
  final Color? color;
45 46

  /// The shape of the popup menu.
47
  final ShapeBorder? shape;
48 49

  /// The elevation of the popup menu.
50
  final double? elevation;
51 52

  /// The text style of items in the popup menu.
53
  final TextStyle? textStyle;
54

55 56 57 58 59
  /// If specified, defines the feedback property for [PopupMenuButton].
  ///
  /// If [PopupMenuButton.enableFeedback] is provided, [enableFeedback] is ignored.
  final bool? enableFeedback;

60 61 62
  /// Creates a copy of this object with the given fields replaced with the
  /// new values.
  PopupMenuThemeData copyWith({
63 64 65 66
    Color? color,
    ShapeBorder? shape,
    double? elevation,
    TextStyle? textStyle,
67
    bool? enableFeedback,
68 69 70 71 72 73
  }) {
    return PopupMenuThemeData(
      color: color ?? this.color,
      shape: shape ?? this.shape,
      elevation: elevation ?? this.elevation,
      textStyle: textStyle ?? this.textStyle,
74
      enableFeedback: enableFeedback ?? this.enableFeedback,
75 76 77 78 79 80 81 82
    );
  }

  /// Linearly interpolate between two popup menu themes.
  ///
  /// If both arguments are null, then null is returned.
  ///
  /// {@macro dart.ui.shadow.lerp}
83
  static PopupMenuThemeData? lerp(PopupMenuThemeData? a, PopupMenuThemeData? b, double t) {
84 85 86 87 88 89 90 91
    assert(t != null);
    if (a == null && b == null)
      return null;
    return PopupMenuThemeData(
      color: Color.lerp(a?.color, b?.color, t),
      shape: ShapeBorder.lerp(a?.shape, b?.shape, t),
      elevation: lerpDouble(a?.elevation, b?.elevation, t),
      textStyle: TextStyle.lerp(a?.textStyle, b?.textStyle, t),
92
      enableFeedback: t < 0.5 ? a?.enableFeedback : b?.enableFeedback,
93 94 95 96 97 98 99 100 101 102
    );
  }

  @override
  int get hashCode {
    return hashValues(
      color,
      shape,
      elevation,
      textStyle,
103
      enableFeedback,
104 105 106 107 108 109 110 111 112
    );
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other))
      return true;
    if (other.runtimeType != runtimeType)
      return false;
113 114 115 116
    return other is PopupMenuThemeData
        && other.elevation == elevation
        && other.color == color
        && other.shape == shape
117 118
        && other.textStyle == textStyle
        && other.enableFeedback == enableFeedback;
119 120 121 122 123 124 125 126 127
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(ColorProperty('color', color, defaultValue: null));
    properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
    properties.add(DoubleProperty('elevation', elevation, defaultValue: null));
    properties.add(DiagnosticsProperty<TextStyle>('text style', textStyle, defaultValue: null));
128
    properties.add(DiagnosticsProperty<bool>('enableFeedback', enableFeedback, defaultValue: null));
129 130 131 132 133 134 135 136
  }
}

/// An inherited widget that defines the configuration for
/// popup menus in this widget's subtree.
///
/// Values specified here are used for popup menu properties that are not
/// given an explicit non-null value.
137
class PopupMenuTheme extends InheritedTheme {
138 139
  /// Creates a popup menu theme that controls the configurations for
  /// popup menus in its widget subtree.
140 141 142
  ///
  /// The data argument must not be null.
  const PopupMenuTheme({
143 144 145
    Key? key,
    required this.data,
    required Widget child,
146
  }) : assert(data != null), super(key: key, child: child);
147 148 149 150 151 152 153 154 155 156 157 158 159 160

  /// The properties for descendant popup menu widgets.
  final PopupMenuThemeData data;

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

165 166
  @override
  Widget wrap(BuildContext context, Widget child) {
167
    return PopupMenuTheme(data: data, child: child);
168 169
  }

170 171 172
  @override
  bool updateShouldNotify(PopupMenuTheme oldWidget) => data != oldWidget.data;
}