custom_check_box.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import 'package:cs_resources/constants/color_constants.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:get/get_utils/get_utils.dart';
  5. import 'package:shared/utils/log_utils.dart';
  6. import 'package:widgets/ext/ex_widget.dart';
  7. import 'package:widgets/my_load_image.dart';
  8. import 'package:widgets/my_text_view.dart';
  9. class CustomCheckBox extends StatefulWidget {
  10. final List<String> options;
  11. final Function(List<int> selectedIndexes) onOptionsSelected; // 选中项的索引回调
  12. final List<String> selectedOptions; // 已选中的选项列表
  13. CustomCheckBox({
  14. required this.options,
  15. required this.onOptionsSelected,
  16. required this.selectedOptions,
  17. });
  18. @override
  19. _CustomCheckBoxState createState() => _CustomCheckBoxState();
  20. }
  21. class _CustomCheckBoxState extends State<CustomCheckBox> {
  22. late List<String> _selectedOptions;
  23. late List<int> _selectedIndexes;
  24. @override
  25. void initState() {
  26. super.initState();
  27. _initializeSelectedOptions();
  28. }
  29. void _initializeSelectedOptions() {
  30. _selectedOptions = List.from(widget.selectedOptions);
  31. _selectedIndexes =
  32. widget.options.asMap().entries.where((entry) => _selectedOptions.contains(entry.value)).map((entry) => entry.key).toList(); // 根据选项匹配初始化索引
  33. }
  34. @override
  35. void didUpdateWidget(CustomCheckBox oldWidget) {
  36. super.didUpdateWidget(oldWidget);
  37. Log.d("oldWidget - selectedOptions :${oldWidget.selectedOptions} newWidget - selectedOptions:${widget.selectedOptions} ");
  38. // 使用ListEquality来比较两个列表的内容
  39. if (!_listEquals(oldWidget.selectedOptions, widget.selectedOptions)) {
  40. _initializeSelectedOptions();
  41. }
  42. }
  43. bool _listEquals(List<String> list1, List<String> list2) {
  44. if (list1.length != list2.length) {
  45. return false;
  46. }
  47. for (int i = 0; i < list1.length; i++) {
  48. if (list1[i] != list2[i]) {
  49. return false;
  50. }
  51. }
  52. return true;
  53. }
  54. @override
  55. Widget build(BuildContext context) {
  56. return Wrap(
  57. spacing: 15.0,
  58. runSpacing: 15.0,
  59. children: widget.options.asMap().entries.map((entry) {
  60. int index = entry.key;
  61. String option = entry.value;
  62. return _buildCheckBoxWithIconAndText(
  63. path: _selectedIndexes.contains(index) ? Assets.baseServiceCheckBoxChecked : Assets.baseServiceCheckBoxUncheck,
  64. text: option,
  65. value: _selectedIndexes.contains(index),
  66. onChanged: (isChecked) {
  67. setState(() {
  68. if (isChecked) {
  69. if (!_selectedOptions.contains(option)) {
  70. _selectedOptions.add(option);
  71. _selectedIndexes.add(index);
  72. }
  73. } else {
  74. _selectedOptions.remove(option);
  75. _selectedIndexes.remove(index);
  76. }
  77. widget.onOptionsSelected(_selectedIndexes);
  78. });
  79. },
  80. );
  81. }).toList(),
  82. );
  83. }
  84. Widget _buildCheckBoxWithIconAndText({
  85. required String path,
  86. required String text,
  87. required bool value,
  88. required Function(bool) onChanged,
  89. }) {
  90. return Row(
  91. mainAxisSize: MainAxisSize.min,
  92. children: <Widget>[
  93. MyAssetImage(path, width: 20.5, height: 20.5),
  94. SizedBox(width: 10),
  95. MyTextView(
  96. text.tr,
  97. textColor: ColorConstants.white,
  98. fontSize: 14,
  99. isFontRegular: true,
  100. ),
  101. ],
  102. ).onTap(() {
  103. onChanged(!value); // 点击时切换选中状态
  104. });
  105. }
  106. }