expansion_tile.dart 6.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2017 The Chromium 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 '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 38 39
  const ExpansionTile({
    Key key,
    this.leading,
    @required this.title,
    this.backgroundColor,
    this.onExpansionChanged,
40
    this.children = const <Widget>[],
41
    this.trailing,
42
    this.initiallyExpanded = false,
43 44
  }) : assert(initiallyExpanded != null),
       super(key: key);
45 46 47 48 49 50 51 52 53 54 55 56 57 58

  /// 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;

  /// Called when the tile expands or collapses.
  ///
  /// When the tile starts expanding, this function is called with the value
59
  /// true. When the tile starts collapsing, this function is called with
60 61 62 63 64 65 66 67 68 69
  /// 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;
70

71 72
  /// A widget to display instead of a rotating arrow icon.
  final Widget trailing;
73

74 75 76
  /// Specifies if the list tile is initially expanded (true) or collapsed (false, the default).
  final bool initiallyExpanded;

77
  @override
78
  _ExpansionTileState createState() => _ExpansionTileState();
79 80 81
}

class _ExpansionTileState extends State<ExpansionTile> with SingleTickerProviderStateMixin {
82 83 84 85 86 87 88 89 90
  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();

91 92
  AnimationController _controller;
  Animation<double> _iconTurns;
93 94 95 96 97
  Animation<double> _heightFactor;
  Animation<Color> _borderColor;
  Animation<Color> _headerColor;
  Animation<Color> _iconColor;
  Animation<Color> _backgroundColor;
98 99 100 101 102 103

  bool _isExpanded = false;

  @override
  void initState() {
    super.initState();
104
    _controller = AnimationController(duration: _kExpand, vsync: this);
105 106 107 108 109 110
    _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));
111

112
    _isExpanded = PageStorage.of(context)?.readState(context) ?? widget.initiallyExpanded;
113 114 115 116 117 118 119 120 121 122 123 124 125
    if (_isExpanded)
      _controller.value = 1.0;
  }

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

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

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

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

  @override
182
  void didChangeDependencies() {
183
    final ThemeData theme = Theme.of(context);
184 185 186
    _borderColorTween
      ..end = theme.dividerColor;
    _headerColorTween
187 188
      ..begin = theme.textTheme.subhead.color
      ..end = theme.accentColor;
189
    _iconColorTween
190 191
      ..begin = theme.unselectedWidgetColor
      ..end = theme.accentColor;
192 193 194 195
    _backgroundColorTween
      ..end = widget.backgroundColor;
    super.didChangeDependencies();
  }
196

197 198
  @override
  Widget build(BuildContext context) {
199
    final bool closed = !_isExpanded && _controller.isDismissed;
200
    return AnimatedBuilder(
201 202
      animation: _controller.view,
      builder: _buildChildren,
203
      child: closed ? null : Column(children: widget.children),
204
    );
205

206 207
  }
}