我正在尝试更改日期选择器的主题,但我无法更改当前日期的主题 这是日期选择器代码:
final DateTime? picked = await showDatePicker(
context: context,
initialDate: null,
firstDate: DateTime(1900, 8),
lastDate: DateTime.now(),
builder: (context, child) {
return Theme(
data: Theme.of(context).copyWith(
colorScheme: ColorScheme.light(
primary: Colors.white, // <-- SEE HERE
onPrimary: primary, // <-- SEE HERE
onSurface: Colors.black, // <-// - SEE HERE
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(foregroundColor: primary),
),
),
child: child!,
);
},
);
您更改了错误的参数。日期选择器采用
datePickerTheme
。以下是使用示例:
return Theme(
data: Theme.of(context).copyWith(
datePickerTheme: DatePickerThemeData(
backgroundColor: const Color(0xFFFFFFFF),
shadowColor: const Color(0xFF795548),
elevation: 10.0,
dividerColor: const Color(0xFFFFC108),
surfaceTintColor: const Color(0xFFE81E63),
headerBackgroundColor: const Color(0xFF69F0AE),
headerForegroundColor: const Color(0xFF9E9E9E),
rangePickerBackgroundColor: const Color(0xFFB2FF59),
rangePickerElevation: 10.0,
rangePickerHeaderBackgroundColor: const Color(0xFFFFFFFF),
rangePickerHeaderForegroundColor: const Color(0xFFFFFFFF),
todayBackgroundColor: MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.selected)) {
return const Color(0xFFFFFFFF);
}
return const Color(0xFF000000);
}),
dayBackgroundColor: MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.selected)) {
return const Color(0xFF000000);
}
return const Color(0xFFFFFFFF);
}),
dayForegroundColor: MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.selected)) {
return const Color(0xFFFFFFFF);
}
return const Color(0xFFFFFFFF);
}),
confirmButtonStyle: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.selected)) {
return const Color(0xFFFFFFFF);
}
return const Color(0xFFFFFFFF);
}),
),
),
)),