popup_menu_theme.dart 6.05 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
// 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';

10
import 'material_state.dart';
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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.
33
@immutable
34
class PopupMenuThemeData with Diagnosticable {
35 36 37 38 39 40
  /// Creates the set of properties used to configure [PopupMenuTheme].
  const PopupMenuThemeData({
    this.color,
    this.shape,
    this.elevation,
    this.textStyle,
41
    this.enableFeedback,
42
    this.mouseCursor,
43 44 45
  });

  /// The background color of the popup menu.
46
  final Color? color;
47 48

  /// The shape of the popup menu.
49
  final ShapeBorder? shape;
50 51

  /// The elevation of the popup menu.
52
  final double? elevation;
53 54

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

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

62 63 64 65 66
  /// {@macro flutter.material.popupmenu.mouseCursor}
  ///
  /// If specified, overrides the default value of [PopupMenuItem.mouseCursor].
  final MaterialStateProperty<MouseCursor?>? mouseCursor;

67 68 69
  /// Creates a copy of this object with the given fields replaced with the
  /// new values.
  PopupMenuThemeData copyWith({
70 71 72 73
    Color? color,
    ShapeBorder? shape,
    double? elevation,
    TextStyle? textStyle,
74
    bool? enableFeedback,
75
    MaterialStateProperty<MouseCursor?>? mouseCursor,
76 77 78 79 80 81
  }) {
    return PopupMenuThemeData(
      color: color ?? this.color,
      shape: shape ?? this.shape,
      elevation: elevation ?? this.elevation,
      textStyle: textStyle ?? this.textStyle,
82
      enableFeedback: enableFeedback ?? this.enableFeedback,
83
      mouseCursor: mouseCursor ?? this.mouseCursor,
84 85 86 87 88 89 90 91
    );
  }

  /// Linearly interpolate between two popup menu themes.
  ///
  /// If both arguments are null, then null is returned.
  ///
  /// {@macro dart.ui.shadow.lerp}
92
  static PopupMenuThemeData? lerp(PopupMenuThemeData? a, PopupMenuThemeData? b, double t) {
93 94 95 96 97 98 99 100
    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),
101
      enableFeedback: t < 0.5 ? a?.enableFeedback : b?.enableFeedback,
102
      mouseCursor: t < 0.5 ? a?.mouseCursor : b?.mouseCursor,
103 104 105 106
    );
  }

  @override
107 108 109 110 111 112 113 114
  int get hashCode => Object.hash(
    color,
    shape,
    elevation,
    textStyle,
    enableFeedback,
    mouseCursor,
  );
115 116 117 118 119 120 121

  @override
  bool operator ==(Object other) {
    if (identical(this, other))
      return true;
    if (other.runtimeType != runtimeType)
      return false;
122 123 124 125
    return other is PopupMenuThemeData
        && other.elevation == elevation
        && other.color == color
        && other.shape == shape
126
        && other.textStyle == textStyle
127
        && other.enableFeedback == enableFeedback
128
        && other.mouseCursor == mouseCursor;
129 130 131 132 133 134 135 136 137
  }

  @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));
138
    properties.add(DiagnosticsProperty<bool>('enableFeedback', enableFeedback, defaultValue: null));
139
    properties.add(DiagnosticsProperty<MaterialStateProperty<MouseCursor?>>('mouseCursor', mouseCursor, defaultValue: null));
140 141 142 143 144 145 146 147
  }
}

/// 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.
148
class PopupMenuTheme extends InheritedTheme {
149 150
  /// Creates a popup menu theme that controls the configurations for
  /// popup menus in its widget subtree.
151 152 153
  ///
  /// The data argument must not be null.
  const PopupMenuTheme({
154
    super.key,
155
    required this.data,
156 157
    required super.child,
  }) : assert(data != null);
158 159 160 161 162 163 164 165 166 167 168 169 170 171

  /// 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) {
172
    final PopupMenuTheme? popupMenuTheme = context.dependOnInheritedWidgetOfExactType<PopupMenuTheme>();
173
    return popupMenuTheme?.data ?? Theme.of(context).popupMenuTheme;
174 175
  }

176 177
  @override
  Widget wrap(BuildContext context, Widget child) {
178
    return PopupMenuTheme(data: data, child: child);
179 180
  }

181 182 183
  @override
  bool updateShouldNotify(PopupMenuTheme oldWidget) => data != oldWidget.data;
}