123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- import 'package:flutter/material.dart';
- import 'cell.dart';
- typedef VCheckBoxCellGroupItemBuilder<T> = Widget Function(T data);
- typedef VCheckBoxCellGroupCheckChanged<T, TValue> = void Function(
- TValue value,
- bool isChecked,
- T data,
- List<TValue> checkedValues,
- );
- class CheckBoxCellGroupItem<T> {
- final T data;
- final bool isChecked;
- final bool isDisabled;
- CheckBoxCellGroupItem(
- this.data, {
- this.isChecked = false,
- this.isDisabled = false,
- });
- }
- class VCheckBoxCellGroup<T, TValue> extends StatefulWidget {
- final List<CheckBoxCellGroupItem<T>> source;
- /// 选项文本提取函数
- final String Function(T data) labelGetter;
- /// 选项值提取函数
- final TValue Function(T data) valueGetter;
- final VCheckBoxCellGroupCheckChanged<T, TValue>? onChanged;
- final bool isScrollable;
- final ScrollController? controller;
- const VCheckBoxCellGroup({
- super.key,
- required this.source,
- required this.labelGetter,
- required this.valueGetter,
- this.onChanged,
- this.isScrollable = true,
- this.controller,
- });
- @override
- State<StatefulWidget> createState() => _VCheckBoxCellGroupState<T, TValue>();
- }
- class _VCheckBoxCellGroupState<T, TValue>
- extends State<VCheckBoxCellGroup<T, TValue>> {
- List<TValue> _checkedValues = [];
- @override
- void initState() {
- _checkedValues = widget.source
- .where((e) => e.isChecked)
- .map((e) => widget.valueGetter.call(e.data))
- .toList();
- 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(CheckBoxCellGroupItem<T> item) {
- final label = widget.labelGetter.call(item.data);
- final value = widget.valueGetter.call(item.data);
- final isChecked = _checkedValues.contains(value);
- return VCheckBoxCell<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) {
- if (_checkedValues.contains(value) == false) {
- _checkedValues.add(value);
- }
- } else {
- _checkedValues.remove(value);
- }
- widget.onChanged?.call(value, isChecked, data, _checkedValues);
- });
- }
- }
|