import 'package:cs_resources/constants/color_constants.dart'; import 'package:cs_resources/generated/assets.dart'; import 'package:flutter/material.dart'; import 'package:get/get_utils/get_utils.dart'; import 'package:get/get_utils/src/extensions/widget_extensions.dart'; import 'package:widgets/ext/ex_widget.dart'; import 'package:widgets/my_load_image.dart'; import 'package:widgets/my_text_view.dart'; class CustomRadioCheck extends StatefulWidget { final List options; int? selectedPosition; final Function(int index, String text) onOptionSelected; CustomRadioCheck({ required this.options, required this.onOptionSelected, this.selectedPosition = 0, }); @override _CustomRadioCheckState createState() => _CustomRadioCheckState(); } class _CustomRadioCheckState extends State { String? _selectedOption; @override void initState() { super.initState(); // 初始化 _selectedOption if (widget.selectedPosition != null && widget.selectedPosition! >= 0 && widget.selectedPosition! < widget.options.length) { _selectedOption = widget.options[widget.selectedPosition!]; } else { // 默认选择第一个选项,如果 selectedPosition 不在有效范围内 _selectedOption = widget.options.isNotEmpty ? widget.options[0] : null; } } @override Widget build(BuildContext context) { return Wrap( spacing: 8.0, // 水平间距 runSpacing: 8.0, // 垂直间距 children: widget.options.map((option) { return _buildRadioWithIconAndText( path: option == _selectedOption ? Assets.cptAuthLoginRadioChecked : Assets.cptAuthLoginRadioUncheck, text: option, value: option == _selectedOption, onChanged: (value) { setState(() { _selectedOption = option; int selectedIndex = widget.options.indexOf(option); widget.onOptionSelected(selectedIndex, option); }); }, ); }).toList(), ); } Widget _buildRadioWithIconAndText({ required String path, required String text, required bool value, required Function(bool) onChanged, }) { return Row( mainAxisSize: MainAxisSize.min, children: [ MyAssetImage(path, width: 22, height: 22), SizedBox(width: 10), MyTextView( text.tr, textColor: ColorConstants.white, fontSize: 14, isFontRegular: true, ), ], ).marginOnly(right: 20, bottom: 5).onTap(() { onChanged(true); }); } }