和Chip组件类似的样式,具有选中与否的属性和选中事件。当选中时左侧组件上层会被✔️遮罩。
相关组件
FilterChip可接受选择事件
【selected】: 是否选择 【bool】
【onSelected】: 选择事件 【Function(bool)】
【selectedColor】: 选择后的颜色 【Color】
【selectedShadowColor】: 选择后的阴影颜色 【Color】,
import 'package:flutter/material.dart';
class CustomFilterChip extends StatefulWidget {
@override
_CustomFilterChipState createState() => _CustomFilterChipState();
}
class _CustomFilterChipState extends State<CustomFilterChip> {
final Map<String, String> map = {
'A': 'Ant',
'B': 'Bug',
'C': 'Cat',
'D': 'Dog',
};
List<String> _selected = <String>[];
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Wrap(
children: map.keys.map((key) =>
_buildChild(key)).toList(),
),
Container(
padding: EdgeInsets.all(10),
child: Text('您已选择: ${_selected.join(', ')}')),
],
);
}
Padding _buildChild(String key) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: FilterChip(
selectedColor: Colors.orange.withAlpha(55),
selectedShadowColor: Colors.blue,
shadowColor: Colors.orangeAccent,
pressElevation: 5,
elevation: 3,
avatar: CircleAvatar(child: Text(key)),
label: Text(map[key]),
selected: _selected.contains(map[key]),
onSelected: (bool value) {
setState(() {
if (value) {
_selected.add(map[key]);
} else {
_selected.removeWhere((name) => name == map[key]);
}
});
},
),
);
}
}