Skip to content

Instantly share code, notes, and snippets.

@nishsvn-dev
Last active May 1, 2020 12:40
Show Gist options
  • Save nishsvn-dev/6d138660f32d1836fbbb5a168d1ca837 to your computer and use it in GitHub Desktop.
Save nishsvn-dev/6d138660f32d1836fbbb5a168d1ca837 to your computer and use it in GitHub Desktop.
CustomLabeledCheckbox optimised for parent-child checkboxes
import 'package:flutter/material.dart';
enum CheckboxType {
Parent,
Child,
}
@immutable
class CustomLabeledCheckbox extends StatelessWidget {
const CustomLabeledCheckbox({
@required this.label,
@required this.value,
@required this.onChanged,
this.checkboxType: CheckboxType.Child,
this.activeColor,
}) : assert(label != null),
assert(checkboxType != null),
assert(
(checkboxType == CheckboxType.Child && value != null) ||
checkboxType == CheckboxType.Parent,
),
tristate = checkboxType == CheckboxType.Parent ? true : false;
final String label;
final bool value;
final bool tristate;
final ValueChanged<bool> onChanged;
final CheckboxType checkboxType;
final Color activeColor;
void _onChanged() {
if (value != null) {
onChanged(!value);
} else {
onChanged(value);
}
}
@override
Widget build(BuildContext context) {
final themeData = Theme.of(context);
return InkWell(
onTap: _onChanged,
child: Padding(
padding: EdgeInsets.only(left: 12, right: 16),
child: Row(
children: <Widget>[
checkboxType == CheckboxType.Parent
? SizedBox(width: 0)
: SizedBox(width: 32),
Checkbox(
tristate: tristate,
value: value,
onChanged: (_) {
_onChanged();
},
activeColor: activeColor ?? themeData.toggleableActiveColor,
),
SizedBox(width: 8),
Text(
label,
style: themeData.textTheme.subhead,
)
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment