123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'cell.dart';
- typedef VRadioCellGroupItemBuilder<T> = Widget Function(T data);
- typedef VRadioCellGroupCheckChanged<T, TValue> = void Function(
- TValue value,
- bool isChecked,
- T data,
- TValue? checkedValue,
- );
- class RadioCellGroupItem<T> {
- final T data;
- final bool isChecked;
- final bool isDisabled;
- RadioCellGroupItem(
- this.data, {
- this.isChecked = false,
- this.isDisabled = false,
- });
- }
- class VRadioCellGroup<T, TValue> extends StatefulWidget {
- final List<RadioCellGroupItem<T>> source;
- /// 选项文本提取函数
- final String Function(T data) labelGetter;
- /// 选项值提取函数
- final TValue Function(T data) valueGetter;
- final VRadioCellGroupCheckChanged<T, TValue>? onChanged;
- final bool isScrollable;
- final ScrollController? controller;
- const VRadioCellGroup({
- super.key,
- required this.source,
- required this.labelGetter,
- required this.valueGetter,
- this.onChanged,
- this.isScrollable = true,
- this.controller,
- });
- @override
- State<StatefulWidget> createState() => _VRadioCellGroupState<T, TValue>();
- }
- class _VRadioCellGroupState<T, TValue>
- extends State<VRadioCellGroup<T, TValue>> {
- TValue? _checkedValue;
- @override
- void initState() {
- T? data = widget.source.firstWhereOrNull((e) => e.isChecked)?.data;
- if (data != null) {
- _checkedValue = widget.valueGetter.call(data);
- }
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- final divider = Divider(
- thickness: 1,
- color: Colors.grey.shade300,
- height: 2,
- );
- final children = <Widget>[];
- final count = widget.source.length;
- for (var i = 0; i < count; i++) {
- if (i > 0) {
- children.add(divider);
- }
- final item = widget.source[i];
- children.add(_buildItemWidget(item));
- }
- if (widget.isScrollable) {
- return ListView(
- shrinkWrap: true,
- controller: widget.controller ?? ScrollController(),
- children: children,
- );
- } else {
- return Column(
- mainAxisAlignment: MainAxisAlignment.center,
- mainAxisSize: MainAxisSize.min,
- children: children,
- );
- }
- }
- Widget _buildItemWidget(RadioCellGroupItem<T> item) {
- final label = widget.labelGetter.call(item.data);
- final value = widget.valueGetter.call(item.data);
- final isChecked = _checkedValue == value;
- return VRadioCell<TValue>(
- label: label,
- value: value,
- isChecked: isChecked,
- isDisabled: item.isDisabled,
- onChanged: (_, isChecked) {
- _onCheckChanged(value, item.data, isChecked);
- },
- );
- }
- void _onCheckChanged(TValue value, T data, bool isChecked) {
- setState(() {
- if (isChecked) {
- _checkedValue = value;
- } else {
- _checkedValue = null;
- }
- widget.onChanged?.call(value, isChecked, data, _checkedValue);
- });
- }
- }
|