I have been trying to make the theme of my date picker dynamic i.e. light theme and dark theme based on the system's theme. Although I have been able to successfully create a single theme, I want a dynamic theme behavior from the date picker and am unsure about how to go about it.
final datePickerDarkTheme = ThemeData.light().copyWith(
colorScheme: const ColorScheme.dark(
onPrimary: Colors.black, // selected text color
onSurface: whiteColor, // default text color
primary: orange, // circle color
),
dialogBackgroundColor: grey,
);
showDatePicker(
context: context,
builder: (context, child) {
return Theme(
data: datePickerDarkTheme,
child: child!,
);
},
initialDate: DateTime.now(),
firstDate: DateTime(1800),
lastDate: DateTime(2200),
);
Any help will be appreciated. Thanks!
Related
I would like the customize the DateRangePicker in flutter to look like the DatePicker.
I am using material 3:
screenshot of DatePicker:
screenshot of DateRangePicker:
Basically in the DateRangePicker you can barely see your selection. I want the background to be as dark as in the DatePicker.
A look at the source code of DateRangePicker reveals that it uses a app bar. I suspect this is the cause of the light color in the header background. But I don't know change that.
This is my code to show the date range picker:
await showDateRangePicker(
context: context,
firstDate: DateTime(1900),
lastDate: DateTime.now(),
);
I manage to achieve what I want thanks to the help of #Rohan Jariwala.
This is what works for me:
await showDateRangePicker(
context: context,
firstDate: DateTime(1900),
lastDate: DateTime.now(),
builder: (context, child) {
return Theme(
data: Theme.of(context).copyWith(
appBarTheme: Theme.of(context)
.appBarTheme
.copyWith(
backgroundColor:
Theme.of(context)
.colorScheme
.primary,
),
),
child: child!,
);
});
You can add builder in showDateRangePicker in following way
builder: (context, Widget? child) => Theme(
data: themeData.copyWith(
appBarTheme: themeData.appBarTheme.copyWith(
backgroundColor: Colors.blue,
iconTheme: themeData.appBarTheme.iconTheme!.copyWith(color: Colors.white)),
colorScheme: ColorScheme.light(
onPrimary: Colors.white,
primary: Colors.red
)),
child: child!,
));
I've added colours to the appBar and colorScheme. You can change those colours.
builder: (context, child) {
return Theme(
data: Theme.of(context).copyWith(
colorScheme: const ColorScheme.light(
primary: Colors.white,
onPrimary: Colors.black,
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
foregroundColor: Colors.black,
),
),
),
child: child!,
);
},
This is my code. And I want to make the header white. But the button is not visible when the header is white. What should I do?
The today color is colorScheme.primary which is white, and we can't see.
final Color todayColor = colorScheme.primary;
.....
} else if (isToday) {
// The current day gets a different text color and a circle stroke
// border.
dayColor = todayColor;
decoration = BoxDecoration(
border: Border.all(color: todayColor),
shape: BoxShape.circle,
);
}
You can check the calendar_date_picker.dart
For now, I can think of creating a local file and customize the color, or changing picker color.
I'm trying to achieve flutter DatePicker rounded corner without installing any external packages ,
I tried to make a new custom datepicker and change some of borders in it but don't work . Any suggestion will be great
Don't know if you still need an answer but you can use the dialogTheme of showDatePicker:
await showDatePicker(
context: context,
initialDate: currentDate,
initialDatePickerMode: DatePickerMode.day,
firstDate:
DateTime(currentDate.day, currentDate.month, currentDate.year - 100),
lastDate: DateTime.now(),
builder: (BuildContext context, child) {
return Theme(
data: Theme.of(context).copyWith(
dialogTheme: DialogTheme(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0), // this is the border radius of the picker
),
),
),
child: child!,
);
},
);
I want to customize DateRangePicker in flutter, How can I change the following elements?
Change the Save button to image.
Remove the Switch to input button.
Change the header background color.
Change day name color.
Change background color.
Change selected item indicator color.
Change selected item text color.
Change selected range indicator color.
Change selected range text color.
showDateRangePicker(
context: context,
firstDate: DateTime.now(),
lastDate: DateTime.now().add(Duration(days: 100)),
builder: (BuildContext context, Widget child) {
return Theme(
data: ThemeData(
...
),
child: child,
);
},
);
Most of these things can only be changed by modifying the source, as others have said before.
You can change the header background color by applying an appBarTheme in the builder callback of showDateRangePicker.
The text colors and the selection color can also be set via applying a theme, you need to use a ColorScheme to set them.
This example sets the header background to blue, the close icon to white, the header texts + the selected date texts to white, and the selection color to red:
final themeData = Theme.of(context);
showDateRangePicker(
context: context,
initialDateRange: initialDateRange,
firstDate: firstDate,
lastDate: lastDate,
currentDate: currentDate,
initialEntryMode: initialEntryMode,
helpText: helpText,
cancelText: cancelText,
confirmText: confirmText,
saveText: saveText,
errorFormatText: errorFormatText,
errorInvalidText: errorInvalidText,
errorInvalidRangeText: errorInvalidRangeText,
fieldStartHintText: fieldStartHintText,
fieldEndHintText: fieldEndHintText,
fieldStartLabelText: fieldStartLabelText,
fieldEndLabelText: fieldEndLabelText,
locale: locale,
useRootNavigator: useRootNavigator,
routeSettings: routeSettings,
textDirection: textDirection,
builder: (context, Widget? child) => Theme(
data: themeData.copyWith(
appBarTheme: themeData.appBarTheme.copyWith(
backgroundColor: Colors.blue,
iconTheme: themeData.appBarTheme.iconTheme!.copyWith(color: Colors.white)),
colorScheme: ColorScheme.light(
onPrimary: Colors.white,
primary: Colors.red
)),
child: child!,
));
Screenshot
#Michael Feinstein is right - to elaborate a little bit on what you have to do:
You need to copy date_range_picker_dialog.dart into your lib folder (you get there by clicking 'go to implementation' on showDateRangePicker()
You need to copy calendar_date_range_picker.dart (~ line 577 of date_range_picker_dialog.dart is where the actual picker widget is returned as body of the dialog)
You need to make the adjustments you want to do in both files. Your numbers 1-3 are in the dialog, have a look at the class _CalendarRangePickerDialog and change what you need. For your 6-9 look at _buildDayItem in the range picker file and the other 2 are also easy to find :-)
Don't forget to change the import in your copy of date_range_picker_dialog.dart so that you import your copy of the date_range_picker.dart, instead of the original one.
Now you are all set and good to go.
I copy below how to change almost everything you asked in terms of color customization:
showDateRangePicker(
context: context,
firstDate: DateTime.now(),
lastDate: DateTime.now().add(Duration(days: 100)),
builder: (BuildContext context, Widget child) {
return Theme(
data: ThemeData.light().copyWith(
//Header background color
primaryColor: Colors.blue,
//Background color
scaffoldBackgroundColor: Colors.grey[50],
//Divider color
dividerColor: Colors.grey,
//Non selected days of the month color
textTheme: TextTheme(
bodyText2:
TextStyle(color: Colors.black),
),
colorScheme: ColorScheme.fromSwatch().copyWith(
//Selected dates background color
primary: Colors.blue,
//Month title and week days color
onSurface: Colors.black,
//Header elements and selected dates text color
//onPrimary: Colors.white,
),
),
child: child,
);
}
);
For that level of customization you will need to get the source code, copy it, and then make your own widget modifying that source code.
You have could do this in two ways:
Fork a library which has the code to create something similar to this and then edit the code directly and customize how you want
Look for a library which allows more customizing to the date picker, below are a few:
Date picker 1
Date picker 2
Many more are available here
I created a package calendar_date_picker2 supporting high-level customisations, just simply wrap it inside a Container and set the container color as the background colors.
When I have 2 radio buttons, where one is selected and the other isn't selected, one of them would be the active color, while the other is grey, so is there any way to change the unselected color from grey to white?
Set unselectedWidgetColor prop of theme in MaterialApp like below:
return new MaterialApp(
title: 'xyz',
home: xyz(),
theme: ThemeData(
brightness: Brightness.dark,
unselectedWidgetColor:Colors.white
),
);
A useful tip that read the source code of radio.dart and you will get everything!
Sure, you have to change the ThemeData of the container of your Radio buttons.
Assuming that you are using a Row Container:
Theme(
data: ThemeData.dark(), //set the dark theme or write your own theme
child: Row(
children: <Widget>[
Radio(
//your attributes here...
),
Radio(
//your attributes here...
)
],
),
)
How it works?
Because if you check the code of the ThemeData, you'll see this validation
unselectedWidgetColor ??= isDark ? Colors.white70 : Colors.black54;
then dark theme is using white70
In case you want only to change Border Color for Widget when it's unselected
Just Customize Fill Color property
Radio(
//here -----
fillColor:
MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
return (isSelected)
? CustomColors.green
: CustomColors.greyMedium;
}),
// ----
value: 'en',
groupValue: langValue,
activeColor: Theme.of(context).primaryColor,
onChanged: (value) {},
),
Wrap your Radio Widgets with Theme widget and add theme data with unselectedWidgetColor as color you want
Ex:
Theme(
data: ThemeData(
//here change to your color
unselectedWidgetColor: Colors.White,
),
child: Row(
children: <Widget>[
Radio(),
Radio()
],
),
);
Color getColor(Set<MaterialState> states) {
return Colors.white;
}
Radio(value: 1,groupValue: numTypeRadio,onChanged: onChangeRadio,fillColor: MaterialStateProperty.resolveWith(getColor),)
For global setting: Setting for an unselected color can be set in the theme with unselectedWidgetColor. However, using fillColor in RadioThemeData affects unselectedWidgetColor. The best practice is to use unselectedWidgetColor in theme and activeColor in the custom widget.