检查您的示例中是否设置了 ThemeData.checkBoxThemeData
重现您的问题的简单代码段。
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
Color getColor(Set<MaterialState> states) {
const Set<MaterialState> interactiveStates = <MaterialState>{
MaterialState.pressed,
MaterialState.hovered,
MaterialState.focused,
};
if (states.any(interactiveStates.contains)) {
return Colors.brown;
}
return Colors.transparent;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
theme: ThemeData(
checkboxTheme: CheckboxThemeData(
fillColor: MaterialStateProperty.resolveWith(getColor)
),
),
home: MyStatefulWidget(),
);
}
}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
bool isSelected = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('CheckBox')
),
body: Center(
child: Checkbox(
value: isSelected,
activeColor: Theme.of(context).primaryColor,
onChanged:(bool? value){
setState((){
isSelected = !isSelected;
});
}
),
),
);
}
}
检查您在 CheckBoxThemeData.fillColor 中是否返回透明
Color getColor(Set<MaterialState> states) {
const Set<MaterialState> interactiveStates = <MaterialState>{
MaterialState.pressed,
MaterialState.hovered,
MaterialState.focused,
};
if (states.any(interactiveStates.contains)) {
return Colors.brown;
}
/// Instead of this you can return the Color
return Colors.transparent;
}