drawer_theme.dart 6.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2014 The Flutter Authors. All rights reserved.
// 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/rendering.dart';
import 'package:flutter/widgets.dart';

import 'theme.dart';

13 14 15
// Examples can assume:
// late BuildContext context;

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/// Defines default property values for descendant [Drawer] widgets.
///
/// Descendant widgets obtain the current [DrawerThemeData] object
/// using `DrawerTheme.of(context)`. Instances of [DrawerThemeData] can be
/// customized with [DrawerThemeData.copyWith].
///
/// Typically a [DrawerThemeData] is specified as part of the
/// overall [Theme] with [ThemeData.drawerTheme].
///
/// All [DrawerThemeData] properties are `null` by default.
///
/// See also:
///
///  * [DrawerTheme], an [InheritedWidget] that propagates the theme down its
///    subtree.
///  * [ThemeData], which describes the overall theme information for the
///    application and can customize a drawer using [ThemeData.drawerTheme].
@immutable
class DrawerThemeData with Diagnosticable {
  /// Creates a theme that can be used for [ThemeData.drawerTheme] and
  /// [DrawerTheme].
  const DrawerThemeData({
    this.backgroundColor,
    this.scrimColor,
    this.elevation,
41 42
    this.shadowColor,
    this.surfaceTintColor,
43
    this.shape,
hangyu's avatar
hangyu committed
44
    this.endShape,
45
    this.width,
46 47 48 49 50 51 52 53 54 55 56
  });

  /// Overrides the default value of [Drawer.backgroundColor].
  final Color? backgroundColor;

  /// Overrides the default value of [DrawerController.scrimColor].
  final Color? scrimColor;

  /// Overrides the default value of [Drawer.elevation].
  final double? elevation;

57 58 59 60 61 62
  /// Overrides the default value for [Drawer.shadowColor].
  final Color? shadowColor;

  /// Overrides the default value for [Drawer.surfaceTintColor].
  final Color? surfaceTintColor;

63 64 65
  /// Overrides the default value of [Drawer.shape].
  final ShapeBorder? shape;

hangyu's avatar
hangyu committed
66 67 68
  /// Overrides the default value of [Drawer.shape] for a end drawer.
  final ShapeBorder? endShape;

69 70 71
  /// Overrides the default value of [Drawer.width].
  final double? width;

72 73 74 75 76 77
  /// Creates a copy of this object with the given fields replaced with the
  /// new values.
  DrawerThemeData copyWith({
    Color? backgroundColor,
    Color? scrimColor,
    double? elevation,
78 79
    Color? shadowColor,
    Color? surfaceTintColor,
80
    ShapeBorder? shape,
hangyu's avatar
hangyu committed
81
    ShapeBorder? endShape,
82
    double? width,
83 84 85 86 87
  }) {
    return DrawerThemeData(
      backgroundColor: backgroundColor ?? this.backgroundColor,
      scrimColor: scrimColor ?? this.scrimColor,
      elevation: elevation ?? this.elevation,
88 89
      shadowColor: shadowColor ?? this.shadowColor,
      surfaceTintColor: surfaceTintColor ?? this.surfaceTintColor,
90
      shape: shape ?? this.shape,
hangyu's avatar
hangyu committed
91
      endShape: endShape ?? this.endShape,
92
      width: width ?? this.width,
93 94 95 96 97 98 99 100 101
    );
  }

  /// Linearly interpolate between two drawer themes.
  ///
  /// If both arguments are null then null is returned.
  ///
  /// {@macro dart.ui.shadow.lerp}
  static DrawerThemeData? lerp(DrawerThemeData? a, DrawerThemeData? b, double t) {
102 103
    if (identical(a, b)) {
      return a;
104
    }
105 106 107 108
    return DrawerThemeData(
      backgroundColor: Color.lerp(a?.backgroundColor, b?.backgroundColor, t),
      scrimColor: Color.lerp(a?.scrimColor, b?.scrimColor, t),
      elevation: lerpDouble(a?.elevation, b?.elevation, t),
109 110
      shadowColor: Color.lerp(a?.shadowColor, b?.shadowColor, t),
      surfaceTintColor: Color.lerp(a?.surfaceTintColor, b?.surfaceTintColor, t),
111
      shape: ShapeBorder.lerp(a?.shape, b?.shape, t),
hangyu's avatar
hangyu committed
112
      endShape: ShapeBorder.lerp(a?.endShape, b?.endShape, t),
113
      width: lerpDouble(a?.width, b?.width, t),
114 115 116 117
    );
  }

  @override
118 119 120 121
  int get hashCode => Object.hash(
    backgroundColor,
    scrimColor,
    elevation,
122 123
    shadowColor,
    surfaceTintColor,
124
    shape,
hangyu's avatar
hangyu committed
125
    endShape,
126
    width,
127
  );
128 129 130

  @override
  bool operator ==(Object other) {
131
    if (identical(this, other)) {
132
      return true;
133 134
    }
    if (other.runtimeType != runtimeType) {
135
      return false;
136
    }
137 138 139 140
    return other is DrawerThemeData
        && other.backgroundColor == backgroundColor
        && other.scrimColor == scrimColor
        && other.elevation == elevation
141 142
        && other.shadowColor == shadowColor
        && other.surfaceTintColor == surfaceTintColor
143
        && other.shape == shape
hangyu's avatar
hangyu committed
144
        && other.endShape == endShape
145
        && other.width == width;
146 147 148 149 150 151 152 153
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(ColorProperty('backgroundColor', backgroundColor, defaultValue: null));
    properties.add(ColorProperty('scrimColor', scrimColor, defaultValue: null));
    properties.add(DoubleProperty('elevation', elevation, defaultValue: null));
154 155
    properties.add(ColorProperty('shadowColor', shadowColor, defaultValue: null));
    properties.add(ColorProperty('surfaceTintColor', surfaceTintColor, defaultValue: null));
156
    properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
hangyu's avatar
hangyu committed
157
    properties.add(DiagnosticsProperty<ShapeBorder>('endShape', endShape, defaultValue: null));
158
    properties.add(DoubleProperty('width', width, defaultValue: null));
159 160 161 162 163 164 165 166 167 168 169 170 171 172
  }
}

/// An inherited widget that defines visual properties for [Drawer]s in this
/// widget's subtree.
///
/// Values specified here are used for [Drawer] properties that are not
/// given an explicit non-null value.
///
/// Using this would allow you to override the [ThemeData.drawerTheme].
class DrawerTheme extends InheritedTheme {
  /// Creates a theme that defines the [DrawerThemeData] properties for a
  /// [Drawer].
  const DrawerTheme({
173
    super.key,
174
    required this.data,
175
    required super.child,
176
  });
177 178 179 180 181 182 183 184 185 186 187 188 189

  /// Specifies the background color, scrim color, elevation, and shape for
  /// descendant [Drawer] widgets.
  final DrawerThemeData data;

  /// The closest instance of this class that encloses the given context.
  ///
  /// If there is no enclosing [DrawerTheme] widget, then
  /// [ThemeData.drawerTheme] is used.
  ///
  /// Typical usage is as follows:
  ///
  /// ```dart
190
  /// DrawerThemeData theme = DrawerTheme.of(context);
191 192 193 194 195 196 197 198 199 200 201 202 203 204
  /// ```
  static DrawerThemeData of(BuildContext context) {
    final DrawerTheme? drawerTheme = context.dependOnInheritedWidgetOfExactType<DrawerTheme>();
    return drawerTheme?.data ?? Theme.of(context).drawerTheme;
  }

  @override
  Widget wrap(BuildContext context, Widget child) {
    return DrawerTheme(data: data, child: child);
  }

  @override
  bool updateShouldNotify(DrawerTheme oldWidget) => data != oldWidget.data;
}