cell.dart 2.5 KB

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