custom_radio_check.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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:get/get_utils/src/extensions/widget_extensions.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. /*
  10. * 条件单选 Radio
  11. */
  12. class CustomRadioCheck extends StatefulWidget {
  13. final List<String> options;
  14. int? selectedPosition;
  15. final Function(int index, String text) onOptionSelected;
  16. final bool enable;
  17. final Color textColor;
  18. CustomRadioCheck({
  19. required this.options,
  20. required this.onOptionSelected,
  21. this.selectedPosition = 0,
  22. this.enable = true, // 默认可用
  23. this.textColor = Colors.white, // 默认可用
  24. });
  25. @override
  26. _CustomRadioCheckState createState() => _CustomRadioCheckState();
  27. }
  28. class _CustomRadioCheckState extends State<CustomRadioCheck> {
  29. String? _selectedOption;
  30. @override
  31. void initState() {
  32. super.initState();
  33. _initializeSelectedOption();
  34. }
  35. void _initializeSelectedOption() {
  36. if (widget.selectedPosition != null && widget.selectedPosition! >= 0 && widget.selectedPosition! < widget.options.length) {
  37. _selectedOption = widget.options[widget.selectedPosition!];
  38. } else {
  39. _selectedOption = widget.options.isNotEmpty ? widget.options[0] : null;
  40. }
  41. }
  42. @override
  43. void didUpdateWidget(CustomRadioCheck oldWidget) {
  44. super.didUpdateWidget(oldWidget);
  45. // 如果 selectedPosition 发生变化,重新初始化选中项
  46. if (oldWidget.selectedPosition != widget.selectedPosition) {
  47. _initializeSelectedOption();
  48. }
  49. }
  50. @override
  51. Widget build(BuildContext context) {
  52. return Wrap(
  53. spacing: 8.0,
  54. runSpacing: 8.0,
  55. children: widget.options.map((option) {
  56. return _buildRadioWithIconAndText(
  57. path: option == _selectedOption ? Assets.cptAuthLoginRadioChecked : Assets.cptAuthLoginRadioUncheck,
  58. text: option,
  59. value: option == _selectedOption,
  60. onChanged: widget.enable
  61. ? (value) {
  62. // 只在可用状态下响应点击
  63. setState(() {
  64. _selectedOption = option;
  65. int selectedIndex = widget.options.indexOf(option);
  66. widget.onOptionSelected(selectedIndex, option);
  67. });
  68. }
  69. : null, // 如果不可用则不设置回调
  70. );
  71. }).toList(),
  72. );
  73. }
  74. Widget _buildRadioWithIconAndText({
  75. required String path,
  76. required String text,
  77. required bool value,
  78. required Function(bool)? onChanged, // 允许 onChanged 为 null
  79. }) {
  80. return Row(
  81. mainAxisSize: MainAxisSize.min,
  82. children: <Widget>[
  83. MyAssetImage(path, width: 22, height: 22),
  84. SizedBox(width: 10),
  85. MyTextView(
  86. text.tr,
  87. textColor: widget.enable ? widget.textColor : ColorConstants.gray, // 根据 enable 改变文本颜色
  88. fontSize: 14,
  89. isFontRegular: true,
  90. ),
  91. ],
  92. ).marginOnly(right: 20, bottom: 5).onTap(() {
  93. if (onChanged != null) {
  94. // 只有在可用状态下才触发
  95. onChanged(true);
  96. }
  97. });
  98. }
  99. }