未选中 Material 复选框不可见
Unchecked Material Checkboxes not visible
目前,我必须处理一个使用 flutter/material.dart
包来显示复选框的 Flutter 项目,如下所示:
// ...
Checkbox(
value: item.areaShowOnMap,
activeColor: theme.colorPrimary,
onChanged: (bool value){ //
item.areaShowOnMap = value;
if (value)
_setCircle(item);
else
_deleteCircle(item);
}
),
// ...
然而结果是这样的:
整个应用程序中的每个复选框都会发生这种情况。我尝试将 checkColor
设置为 Colors.black
,但在未选中时它仍然不可见,在 Android 和 iOS 上都是如此。
我的问题是:问题的原因可能是什么?我猜非活动颜色是透明的,但我不知道如何控制它。
检查您的示例中是否设置了 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;
}
目前,我必须处理一个使用 flutter/material.dart
包来显示复选框的 Flutter 项目,如下所示:
// ...
Checkbox(
value: item.areaShowOnMap,
activeColor: theme.colorPrimary,
onChanged: (bool value){ //
item.areaShowOnMap = value;
if (value)
_setCircle(item);
else
_deleteCircle(item);
}
),
// ...
然而结果是这样的:
整个应用程序中的每个复选框都会发生这种情况。我尝试将 checkColor
设置为 Colors.black
,但在未选中时它仍然不可见,在 Android 和 iOS 上都是如此。
我的问题是:问题的原因可能是什么?我猜非活动颜色是透明的,但我不知道如何控制它。
检查您的示例中是否设置了 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;
}