Line data Source code
1 : import 'package:basf_flutter_components/basf_flutter_components.dart';
2 : import 'package:flutter/material.dart';
3 :
4 : /// {@template basf_checkbox}
5 : /// [BasfCheckbox]
6 : ///
7 : /// Example:
8 : /// ```dart
9 : /// BasfCheckbox(
10 : /// text: 'Default value',
11 : /// value: _value,
12 : /// onChanged: (value) {
13 : /// _value = !value;
14 : /// setState(() {});
15 : /// },
16 : /// ),
17 : /// ```
18 : /// {@endtemplate}
19 : class BasfCheckbox extends StatelessWidget {
20 : /// {@macro basf_checkbox}
21 1 : const BasfCheckbox({
22 : super.key,
23 : required this.value,
24 : required this.onChanged,
25 : this.text,
26 : this.reverse = false,
27 : this.alignment = MainAxisAlignment.start,
28 : this.inactiveColor,
29 : this.color,
30 : this.activeSplashColor,
31 : this.inactiveSplashColor,
32 : this.icon,
33 : this.iconColor,
34 : });
35 :
36 : /// Value of the checkbox
37 : final bool value;
38 :
39 : /// What happens when this value changes
40 : final void Function(bool) onChanged;
41 :
42 : /// Text of the checkbox
43 : final String? text;
44 :
45 : /// Position of the text relative to the checkbox
46 : final bool reverse;
47 :
48 : /// Alignment
49 : final MainAxisAlignment alignment;
50 :
51 : /// Main color of the checkbox
52 : final Color? color;
53 :
54 : /// Inactive color of the checkbox
55 : final Color? inactiveColor;
56 :
57 : /// Splash color of the checkbox
58 : final Color? activeSplashColor;
59 :
60 : /// Inactive splash color of the checkbox
61 : final Color? inactiveSplashColor;
62 :
63 : /// IconData for the checkbox
64 : final IconData? icon;
65 :
66 : /// Color of the icon
67 : final Color? iconColor;
68 :
69 1 : @override
70 : Widget build(BuildContext context) {
71 1 : return _checkBoxLayout();
72 : }
73 :
74 1 : Widget _checkBoxLayout() {
75 2 : final leading = reverse ? _text() : _checkbox();
76 2 : final trailing = reverse ? _checkbox() : _text();
77 1 : return Row(
78 1 : mainAxisAlignment: alignment,
79 1 : children: [leading, trailing],
80 : );
81 : }
82 :
83 1 : Widget _checkbox() {
84 1 : return Builder(
85 1 : builder: (context) {
86 1 : return MaterialButton(
87 : minWidth: 0,
88 1 : splashColor: !value
89 4 : ? activeSplashColor ?? Theme.of(context).colorScheme.primary
90 1 : : inactiveSplashColor ?? BasfColors.middleGrey,
91 : shape: const CircleBorder(),
92 4 : onPressed: () => onChanged(value),
93 1 : child: Container(
94 : padding: const EdgeInsets.all(3),
95 1 : decoration: BoxDecoration(
96 1 : color: value
97 4 : ? color ?? Theme.of(context).colorScheme.primary
98 1 : : inactiveColor ?? BasfColors.middleGrey,
99 1 : borderRadius: BorderRadius.circular(20),
100 : ),
101 1 : child: Icon(
102 1 : icon ?? Icons.check,
103 1 : color: iconColor ?? Colors.white,
104 : ),
105 : ),
106 : );
107 : },
108 : );
109 : }
110 :
111 1 : Widget _text() {
112 1 : return Expanded(
113 2 : child: Text(text ?? ''),
114 : );
115 : }
116 : }
|