cell.dart 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import 'package:flutter/material.dart';
  2. typedef VCheckBoxCellChangeCallback<T> = void Function(
  3. T? value, bool isChecked);
  4. class VCheckBoxCell<T> extends StatefulWidget {
  5. final String label;
  6. final T? value;
  7. final bool isChecked;
  8. final bool isDisabled;
  9. final VCheckBoxCellChangeCallback<T>? onChanged;
  10. const VCheckBoxCell({
  11. super.key,
  12. required this.label,
  13. required this.value,
  14. required this.isChecked,
  15. required this.isDisabled,
  16. this.onChanged,
  17. });
  18. @override
  19. State<StatefulWidget> createState() => _VCheckBoxCellState<T>();
  20. }
  21. class _VCheckBoxCellState<T> extends State<VCheckBoxCell<T>> {
  22. static const _horizontalPadding = 24.0;
  23. late bool _isChecked;
  24. static const _height = 56.0;
  25. @override
  26. void initState() {
  27. _isChecked = widget.isChecked;
  28. super.initState();
  29. }
  30. @override
  31. Widget build(BuildContext context) {
  32. final iconWidget = _CheckStatusIcon(
  33. isChecked: _isChecked,
  34. isDisabled: widget.isDisabled,
  35. );
  36. final labelWidget = Text(
  37. widget.label,
  38. style: TextStyle(
  39. color: _isChecked ? Theme.of(context).primaryColor : Colors.black87,
  40. fontSize: 20,
  41. ),
  42. );
  43. return Material(
  44. child: Ink(
  45. child: InkWell(
  46. onTap: widget.isDisabled ? null : _onClick,
  47. child: Container(
  48. height: _height,
  49. padding: const EdgeInsets.symmetric(horizontal: _horizontalPadding),
  50. color: _isChecked ? Theme.of(context).secondaryHeaderColor : null,
  51. child: Row(
  52. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  53. children: [
  54. labelWidget,
  55. iconWidget,
  56. ],
  57. ),
  58. ),
  59. ),
  60. ),
  61. );
  62. }
  63. void _onClick() {
  64. setState(() {
  65. _isChecked = !_isChecked;
  66. widget.onChanged?.call(widget.value, _isChecked);
  67. });
  68. }
  69. }
  70. class _CheckStatusIcon extends StatelessWidget {
  71. final bool isChecked;
  72. final bool isDisabled;
  73. const _CheckStatusIcon({
  74. super.key,
  75. required this.isChecked,
  76. required this.isDisabled,
  77. });
  78. @override
  79. Widget build(BuildContext context) {
  80. if (isChecked) {
  81. return Icon(
  82. Icons.check_outlined,
  83. color: Theme.of(context).primaryColor,
  84. size: 24,
  85. );
  86. } else {
  87. return const Icon(
  88. Icons.check_box_outline_blank_outlined,
  89. color: Colors.grey,
  90. size: 24,
  91. );
  92. }
  93. }
  94. }