67 lines
1.6 KiB
Dart
67 lines
1.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class CheckboxWithLabel extends StatefulWidget {
|
|
const CheckboxWithLabel({
|
|
super.key,
|
|
required String this.label,
|
|
required this.callback,
|
|
this.state = false,
|
|
});
|
|
|
|
final String label;
|
|
final Function(bool state) callback;
|
|
final bool state;
|
|
|
|
@override
|
|
State<CheckboxWithLabel> createState() => _CheckboxWithLabelState();
|
|
}
|
|
|
|
class _CheckboxWithLabelState extends State<CheckboxWithLabel> {
|
|
bool isChecked = false;
|
|
|
|
@override
|
|
void initState() {
|
|
// TODO: implement initState
|
|
super.initState();
|
|
isChecked = widget.state;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
Color getColor(Set<WidgetState> states) {
|
|
const Set<WidgetState> interactiveStates = <WidgetState>{
|
|
WidgetState.pressed,
|
|
WidgetState.hovered,
|
|
WidgetState.focused,
|
|
};
|
|
if (states.any(interactiveStates.contains)) {
|
|
return Colors.blue;
|
|
}
|
|
return Colors.black54;
|
|
}
|
|
|
|
return Row(
|
|
children: [
|
|
Checkbox(
|
|
checkColor: Colors.white,
|
|
fillColor: WidgetStateProperty.resolveWith(getColor),
|
|
side: WidgetStateBorderSide.resolveWith(
|
|
(states) => BorderSide(width: 1.0, color: Colors.grey),
|
|
),
|
|
value: isChecked,
|
|
onChanged: (bool? value) {
|
|
setState(() {
|
|
isChecked = value!;
|
|
widget.callback(isChecked);
|
|
});
|
|
},
|
|
),
|
|
Text(
|
|
widget.label,
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|