expansion_tile.dart 6.66 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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';

import 'colors.dart';
import 'icons.dart';
import 'list_tile.dart';
import 'theme.dart';
import 'theme_data.dart';

13
const Duration _kExpand = Duration(milliseconds: 200);
14 15 16 17 18

/// A single-line [ListTile] with a trailing button that expands or collapses
/// the tile to reveal or hide the [children].
///
/// This widget is typically used with [ListView] to create an
19
/// "expand / collapse" list entry. When used with scrolling widgets like
20 21 22
/// [ListView], a unique [PageStorageKey] must be specified to enable the
/// [ExpansionTile] to save and restore its expanded state when it is scrolled
/// in and out of view.
23 24 25 26 27 28 29 30 31
///
/// See also:
///
///  * [ListTile], useful for creating expansion tile [children] when the
///    expansion tile represents a sublist.
///  * The "Expand/collapse" section of
///    <https://material.io/guidelines/components/lists-controls.html>.
class ExpansionTile extends StatefulWidget {
  /// Creates a single-line [ListTile] with a trailing button that expands or collapses
32 33
  /// the tile to reveal or hide the [children]. The [initiallyExpanded] property must
  /// be non-null.
34 35 36 37
  const ExpansionTile({
    Key key,
    this.leading,
    @required this.title,
38
    this.subtitle,
39 40
    this.backgroundColor,
    this.onExpansionChanged,
41
    this.children = const <Widget>[],
42
    this.trailing,
43
    this.initiallyExpanded = false,
44 45
  }) : assert(initiallyExpanded != null),
       super(key: key);
46 47 48 49 50 51 52 53 54 55 56

  /// A widget to display before the title.
  ///
  /// Typically a [CircleAvatar] widget.
  final Widget leading;

  /// The primary content of the list item.
  ///
  /// Typically a [Text] widget.
  final Widget title;

57 58 59 60 61
  /// Additional content displayed below the title.
  ///
  /// Typically a [Text] widget.
  final Widget subtitle;

62 63 64
  /// Called when the tile expands or collapses.
  ///
  /// When the tile starts expanding, this function is called with the value
65
  /// true. When the tile starts collapsing, this function is called with
66 67 68 69 70 71 72 73 74 75
  /// the value false.
  final ValueChanged<bool> onExpansionChanged;

  /// The widgets that are displayed when the tile expands.
  ///
  /// Typically [ListTile] widgets.
  final List<Widget> children;

  /// The color to display behind the sublist when expanded.
  final Color backgroundColor;
76

77 78
  /// A widget to display instead of a rotating arrow icon.
  final Widget trailing;
79

80 81 82
  /// Specifies if the list tile is initially expanded (true) or collapsed (false, the default).
  final bool initiallyExpanded;

83
  @override
84
  _ExpansionTileState createState() => _ExpansionTileState();
85 86 87
}

class _ExpansionTileState extends State<ExpansionTile> with SingleTickerProviderStateMixin {
88 89 90 91 92 93 94 95 96
  static final Animatable<double> _easeOutTween = CurveTween(curve: Curves.easeOut);
  static final Animatable<double> _easeInTween = CurveTween(curve: Curves.easeIn);
  static final Animatable<double> _halfTween = Tween<double>(begin: 0.0, end: 0.5);

  final ColorTween _borderColorTween = ColorTween();
  final ColorTween _headerColorTween = ColorTween();
  final ColorTween _iconColorTween = ColorTween();
  final ColorTween _backgroundColorTween = ColorTween();

97 98
  AnimationController _controller;
  Animation<double> _iconTurns;
99 100 101 102 103
  Animation<double> _heightFactor;
  Animation<Color> _borderColor;
  Animation<Color> _headerColor;
  Animation<Color> _iconColor;
  Animation<Color> _backgroundColor;
104 105 106 107 108 109

  bool _isExpanded = false;

  @override
  void initState() {
    super.initState();
110
    _controller = AnimationController(duration: _kExpand, vsync: this);
111 112 113 114 115 116
    _heightFactor = _controller.drive(_easeInTween);
    _iconTurns = _controller.drive(_halfTween.chain(_easeInTween));
    _borderColor = _controller.drive(_borderColorTween.chain(_easeOutTween));
    _headerColor = _controller.drive(_headerColorTween.chain(_easeInTween));
    _iconColor = _controller.drive(_iconColorTween.chain(_easeInTween));
    _backgroundColor = _controller.drive(_backgroundColorTween.chain(_easeOutTween));
117

118
    _isExpanded = PageStorage.of(context)?.readState(context) as bool ?? widget.initiallyExpanded;
119 120 121 122 123 124 125 126 127 128 129 130 131
    if (_isExpanded)
      _controller.value = 1.0;
  }

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

  void _handleTap() {
    setState(() {
      _isExpanded = !_isExpanded;
132
      if (_isExpanded) {
133
        _controller.forward();
134 135 136 137
      } else {
        _controller.reverse().then<void>((void value) {
          if (!mounted)
            return;
138 139 140 141
          setState(() {
            // Rebuild without widget.children.
          });
        });
142
      }
143 144 145 146 147 148 149
      PageStorage.of(context)?.writeState(context, _isExpanded);
    });
    if (widget.onExpansionChanged != null)
      widget.onExpansionChanged(_isExpanded);
  }

  Widget _buildChildren(BuildContext context, Widget child) {
150
    final Color borderSideColor = _borderColor.value ?? Colors.transparent;
151

152 153
    return Container(
      decoration: BoxDecoration(
154
        color: _backgroundColor.value ?? Colors.transparent,
155 156 157
        border: Border(
          top: BorderSide(color: borderSideColor),
          bottom: BorderSide(color: borderSideColor),
158
        ),
159
      ),
160
      child: Column(
161 162
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
163 164 165
          ListTileTheme.merge(
            iconColor: _iconColor.value,
            textColor: _headerColor.value,
166
            child: ListTile(
167 168
              onTap: _handleTap,
              leading: widget.leading,
169
              title: widget.title,
170
              subtitle: widget.subtitle,
171
              trailing: widget.trailing ?? RotationTransition(
172 173 174 175 176
                turns: _iconTurns,
                child: const Icon(Icons.expand_more),
              ),
            ),
          ),
177 178
          ClipRect(
            child: Align(
179
              heightFactor: _heightFactor.value,
180 181 182 183 184 185 186 187 188
              child: child,
            ),
          ),
        ],
      ),
    );
  }

  @override
189
  void didChangeDependencies() {
190
    final ThemeData theme = Theme.of(context);
191 192 193
    _borderColorTween
      ..end = theme.dividerColor;
    _headerColorTween
194
      ..begin = theme.textTheme.subtitle1.color
195
      ..end = theme.accentColor;
196
    _iconColorTween
197 198
      ..begin = theme.unselectedWidgetColor
      ..end = theme.accentColor;
199 200 201 202
    _backgroundColorTween
      ..end = widget.backgroundColor;
    super.didChangeDependencies();
  }
203

204 205
  @override
  Widget build(BuildContext context) {
206
    final bool closed = !_isExpanded && _controller.isDismissed;
207
    return AnimatedBuilder(
208 209
      animation: _controller.view,
      builder: _buildChildren,
210
      child: closed ? null : Column(children: widget.children),
211
    );
212

213 214
  }
}