我正在使用showDatePicker()方法在flutter应用程序中显示日期选择器。如何自定义日期选择器的颜色?
这是我主题的代码:`
Widget dateOfBirth(String hintText) {
return Theme(
data: Theme.of(context).copyWith(
buttonTheme: ButtonThemeData(
textTheme: ButtonTextTheme
.accent //Colour of the text in the button "OK/CANCEL"
),
),
child: Builder(
builder: (context) {
return GestureDetector(
onTap: () async {
DateTime initialDate = DateTime(DateTime.now().year - 17,
DateTime.now().month, DateTime.now().day);
final picked = await showDatePicker(
context: context,
initialDate: initialDate,
firstDate: DateTime(DateTime.now().year - 100,
DateTime.now().month, DateTime.now().day),
lastDate: DateTime(DateTime.now().year - 17, DateTime.now().month,
DateTime.now().day),
);
if (picked != null && picked != dobSelected) {
setState(() {
});
}
return picked;
},
child: Padding(
//You can use any other widget here
padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: Container(
height: 55,
width: MediaQuery.of(context).size.width,
alignment: Alignment.centerLeft,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(3)),
color: Color(0xFFF2F2F2),
),
padding: const EdgeInsets.symmetric(horizontal: 13),
child: dobSelected == null
? Text(
'Date Of Birth',
style: TextStyle(
color: widget.isLender
? Color(0xFF8B8B8B)
: Color(0xFFB3B1B1),
fontSize: 15),
)
: Text(DateFormat('yyyy-MM-dd').format(dobSelected))),
),
);
},
),
);}
我假设您要自定义日期选择器而不是主要主题。通常,日期选择器遵循您的主题。 这是我将页面包装为主题的代码:
@override
Widget build(BuildContext context) {
[...]
return new CustomTheme(
new Scaffold(
[...]
)
);}
我想更改日期选择器的选定值颜色
答案 0 :(得分:0)
您没有在尝试进行更改的地方向我们显示了代码。您应该只需用isLender
来更改setState
变量即可完成操作(在将其传递给它的父类中)。
setState((){
isLender = true; // or false
});
答案 1 :(得分:0)
如果您在 2021 年更改颜色时仍然面临空安全问题..那么这里是简单的解决方案
Future<void> _selectDate(BuildContext context) async {
DateTime? picked = await showDatePicker(
context: context,
builder: (BuildContext context, Widget ?child) {
return Theme(
data: ThemeData(
primarySwatch: Colors.grey,
splashColor: Colors.black,
textTheme: TextTheme(
subtitle1: TextStyle(color: Colors.black),
button: TextStyle(color: Colors.black),
),
accentColor: Colors.black,
colorScheme: ColorScheme.light(
primary: Color(0xffffbc00),
primaryVariant: Colors.black,
secondaryVariant: Colors.black,
onSecondary: Colors.black,
onPrimary: Colors.white,
surface: Colors.black,
onSurface: Colors.black,
secondary: Colors.black),
dialogBackgroundColor: Colors.white,
),
child: child ??Text(""),
);
}
initialDate: selectedDate,
firstDate: DateTime(1960, 8),
lastDate: DateTime.now());
if (picked != null && picked != selectedDate)
setState(() {
selectedDate = picked;
String da = picked.day.toString() +
"-" +
picked.month.toString() +
"-" +
picked.year.toString();
dateOfBirth.text = da;
});}