my_checkbox_group.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import 'package:cs_resources/theme/app_colors_theme.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:shared/utils/log_utils.dart';
  4. class MyCheckboxGroup extends StatefulWidget {
  5. final List<Map<String, dynamic>> items;
  6. final bool isSingleSelect;
  7. final String nameStr;
  8. final String valueStr;
  9. final ValueChanged<List<Map<String, dynamic>>> onChanged;
  10. final List<Map<String, dynamic>>? defaultSelectedItems;
  11. final TextStyle? labelStyle;
  12. final Color? acviteColor;
  13. final Color? borderSideColor;
  14. const MyCheckboxGroup({super.key,
  15. required this.items,
  16. this.isSingleSelect = false,
  17. this.valueStr = 'id',
  18. this.nameStr = 'name',
  19. required this.onChanged,
  20. this.defaultSelectedItems,
  21. this.acviteColor = const Color(0xFF4161D0),
  22. this.borderSideColor = const Color(0xFF4161D0),
  23. this.labelStyle = const TextStyle(
  24. fontSize: 14,
  25. fontWeight: FontWeight.w500,
  26. ),
  27. });
  28. @override
  29. _MyCheckboxGroupState createState() => _MyCheckboxGroupState();
  30. }
  31. class _MyCheckboxGroupState extends State<MyCheckboxGroup> {
  32. Set<String> _selectedItemKeys = {};
  33. @override
  34. void initState() {
  35. super.initState();
  36. if (widget.defaultSelectedItems != null) {
  37. _selectedItemKeys.addAll(widget.defaultSelectedItems!.map((item) => item[widget.valueStr]!.toString()));
  38. widget.onChanged(widget.items.where((item) => _selectedItemKeys.contains(item[widget.valueStr]!.toString())).toList());
  39. }
  40. }
  41. void _toggleSelection(String key) {
  42. if (widget.isSingleSelect) {
  43. setState(() {
  44. _selectedItemKeys.clear();
  45. _selectedItemKeys.add(key);
  46. });
  47. } else {
  48. setState(() {
  49. if (_selectedItemKeys.contains(key)) {
  50. _selectedItemKeys.remove(key);
  51. } else {
  52. _selectedItemKeys.add(key);
  53. }
  54. });
  55. }
  56. widget.onChanged(widget.items.where((item) => _selectedItemKeys.contains(item[widget.valueStr]!.toString())).toList());
  57. }
  58. @override
  59. Widget build(BuildContext context) {
  60. return Column(
  61. children: widget.items.map((item) {
  62. final key = item[widget.valueStr]!.toString();
  63. final label = item[widget.nameStr]! as String;
  64. return Container(
  65. child: Row(
  66. children: [
  67. Checkbox(
  68. activeColor: widget.acviteColor ?? context.appColors.textPrimary,
  69. side: BorderSide(
  70. color: widget.borderSideColor ?? context.appColors.textPrimary,
  71. ),
  72. value: _selectedItemKeys.contains(key),
  73. onChanged: (bool? checked) {
  74. if (checked != null) {
  75. _toggleSelection(key);
  76. }
  77. },
  78. ),
  79. Text(
  80. label,
  81. style: widget.labelStyle ?? const TextStyle(
  82. fontSize: 14,
  83. fontWeight: FontWeight.w500,
  84. ),
  85. ),
  86. ],
  87. ),
  88. );
  89. }).toList(),
  90. );
  91. }
  92. }