和Chip组件类似的样式,具有选中与否的属性和选中事件。当选中时左侧组件上层会被✔️遮罩。

相关组件

Chip ChoiceChip ActionChip InputChip RawChip

FilterChip可接受选择事件

【selected】: 是否选择 【bool】
【onSelected】: 选择事件 【Function(bool)】
【selectedColor】: 选择后的颜色 【Color】
【selectedShadowColor】: 选择后的阴影颜色 【Color】,
17.gif

  1. import 'package:flutter/material.dart';
  2. class CustomFilterChip extends StatefulWidget {
  3. @override
  4. _CustomFilterChipState createState() => _CustomFilterChipState();
  5. }
  6. class _CustomFilterChipState extends State<CustomFilterChip> {
  7. final Map<String, String> map = {
  8. 'A': 'Ant',
  9. 'B': 'Bug',
  10. 'C': 'Cat',
  11. 'D': 'Dog',
  12. };
  13. List<String> _selected = <String>[];
  14. @override
  15. Widget build(BuildContext context) {
  16. return Column(
  17. mainAxisAlignment: MainAxisAlignment.center,
  18. children: <Widget>[
  19. Wrap(
  20. children: map.keys.map((key) =>
  21. _buildChild(key)).toList(),
  22. ),
  23. Container(
  24. padding: EdgeInsets.all(10),
  25. child: Text('您已选择: ${_selected.join(', ')}')),
  26. ],
  27. );
  28. }
  29. Padding _buildChild(String key) {
  30. return Padding(
  31. padding: const EdgeInsets.all(4.0),
  32. child: FilterChip(
  33. selectedColor: Colors.orange.withAlpha(55),
  34. selectedShadowColor: Colors.blue,
  35. shadowColor: Colors.orangeAccent,
  36. pressElevation: 5,
  37. elevation: 3,
  38. avatar: CircleAvatar(child: Text(key)),
  39. label: Text(map[key]),
  40. selected: _selected.contains(map[key]),
  41. onSelected: (bool value) {
  42. setState(() {
  43. if (value) {
  44. _selected.add(map[key]);
  45. } else {
  46. _selected.removeWhere((name) => name == map[key]);
  47. }
  48. });
  49. },
  50. ),
  51. );
  52. }
  53. }