dialog.dart 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import '../alert_dialog.dart';
  4. import 'group.dart';
  5. class VDialogCheckBoxCellGroup<T, TValue> extends StatefulWidget {
  6. final String? title;
  7. final List<CheckBoxCellGroupItem<T>> source;
  8. /// 选项文本提取函数
  9. final String Function(T data) labelGetter;
  10. /// 选项值提取函数
  11. final TValue Function(T data) valueGetter;
  12. final VCheckBoxCellGroupCheckChanged<T, TValue>? onChanged;
  13. const VDialogCheckBoxCellGroup({
  14. super.key,
  15. this.title,
  16. required this.source,
  17. required this.labelGetter,
  18. required this.valueGetter,
  19. this.onChanged,
  20. });
  21. Future<List<V>> show<V>() async {
  22. final result = await VAlertDialog.showDialog<List<V>>(this);
  23. return result!;
  24. }
  25. @override
  26. State<StatefulWidget> createState() => _DialogGroupState<T, TValue>();
  27. }
  28. class _DialogGroupState<T, TValue>
  29. extends State<VDialogCheckBoxCellGroup<T, TValue>> {
  30. late List<TValue> _values;
  31. @override
  32. void initState() {
  33. _values = widget.source
  34. .where((e) => e.isChecked)
  35. .map((e) => widget.valueGetter.call(e.data))
  36. .toList();
  37. super.initState();
  38. }
  39. @override
  40. Widget build(BuildContext context) {
  41. return VAlertDialog(
  42. title: widget.title,
  43. // contentPadding: EdgeInsets.symmetric(horizontal: 20),
  44. content: Scrollbar(
  45. thumbVisibility: true,
  46. child: VCheckBoxCellGroup<T, TValue>(
  47. labelGetter: widget.labelGetter,
  48. valueGetter: widget.valueGetter,
  49. source: widget.source,
  50. onChanged: (value, isChecked, data, checkedValues) {
  51. _values = checkedValues;
  52. widget.onChanged?.call(value, isChecked, data, checkedValues);
  53. },
  54. isScrollable: true,
  55. ),
  56. ),
  57. onConfirm: () {
  58. Get.back(result: _values);
  59. },
  60. );
  61. }
  62. }