I'm using showDatePicker to display a calendar. How do I change the button color and also the today's date background color
I've tried different colors setting in Theme Data. but the OK, Cancel & today's date still in blue
Theme(data: ThemeData(
splashColor: Color(0xFFFE9BC0),
)
child: myWidget(),
);
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Kindly fine the highlighted code, this is the simplest way to change the colour of your datePicker.
What i did to change the color of the datePicker was:
Theme(
data: ThemeData(primarySwatch: Colors.red),
child: myWidget(),
);
Setting an appropriate primarySwatch instead of splashColor will change all the appearance of the picker, this is because a swatch is like a palette of color.
Another thing you could do is set a primarySwatch with a MaterialColor similar to the one you want as main color and then customize the splash color or primary color like this:
Theme(
data: ThemeData(primarySwatch: Colors.red, splashColor: Colors.green),
child: myWidget(),
);
Hope it helps!
If you still face null safety problem in changing color in 2021 ..then here is the simeple solution
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;
});}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With