[英]How to make Checkbox shape rounded in CheckboxListTile in Flutter?
I have implemented CheckboxListTile
in a flutter.我在 flutter 中实现了CheckboxListTile
。 The default checkbox is square.默认复选框是方形的。 But I need a circular Checkbox, just like the one in the below image.但我需要一个圆形复选框,就像下图中的那个。 Is there any way to do it?有什么办法吗? Thanks for the help:)谢谢您的帮助:)
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
@override
Widget build(BuildContext context) {
return CheckboxListTile(
title: const Text('Animate Slowly'),
value: timeDilation != 1.0,
onChanged: (bool? value) {
setState(() {
timeDilation = value! ? 10.0 : 1.0;
});
},
secondary: const Icon(Icons.hourglass_empty),
);
}
}
If you want to change the default checkbox theme you need to override it like this如果要更改默认复选框主题,则需要像这样覆盖它
class Exmaple extends StatelessWidget {
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final oldCheckboxTheme = theme.checkboxTheme;
final newCheckBoxTheme = oldCheckboxTheme.copyWith(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(100)),
);
return Theme(
data: theme.copyWith(checkboxTheme: newCheckBoxTheme),
child: CheckboxListTile(
onChanged: (_) {},
value: false,
),
);
}
}
You can also implement it directly in the widget, in case you need to implement a theme first.您也可以直接在小部件中实现它,以防您需要先实现主题。
class Exmaple extends StatelessWidget {
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final oldCheckboxTheme = theme.checkboxTheme;
final newCheckBoxTheme = oldCheckboxTheme.copyWith(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(100)),
);
return Theme(
data: ThemeData(
checkboxTheme: CheckboxThemeData(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25),
),
),
unselectedWidgetColor: const Color(0xFF95A1AC),
),
child: CheckboxListTile(
shape: CircleBorder(),
value: checkboxListTileValue ??= false,
onChanged: (newValue) =>
checkboxListTileValue = newValue!,
title: Text(
"test of text",
style: const TextStyle(
// fontSize: 22.0,
// fontWeight: FontWeight.bold
),
),
),
),
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.