|
| 1 | +// Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | + |
| 7 | +const Duration _kExpand = Duration(milliseconds: 200); |
| 8 | + |
| 9 | +/// A single-line [ListTile] with a trailing button that expands or collapses |
| 10 | +/// the tile to reveal or hide the [children]. |
| 11 | +/// |
| 12 | +/// This widget is typically used with [ListView] to create an |
| 13 | +/// "expand / collapse" list entry. When used with scrolling widgets like |
| 14 | +/// [ListView], a unique [PageStorageKey] must be specified to enable the |
| 15 | +/// [ExpansionTile] to save and restore its expanded state when it is scrolled |
| 16 | +/// in and out of view. |
| 17 | +/// |
| 18 | +/// See also: |
| 19 | +/// |
| 20 | +/// * [ListTile], useful for creating expansion tile [children] when the |
| 21 | +/// expansion tile represents a sublist. |
| 22 | +/// * The "Expand/collapse" section of |
| 23 | +/// <https://material.io/guidelines/components/lists-controls.html>. |
| 24 | +class ExpansionTile extends StatefulWidget { |
| 25 | + /// Creates a single-line [ListTile] with a trailing button that expands or collapses |
| 26 | + /// the tile to reveal or hide the [children]. The [initiallyExpanded] property must |
| 27 | + /// be non-null. |
| 28 | + const ExpansionTile({ |
| 29 | + Key key, |
| 30 | + this.headerBackgroundColor, |
| 31 | + this.leading, |
| 32 | + @required this.title, |
| 33 | + this.backgroundColor, |
| 34 | + this.iconColor, |
| 35 | + this.onExpansionChanged, |
| 36 | + this.children = const <Widget>[], |
| 37 | + this.trailing, |
| 38 | + this.initiallyExpanded = false, |
| 39 | + }) : assert(initiallyExpanded != null), |
| 40 | + super(key: key); |
| 41 | + |
| 42 | + /// A widget to display before the title. |
| 43 | + /// |
| 44 | + /// Typically a [CircleAvatar] widget. |
| 45 | + final Widget leading; |
| 46 | + |
| 47 | + /// The primary content of the list item. |
| 48 | + /// |
| 49 | + /// Typically a [Text] widget. |
| 50 | + final Widget title; |
| 51 | + |
| 52 | + /// Called when the tile expands or collapses. |
| 53 | + /// |
| 54 | + /// When the tile starts expanding, this function is called with the value |
| 55 | + /// true. When the tile starts collapsing, this function is called with |
| 56 | + /// the value false. |
| 57 | + final ValueChanged<bool> onExpansionChanged; |
| 58 | + |
| 59 | + /// The widgets that are displayed when the tile expands. |
| 60 | + /// |
| 61 | + /// Typically [ListTile] widgets. |
| 62 | + final List<Widget> children; |
| 63 | + |
| 64 | + /// The color to display behind the sublist when expanded. |
| 65 | + final Color backgroundColor; |
| 66 | + |
| 67 | + /// The color to display the background of the header. |
| 68 | + final Color headerBackgroundColor; |
| 69 | + |
| 70 | + /// The color to display the icon of the header. |
| 71 | + final Color iconColor; |
| 72 | + |
| 73 | + /// A widget to display instead of a rotating arrow icon. |
| 74 | + final Widget trailing; |
| 75 | + |
| 76 | + /// Specifies if the list tile is initially expanded (true) or collapsed (false, the default). |
| 77 | + final bool initiallyExpanded; |
| 78 | + |
| 79 | + @override |
| 80 | + _ExpansionTileState createState() => _ExpansionTileState(); |
| 81 | +} |
| 82 | + |
| 83 | +class _ExpansionTileState extends State<ExpansionTile> |
| 84 | + with SingleTickerProviderStateMixin { |
| 85 | + static final Animatable<double> _easeOutTween = |
| 86 | + CurveTween(curve: Curves.easeOut); |
| 87 | + static final Animatable<double> _easeInTween = |
| 88 | + CurveTween(curve: Curves.easeIn); |
| 89 | + static final Animatable<double> _halfTween = |
| 90 | + Tween<double>(begin: 0.0, end: 0.5); |
| 91 | + |
| 92 | + final ColorTween _borderColorTween = ColorTween(); |
| 93 | + final ColorTween _headerColorTween = ColorTween(); |
| 94 | + final ColorTween _iconColorTween = ColorTween(); |
| 95 | + final ColorTween _backgroundColorTween = ColorTween(); |
| 96 | + |
| 97 | + AnimationController _controller; |
| 98 | + Animation<double> _iconTurns; |
| 99 | + Animation<double> _heightFactor; |
| 100 | + Animation<Color> _borderColor; |
| 101 | + Animation<Color> _headerColor; |
| 102 | + Animation<Color> _iconColor; |
| 103 | + Animation<Color> _backgroundColor; |
| 104 | + |
| 105 | + bool _isExpanded = false; |
| 106 | + |
| 107 | + @override |
| 108 | + void initState() { |
| 109 | + super.initState(); |
| 110 | + _controller = AnimationController(duration: _kExpand, vsync: this); |
| 111 | + _heightFactor = _controller.drive(_easeInTween); |
| 112 | + _iconTurns = _controller.drive(_halfTween.chain(_easeInTween)); |
| 113 | + _borderColor = _controller.drive(_borderColorTween.chain(_easeOutTween)); |
| 114 | + _headerColor = _controller.drive(_headerColorTween.chain(_easeInTween)); |
| 115 | + _iconColor = _controller.drive(_iconColorTween.chain(_easeInTween)); |
| 116 | + _backgroundColor = |
| 117 | + _controller.drive(_backgroundColorTween.chain(_easeOutTween)); |
| 118 | + |
| 119 | + _isExpanded = |
| 120 | + PageStorage.of(context)?.readState(context) ?? widget.initiallyExpanded; |
| 121 | + if (_isExpanded) _controller.value = 1.0; |
| 122 | + } |
| 123 | + |
| 124 | + @override |
| 125 | + void dispose() { |
| 126 | + _controller.dispose(); |
| 127 | + super.dispose(); |
| 128 | + } |
| 129 | + |
| 130 | + void _handleTap() { |
| 131 | + setState(() { |
| 132 | + _isExpanded = !_isExpanded; |
| 133 | + if (_isExpanded) { |
| 134 | + _controller.forward(); |
| 135 | + } else { |
| 136 | + _controller.reverse().then<void>((void value) { |
| 137 | + if (!mounted) return; |
| 138 | + setState(() { |
| 139 | + // Rebuild without widget.children. |
| 140 | + }); |
| 141 | + }); |
| 142 | + } |
| 143 | + PageStorage.of(context)?.writeState(context, _isExpanded); |
| 144 | + }); |
| 145 | + if (widget.onExpansionChanged != null) |
| 146 | + widget.onExpansionChanged(_isExpanded); |
| 147 | + } |
| 148 | + |
| 149 | + Widget _buildChildren(BuildContext context, Widget child) { |
| 150 | + final Color borderSideColor = _borderColor.value ?? Colors.transparent; |
| 151 | + final Color titleColor = _headerColor.value; |
| 152 | + |
| 153 | + return Container( |
| 154 | + decoration: BoxDecoration( |
| 155 | + color: _backgroundColor.value ?? Colors.transparent, |
| 156 | + border: Border( |
| 157 | + top: BorderSide(color: borderSideColor), |
| 158 | + bottom: BorderSide(color: borderSideColor), |
| 159 | + )), |
| 160 | + child: Column( |
| 161 | + mainAxisSize: MainAxisSize.min, |
| 162 | + children: <Widget>[ |
| 163 | + IconTheme.merge( |
| 164 | + data: IconThemeData(color: _iconColor.value), |
| 165 | + child: Container( |
| 166 | + color: widget.headerBackgroundColor ?? Colors.black, |
| 167 | + child: ListTile( |
| 168 | + onTap: _handleTap, |
| 169 | + leading: widget.leading, |
| 170 | + title: DefaultTextStyle( |
| 171 | + style: Theme.of(context) |
| 172 | + .textTheme |
| 173 | + .subhead |
| 174 | + .copyWith(color: titleColor), |
| 175 | + child: widget.title, |
| 176 | + ), |
| 177 | + trailing: widget.trailing ?? |
| 178 | + RotationTransition( |
| 179 | + turns: _iconTurns, |
| 180 | + child: Icon( |
| 181 | + Icons.expand_more, |
| 182 | + color: widget.iconColor ?? Colors.grey, |
| 183 | + ), |
| 184 | + ), |
| 185 | + ), |
| 186 | + ), |
| 187 | + ), |
| 188 | + ClipRect( |
| 189 | + child: Align( |
| 190 | + heightFactor: _heightFactor.value, |
| 191 | + child: child, |
| 192 | + ), |
| 193 | + ), |
| 194 | + ], |
| 195 | + ), |
| 196 | + ); |
| 197 | + } |
| 198 | + |
| 199 | + @override |
| 200 | + void didChangeDependencies() { |
| 201 | + final ThemeData theme = Theme.of(context); |
| 202 | + _borderColorTween..end = theme.dividerColor; |
| 203 | + _headerColorTween |
| 204 | + ..begin = theme.textTheme.subhead.color |
| 205 | + ..end = theme.accentColor; |
| 206 | + _iconColorTween |
| 207 | + ..begin = theme.unselectedWidgetColor |
| 208 | + ..end = theme.accentColor; |
| 209 | + _backgroundColorTween..end = widget.backgroundColor; |
| 210 | + super.didChangeDependencies(); |
| 211 | + } |
| 212 | + |
| 213 | + @override |
| 214 | + Widget build(BuildContext context) { |
| 215 | + final bool closed = !_isExpanded && _controller.isDismissed; |
| 216 | + return AnimatedBuilder( |
| 217 | + animation: _controller.view, |
| 218 | + builder: _buildChildren, |
| 219 | + child: closed ? null : Column(children: widget.children), |
| 220 | + ); |
| 221 | + } |
| 222 | +} |
0 commit comments