custom_check_box.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. if (oldWidget.selectedOptions != widget.selectedOptions) {
  38. _initializeSelectedOptions();
  39. }
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. return Wrap(
  44. spacing: 15.0,
  45. runSpacing: 15.0,
  46. children: widget.options.asMap().entries.map((entry) {
  47. int index = entry.key;
  48. String option = entry.value;
  49. return _buildCheckBoxWithIconAndText(
  50. path: _selectedIndexes.contains(index) ? Assets.baseServiceCheckBoxChecked : Assets.baseServiceCheckBoxUncheck,
  51. text: option,
  52. value: _selectedIndexes.contains(index),
  53. onChanged: (isChecked) {
  54. setState(() {
  55. if (isChecked) {
  56. if (!_selectedOptions.contains(option)) {
  57. _selectedOptions.add(option);
  58. _selectedIndexes.add(index);
  59. }
  60. } else {
  61. _selectedOptions.remove(option);
  62. _selectedIndexes.remove(index);
  63. }
  64. widget.onOptionsSelected(_selectedIndexes);
  65. });
  66. },
  67. );
  68. }).toList(),
  69. );
  70. }
  71. Widget _buildCheckBoxWithIconAndText({
  72. required String path,
  73. required String text,
  74. required bool value,
  75. required Function(bool) onChanged,
  76. }) {
  77. return Row(
  78. mainAxisSize: MainAxisSize.min,
  79. children: <Widget>[
  80. MyAssetImage(path, width: 20.5, height: 20.5),
  81. SizedBox(width: 10),
  82. MyTextView(
  83. text.tr,
  84. textColor: ColorConstants.white,
  85. fontSize: 14,
  86. isFontRegular: true,
  87. ),
  88. ],
  89. ).onTap(() {
  90. onChanged(!value); // 点击时切换选中状态
  91. });
  92. }
  93. }