checkbox_button.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import 'package:flutter/material.dart';
  2. /// 按钮式多选框组
  3. class VCheckBoxButtonGroup<T, TValue> extends StatefulWidget {
  4. final List<T> source;
  5. final List<TValue>? values;
  6. final String Function(T data) labelGetter;
  7. final TValue Function(T data) valueGetter;
  8. final ValueChanged<List<TValue>>? onChanged;
  9. final double? itemWidth;
  10. const VCheckBoxButtonGroup({
  11. super.key,
  12. required this.source,
  13. this.values,
  14. required this.labelGetter,
  15. required this.valueGetter,
  16. this.onChanged,
  17. this.itemWidth,
  18. });
  19. @override
  20. State<StatefulWidget> createState() => _VCheckBoxGroupState<T, TValue>();
  21. }
  22. class _VCheckBoxGroupState<T, TValue>
  23. extends State<VCheckBoxButtonGroup<T, TValue>> {
  24. late List<TValue> _checkedValues;
  25. @override
  26. void initState() {
  27. if (widget.values != null) {
  28. _checkedValues = List.from(widget.values!);
  29. } else {
  30. _checkedValues = [];
  31. }
  32. super.initState();
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. final children = <Widget>[];
  37. final length = widget.source.length;
  38. for (var i = 0; i < length; i++) {
  39. final e = widget.source[i];
  40. final value = widget.valueGetter(e);
  41. final isChecked = _checkedValues.contains(value);
  42. children.add(
  43. SizedBox(
  44. width: widget.itemWidth ?? 160,
  45. child: VCheckBoxButton(
  46. label: widget.labelGetter(e),
  47. isChecked: isChecked,
  48. onChanged: (status) {
  49. _onItemChanged(value, status);
  50. },
  51. ),
  52. ),
  53. );
  54. }
  55. return Wrap(
  56. spacing: 12,
  57. runSpacing: 8,
  58. children: children,
  59. );
  60. }
  61. void _onItemChanged(TValue value, bool isChecked) {
  62. if (isChecked) {
  63. if (_checkedValues.contains(value) == false) {
  64. _checkedValues.add(value);
  65. }
  66. } else {
  67. _checkedValues.remove(value);
  68. }
  69. widget.onChanged?.call(_checkedValues);
  70. }
  71. }
  72. /// 按钮式多选框
  73. class VCheckBoxButton extends StatefulWidget {
  74. /// 文本
  75. final String label;
  76. /// 是否默认选中
  77. final bool? isChecked;
  78. /// 选中状态变更
  79. final ValueChanged<bool>? onChanged;
  80. const VCheckBoxButton({
  81. super.key,
  82. required this.label,
  83. this.isChecked,
  84. this.onChanged,
  85. });
  86. @override
  87. State<StatefulWidget> createState() => _VCheckBoxState();
  88. }
  89. class _VCheckBoxState extends State<VCheckBoxButton> {
  90. bool _isChecked = false;
  91. @override
  92. void initState() {
  93. if (widget.isChecked != null) {
  94. _isChecked = widget.isChecked!;
  95. }
  96. super.initState();
  97. }
  98. @override
  99. Widget build(BuildContext context) {
  100. const height = 56.0;
  101. const borderRadius = height / 2;
  102. final primaryColor = Theme.of(context).primaryColor;
  103. return Material(
  104. child: Ink(
  105. child: InkWell(
  106. borderRadius: BorderRadius.circular(borderRadius),
  107. onTap: () {
  108. setState(() {
  109. _isChecked = !_isChecked;
  110. widget.onChanged?.call(_isChecked);
  111. });
  112. },
  113. child: Container(
  114. padding: const EdgeInsets.only(left: 0, right: borderRadius),
  115. alignment: Alignment.center,
  116. height: height,
  117. decoration: BoxDecoration(
  118. color: _isChecked ? primaryColor : Colors.grey.shade200,
  119. borderRadius: BorderRadius.circular(borderRadius),
  120. border: _isChecked ? null : Border.all(color: primaryColor),
  121. ),
  122. child: Row(
  123. mainAxisSize: MainAxisSize.min,
  124. crossAxisAlignment: CrossAxisAlignment.center,
  125. children: [
  126. if (_isChecked) ...[
  127. const Icon(
  128. Icons.check_rounded,
  129. color: Colors.green,
  130. size: 24,
  131. ),
  132. const SizedBox(width: 8),
  133. ],
  134. if (!_isChecked) const SizedBox(width: 32),
  135. Text(
  136. widget.label,
  137. style: TextStyle(
  138. color: _isChecked ? Colors.white : primaryColor,
  139. fontSize: 20,
  140. ),
  141. ),
  142. ],
  143. ),
  144. ),
  145. ),
  146. ),
  147. );
  148. }
  149. }