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.
Related
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 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!
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.
I'm using the showAboutDialog function from flutter to show used licences in my project. How ever I'm stuck with changing the text color of the VIEW LICENSES and CLOSE textbuttons. See this image for clarification:
This is my code:
...
onTap: () {
showAboutDialog(
context: context,
applicationName: 'bla',
applicationLegalese: 'November 2023',
);
},
What I tried so far is looking for a color field inside the showAboutDialog how ever I could not find anything. I'm assuming that I could change the color in my MaterialApp ThemeData. Unfortunately I was not able to find the specific theme to override the default styling of those textbuttons.
I tried the following in my MaterialApp ThemeData to change the color of VIEW LICENSES and CLOSE to green but that did not change anything:
textButtonTheme: TextButtonThemeData(style: ButtonStyle(foregroundColor: MaterialStateProperty.all<Color>(Colors.green))
Any ideas about this?
I was not satisfied with the answers here because all were showing only MaterialColor use-cases and I wanted a custom color. But I finally found something explaining it well on the following link.
https://blog.logrocket.com/new-material-buttons-in-flutter/
Basically, what is confusing is that the new design uses the primary color instead of the textStyle property. You can still apply the other answers to change the overall theme using a MaterialColor, and you can override the existing color theme using any color by using primary under TextButton.styleFrom.
Example for anywhere in the app:
TextButton(
onPressed: () {},
style: TextButton.styleFrom(
foregroundColor: Colors.pink,
),
child: Text(
'TextButton (New)',
style: TextStyle(fontSize: 30),
),
)
Example for the theme:
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
primary: kDarkColor, // This is a custom color variable
textStyle: GoogleFonts.fredokaOne(),
),
),
You can use this:
return MaterialApp(
theme: ThemeData.dark().copyWith(
textButtonTheme: TextButtonThemeData(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith(
(state) => Colors.orange)))),
home: MyWidget(),
);
MaterialStateProperty.resolveWith takes a function, you can specify the color based on states, such as
MaterialState.pressed,
MaterialState.hovered,
MaterialState.focused,
More info on this.
How about this one?
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
colorScheme: ColorScheme.fromSwatch(
primarySwatch: Colors.green,
).copyWith(),
),
debugShowCheckedModeBanner: false,
home: YourScreen(),
);
}
i run this code.
after some research i find out this way to change colour.
for this you need to set application main theme colour change, like this
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.brown,//i am set brown colour,you can set your colour here
),
debugShowCheckedModeBanner: false,
home: YourScreen(),
);
}
after this its work,
showAboutDialog(
context: context,
applicationName: 'bla',
applicationLegalese: 'November 2023',
);
If you want to change the colors only for the dialog and not for the whole app, you have to create a new context. Surround the Button that showing the dialog with a Theme and a Builder
Theme(
data: Theme.of(context).copyWith(
colorScheme: colorScheme.copyWith(primary: Colors.green),
),
child: Builder(
builder: (context) {
return ListTile(
title: Text('show dialog'),
onTap: () => showAboutDialog(
context: context,
...)
);
},
),
)