可展开的列表组件,可根据逻辑来实现单展开或多展开。可指定展开动画时长,接收展开回调

相关组件

ExpansionTile

ExpansionPanelList基本使用

【children】 : 子组件列表 【List
【animationDuration】 : 动画时长 【Duration】
【expansionCallback】 : 展开回调 【List
【onPressed】 : 点击事件 【Function()】
157.gif

  1. import 'package:flutter/material.dart';
  2. class CustomExpansionPanelList extends StatefulWidget {
  3. @override
  4. _CustomExpansionPanelListState createState() =>
  5. _CustomExpansionPanelListState();
  6. }
  7. class _CustomExpansionPanelListState extends State<CustomExpansionPanelList> {
  8. var data = <Color>[
  9. Colors.red[50],
  10. Colors.red[100],
  11. Colors.red[200],
  12. Colors.red[300],
  13. Colors.red[400],
  14. Colors.red[500],
  15. Colors.red[600],
  16. Colors.red[700],
  17. Colors.red[800],
  18. Colors.red[900],
  19. ];
  20. int _position = 0;
  21. @override
  22. Widget build(BuildContext context) {
  23. return Container(
  24. width: 300,
  25. child: ExpansionPanelList(
  26. children: data.map((color) => _buildItem(color)).toList(),
  27. animationDuration: Duration(milliseconds: 200),
  28. expansionCallback: (index, open) {
  29. setState(() => _position=open?-1:index);
  30. },
  31. ),
  32. );
  33. }
  34. ExpansionPanel _buildItem(Color color) {
  35. return ExpansionPanel(
  36. isExpanded: data.indexOf(color) == _position,
  37. canTapOnHeader: true,
  38. headerBuilder: (ctx, index) => Center(
  39. child: Wrap(
  40. crossAxisAlignment: WrapCrossAlignment.center,
  41. children: <Widget>[
  42. Container(
  43. height: 30,
  44. width: 30,
  45. decoration:
  46. BoxDecoration(color: color, shape: BoxShape.circle),
  47. ),
  48. Container(
  49. width: 120,
  50. alignment: Alignment.center,
  51. height: 50,
  52. child: Text(
  53. colorString(color),
  54. style: TextStyle(color: Colors.black),
  55. ),
  56. ),
  57. ],
  58. ),
  59. ),
  60. body: Container(
  61. alignment: Alignment.center,
  62. height: 50,
  63. color: color,
  64. child: Text(
  65. colorString(color),
  66. style: TextStyle(color: Colors.white, shadows: [
  67. Shadow(color: Colors.black, offset: Offset(.5, .5), blurRadius: 2)
  68. ]),
  69. ),
  70. ));
  71. }
  72. String colorString(Color color) =>
  73. "#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";
  74. }