label_checkbox.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import 'package:flutter/material.dart';
  2. class LabeledCheckbox extends StatelessWidget {
  3. const LabeledCheckbox({
  4. Key? key,
  5. required this.label,
  6. required this.padding,
  7. required this.value,
  8. required this.onChanged,
  9. this.mainAxisAlignment = MainAxisAlignment.start,
  10. this.pageName = 'LabeledCheckbox',
  11. }) : super(key: key);
  12. @override
  13. final String pageName;
  14. /// 选择框文案
  15. final Widget label;
  16. final MainAxisAlignment mainAxisAlignment;
  17. /// 间距
  18. final EdgeInsets padding;
  19. /// 选择值
  20. final bool value;
  21. /// 选择回调
  22. final Function onChanged;
  23. @override
  24. Widget build(BuildContext context) {
  25. final labeledCheckboxbody = Padding(
  26. padding: padding,
  27. child: Flex(
  28. direction: Axis.horizontal,
  29. mainAxisAlignment: mainAxisAlignment,
  30. children: [
  31. SizedBox(
  32. width: 28,
  33. child: Checkbox(
  34. value: value,
  35. checkColor: Colors.white,
  36. shape: RoundedRectangleBorder(
  37. side: BorderSide.none,
  38. borderRadius: BorderRadius.circular(4),
  39. ),
  40. activeColor: Theme.of(context).primaryColor,
  41. onChanged: (bool? newValue) {
  42. onChanged(newValue);
  43. },
  44. ),
  45. ),
  46. Flexible(
  47. flex: 0,
  48. child: label,
  49. ),
  50. ],
  51. ),
  52. );
  53. return GestureDetector(
  54. child: labeledCheckboxbody,
  55. onTap: () => onChanged(!value),
  56. );
  57. }
  58. }