可展开的列表组件,可根据逻辑来实现单展开或多展开。可指定展开动画时长,接收展开回调
相关组件
ExpansionPanelList基本使用
【children】 : 子组件列表 【List
【animationDuration】 : 动画时长 【Duration】
【expansionCallback】 : 展开回调 【List
【onPressed】 : 点击事件 【Function()】
import 'package:flutter/material.dart';
class CustomExpansionPanelList extends StatefulWidget {
@override
_CustomExpansionPanelListState createState() =>
_CustomExpansionPanelListState();
}
class _CustomExpansionPanelListState extends State<CustomExpansionPanelList> {
var data = <Color>[
Colors.red[50],
Colors.red[100],
Colors.red[200],
Colors.red[300],
Colors.red[400],
Colors.red[500],
Colors.red[600],
Colors.red[700],
Colors.red[800],
Colors.red[900],
];
int _position = 0;
@override
Widget build(BuildContext context) {
return Container(
width: 300,
child: ExpansionPanelList(
children: data.map((color) => _buildItem(color)).toList(),
animationDuration: Duration(milliseconds: 200),
expansionCallback: (index, open) {
setState(() => _position=open?-1:index);
},
),
);
}
ExpansionPanel _buildItem(Color color) {
return ExpansionPanel(
isExpanded: data.indexOf(color) == _position,
canTapOnHeader: true,
headerBuilder: (ctx, index) => Center(
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: <Widget>[
Container(
height: 30,
width: 30,
decoration:
BoxDecoration(color: color, shape: BoxShape.circle),
),
Container(
width: 120,
alignment: Alignment.center,
height: 50,
child: Text(
colorString(color),
style: TextStyle(color: Colors.black),
),
),
],
),
),
body: Container(
alignment: Alignment.center,
height: 50,
color: color,
child: Text(
colorString(color),
style: TextStyle(color: Colors.white, shadows: [
Shadow(color: Colors.black, offset: Offset(.5, .5), blurRadius: 2)
]),
),
));
}
String colorString(Color color) =>
"#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";
}