import 'package:baseproject/core/components/form/form_builder_field.dart'; import 'package:baseproject/core/extension/string_extension.dart'; import 'package:baseproject/core/theme/custom_color.dart'; import 'package:flutter/material.dart'; // ignore: must_be_immutable class CustomRadio extends FormBuilderField { CustomRadio( {Key? key, String name = "radio", InputDecoration decoration = const InputDecoration(), this.groupValue, this.padding = const EdgeInsets.symmetric(horizontal: 12, vertical: 8), this.textStyle, ValueChanged? onChanged, FormFieldValidator? validator, AutovalidateMode autovalidateMode = AutovalidateMode.disabled, this.text = "", this.value}) : super( key: key, name: name, onChanged: onChanged, validator: validator, autovalidateMode: autovalidateMode, builder: (FormFieldState field) { final _CustomRadiotate state = field as _CustomRadiotate; return state.buildBody(state.context); return InputDecorator( decoration: decoration, child: state.buildBody(state.context), ); }, ); final EdgeInsets padding; final TextStyle? textStyle; final String text; final T? value; T? groupValue; @override _CustomRadiotate createState() => _CustomRadiotate(); } class _CustomRadiotate extends FormBuilderFieldState, T> { bool isShowSelect = false; EdgeInsets get padding => widget.padding; bool isFirstLoad = true; double get radioSize => 18 * 1; T? get radioValue => widget.value; T? groupValue; @override void initState() { super.initState(); groupValue = widget.groupValue; } @override void didUpdateWidget(covariant CustomRadio oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.groupValue != widget.groupValue) { setState(() { groupValue = widget.groupValue; }); } } Widget buildBody(BuildContext context) { if (widget.text.isNullOrEmpty) return _buildRadio(); return Row( children: [ Container( margin: const EdgeInsets.only(right: 12), child: // Transform.scale( // scale: 1.2, // child: Radio( // activeColor: widget.activeColor, // value: radioValue, // groupValue: groupValue, // onChanged: (T? value) { // groupValue = value; // onChanged(radioValue); // }, // ), // ), _buildRadio(), ), Expanded( child: GestureDetector( onTap: () { onChanged(radioValue); }, child: Text(widget.text, style: widget.textStyle), ), ) ], ); } Widget _buildRadio() { bool selected = groupValue == radioValue; return SizedBox( width: radioSize, height: radioSize, child: InkWell( onTap: () => onChanged(radioValue), child: Container( width: radioSize, height: radioSize, decoration: BoxDecoration( shape: BoxShape.circle, color: selected ? CustomColor.primaryColor : Colors.transparent, border: Border.all( color: selected ? CustomColor.primaryColor : CustomColor.textGray, width: 1.0, ), ), child: selected ? Center( child: Container( width: radioSize * 0.4, height: radioSize * 0.4, decoration: const BoxDecoration( shape: BoxShape.circle, color: Colors.white, ), ), ) : null, ), ), ); } void onChanged(T? data, {bool isFirstLoad = false}) { if (!isFirstLoad) { setState(() { didChange(data); }); } } }