expand_icon.dart 6.36 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
Ian Hickson's avatar
Ian Hickson committed
4

5
import 'dart:math' as math;
6 7 8 9 10 11

import 'package:flutter/widgets.dart';

import 'colors.dart';
import 'debug.dart';
import 'icon_button.dart';
12
import 'icons.dart';
13
import 'material_localizations.dart';
14 15 16
import 'theme.dart';

/// A widget representing a rotating expand/collapse button. The icon rotates
17
/// 180 degrees when pressed, then reverts the animation on a second press.
18 19
/// The underlying icon is [Icons.expand_more].
///
20 21 22 23
/// The expand icon does not include a semantic label for accessibility. In
/// order to be accessible it should be combined with a label using
/// [MergeSemantics]. This is done automatically by the [ExpansionPanel] widget.
///
24 25
/// See [IconButton] for a more general implementation of a pressable button
/// with an icon.
26 27 28 29
///
/// See also:
///
///  * https://material.io/design/iconography/system-icons.html
30 31 32
class ExpandIcon extends StatefulWidget {
  /// Creates an [ExpandIcon] with the given padding, and a callback that is
  /// triggered when the icon is pressed.
33
  const ExpandIcon({
34
    super.key,
35 36
    this.isExpanded = false,
    this.size = 24.0,
37
    required this.onPressed,
38
    this.padding = const EdgeInsets.all(8.0),
39 40 41
    this.color,
    this.disabledColor,
    this.expandedColor,
42 43
  }) : assert(isExpanded != null),
       assert(size != null),
44
       assert(padding != null);
45

46 47 48 49 50 51
  /// Whether the icon is in an expanded state.
  ///
  /// Rebuilding the widget with a different [isExpanded] value will trigger
  /// the animation, but will not trigger the [onPressed] callback.
  final bool isExpanded;

52 53 54 55 56 57
  /// The size of the icon.
  ///
  /// This property must not be null. It defaults to 24.0.
  final double size;

  /// The callback triggered when the icon is pressed and the state changes
58
  /// between expanded and collapsed. The value passed to the current state.
59 60
  ///
  /// If this is set to null, the button will be disabled.
61
  final ValueChanged<bool>? onPressed;
62

63
  /// The padding around the icon. The entire padded icon will react to input
64 65 66
  /// gestures.
  ///
  /// This property must not be null. It defaults to 8.0 padding on all sides.
67
  final EdgeInsetsGeometry padding;
68

69 70 71 72 73 74 75 76

  /// The color of the icon.
  ///
  /// Defaults to [Colors.black54] when the theme's
  /// [ThemeData.brightness] is [Brightness.light] and to
  /// [Colors.white60] when it is [Brightness.dark]. This adheres to the
  /// Material Design specifications for [icons](https://material.io/design/iconography/system-icons.html#color)
  /// and for [dark theme](https://material.io/design/color/dark-theme.html#ui-application)
77
  final Color? color;
78 79 80 81 82 83

  /// The color of the icon when it is disabled,
  /// i.e. if [onPressed] is null.
  ///
  /// Defaults to [Colors.black38] when the theme's
  /// [ThemeData.brightness] is [Brightness.light] and to
84
  /// [Colors.white38] when it is [Brightness.dark]. This adheres to the
85 86
  /// Material Design specifications for [icons](https://material.io/design/iconography/system-icons.html#color)
  /// and for [dark theme](https://material.io/design/color/dark-theme.html#ui-application)
87
  final Color? disabledColor;
88 89 90 91 92 93 94 95

  /// The color of the icon when the icon is expanded.
  ///
  /// Defaults to [Colors.black54] when the theme's
  /// [ThemeData.brightness] is [Brightness.light] and to
  /// [Colors.white] when it is [Brightness.dark]. This adheres to the
  /// Material Design specifications for [icons](https://material.io/design/iconography/system-icons.html#color)
  /// and for [dark theme](https://material.io/design/color/dark-theme.html#ui-application)
96
  final Color? expandedColor;
97

98
  @override
99
  State<ExpandIcon> createState() => _ExpandIconState();
100 101
}

102
class _ExpandIconState extends State<ExpandIcon> with SingleTickerProviderStateMixin {
103 104
  late AnimationController _controller;
  late Animation<double> _iconTurns;
105

106 107 108
  static final Animatable<double> _iconTurnTween = Tween<double>(begin: 0.0, end: 0.5)
    .chain(CurveTween(curve: Curves.fastOutSlowIn));

109 110 111
  @override
  void initState() {
    super.initState();
112
    _controller = AnimationController(duration: kThemeAnimationDuration, vsync: this);
113
    _iconTurns = _controller.drive(_iconTurnTween);
114 115 116 117
    // If the widget is initially expanded, rotate the icon without animating it.
    if (widget.isExpanded) {
      _controller.value = math.pi;
    }
118 119 120 121 122 123 124 125
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

126
  @override
127
  void didUpdateWidget(ExpandIcon oldWidget) {
128
    super.didUpdateWidget(oldWidget);
129 130
    if (widget.isExpanded != oldWidget.isExpanded) {
      if (widget.isExpanded) {
131
        _controller.forward();
132
      } else {
133
        _controller.reverse();
134 135 136
      }
    }
  }
137

138
  void _handlePressed() {
139
    widget.onPressed?.call(widget.isExpanded);
140 141
  }

142 143 144 145 146 147 148
  /// Default icon colors and opacities for when [Theme.brightness] is set to
  /// [Brightness.light] are based on the
  /// [Material Design system icon specifications](https://material.io/design/iconography/system-icons.html#color).
  /// Icon colors and opacities for [Brightness.dark] are based on the
  /// [Material Design dark theme specifications](https://material.io/design/color/dark-theme.html#ui-application)
  Color get _iconColor {
    if (widget.isExpanded && widget.expandedColor != null) {
149
      return widget.expandedColor!;
150 151 152
    }

    if (widget.color != null) {
153
      return widget.color!;
154 155
    }

156
    switch(Theme.of(context).brightness) {
157 158 159 160 161 162 163
      case Brightness.light:
        return Colors.black54;
      case Brightness.dark:
        return Colors.white60;
    }
  }

164 165 166
  @override
  Widget build(BuildContext context) {
    assert(debugCheckHasMaterial(context));
167
    assert(debugCheckHasMaterialLocalizations(context));
168
    final MaterialLocalizations localizations = MaterialLocalizations.of(context);
169 170
    final String onTapHint = widget.isExpanded ? localizations.expandedIconTapHint : localizations.collapsedIconTapHint;

171
    return Semantics(
172
      onTapHint: widget.onPressed == null ? null : onTapHint,
173
      child: IconButton(
174
        padding: widget.padding,
175
        iconSize: widget.size,
176 177
        color: _iconColor,
        disabledColor: widget.disabledColor,
178
        onPressed: widget.onPressed == null ? null : _handlePressed,
179
        icon: RotationTransition(
180
          turns: _iconTurns,
181
          child: const Icon(Icons.expand_more),
182 183
        ),
      ),
184 185 186
    );
  }
}