THATMobile/lib/core/components/radio/custom_radio.dart
2026-02-26 10:39:42 +07:00

147 lines
4.1 KiB
Dart

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<T> extends FormBuilderField<T> {
CustomRadio(
{Key? key,
String name = "radio",
InputDecoration decoration = const InputDecoration(),
this.groupValue,
this.padding = const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
this.textStyle,
ValueChanged<T?>? onChanged,
FormFieldValidator<T?>? validator,
AutovalidateMode autovalidateMode = AutovalidateMode.disabled,
this.text = "",
this.value})
: super(
key: key,
name: name,
onChanged: onChanged,
validator: validator,
autovalidateMode: autovalidateMode,
builder: (FormFieldState<T?> field) {
final _CustomRadiotate<T> state = field as _CustomRadiotate<T>;
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<T> createState() => _CustomRadiotate<T>();
}
class _CustomRadiotate<T> extends FormBuilderFieldState<CustomRadio<T>, 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<T> 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: <Widget>[
Container(
margin: const EdgeInsets.only(right: 12),
child:
// Transform.scale(
// scale: 1.2,
// child: Radio<T?>(
// 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);
});
}
}
}