How to customize a TimePicker widget in Flutter - flutter

How can you change the Purple selection color, Purple border color, time text color and Hour and Minute color from the input time picker widget, can't seem to find the properties in TimePickerThemeData

showTimePicker(
context: context,
initialTime: TimeOfDay(hour: 10, minute: 47),
builder: (context, child) {
return Theme(
data: ThemeData.light().copyWith(
colorScheme: ColorScheme.light(
// change the border color
primary: Colors.red,
// change the text color
onSurface: Colors.purple,
),
// button colors
buttonTheme: ButtonThemeData(
colorScheme: ColorScheme.light(
primary: Colors.green,
),
),
),
child: child,
);
},
);
Result:

You are able to use states on your theme too! Controlling the styles when making some action (like selected or focused actions).
Here an example:
final theme = ThemeData.light().copyWith(
timePickerTheme: TimePickerThemeData(
backgroundColor: Colors.green.shade200,
hourMinuteColor: MaterialStateColor.resolveWith((states) =>
states.contains(MaterialState.selected)
? Colors.blue.withOpacity(0.2)
: Colors.orange),
hourMinuteTextColor: MaterialStateColor.resolveWith((states) =>
states.contains(MaterialState.selected)
? Colors.pink
: Colors.deepPurple),
dialHandColor: Colors.pink.shade800,
dialBackgroundColor: Colors.purple.withOpacity(0.5),
dialTextColor: MaterialStateColor.resolveWith((states) =>
states.contains(MaterialState.selected)
? Colors.green
: Colors.black),
entryModeIconColor: Colors.yellow
),
textTheme: TextTheme(
overline: TextStyle(
color: Colors.red,
),
),
textButtonTheme: TextButtonThemeData(
style: ButtonStyle(
backgroundColor: MaterialStateColor.resolveWith((states) => Colors.black),
foregroundColor: MaterialStateColor.resolveWith((states) => Colors.green),
overlayColor: MaterialStateColor.resolveWith((states) => Colors.pink),
)));

Related

Hover radius flutter data picker

I’m wondering if there is a possibility to change the hover radius of buttons inside a Date Picker component? The default radius looks too big. Thanks in advance!
Our current code only changes colors, but not the radius of the hover.
return Theme(
data: Theme.of(context).copyWith(
colorScheme: ColorScheme.light(
primary: accentColor, // header background color
onSecondary: colorSecondary,
onPrimary: colorBlack, // header text color
onSurface: colorBlack, // body text color
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
primary: colorBlack, // button text color
),
),
),
child: Center(
child:
ConstrainedBox(
constraints: BoxConstraints(
maxWidth: Responsive.isMobile(context) ? MediaQuery.of(context).size.width : 500.0,
maxHeight: Responsive.isMobile(context) ? MediaQuery.of(context).size.height : 490,
),
child: child,
)
));
try using Theme Widget for date picker
showDatePicker(
builder: (context,child){
return Theme(
data: Theme.of(context).copyWith(
colorScheme: ColorScheme.light(
primary: Colors.black, // header background color
onPrimary: Colors.white, // header text color
onSurface: Colors.black, // body text color
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
primary: Colors.black, // button text color
),
),
),
child: child!,
);
},
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2022),
lastDate: DateTime(2025),
)

flutter: change default Icon text color

I modified textTheme theme data on my application :
ThemeData get lightTheme => ThemeData(
disabledColor: AppColors.disabled,
scaffoldBackgroundColor: AppColors.paper,
textTheme: TextTheme(
button: AppTextStyles.button,
overline: AppTextStyles.overline,
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(AppSize.s32),
),
),
backgroundColor: MaterialStateProperty.resolveWith(
(final states) => states.contains(MaterialState.disabled)
? 50.gray
: 500.primary,
),
),
),
I Used this code too but not worked:
elevatedButtonTheme: ElevatedButtonThemeData(
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(AppSize.s32),
),
),
backgroundColor: MaterialStateProperty.resolveWith(
(final states) => states.contains(MaterialState.disabled)
? 50.gray
: 500.primary,
),
textStyle: MaterialStateProperty.resolveWith(
(final states) {
return const TextStyle(color: Color(0xFF661F1F));
},
),
),
),
This is AppTextStyles.button,:
static final TextStyle _base = GoogleFonts.inder(
color: Colors.black,
fontWeight: FontWeight.w600,
);
static final button = _base.copyWith(
fontSize: 16.sp, fontWeight: FontWeight.w700, color: Colors.black);
But when I added ElevatedButton the text color is still white?
ElevatedButton.icon(
onPressed: () {},
icon: Icon(Icons.access_alarm),
label: Text("Sign in")),
This is my material:
GetMaterialApp(
theme: AppThemes().lightTheme,
Use the foregroundColor property of the ButtonStyle.
foregroundColor: MaterialStateProperty.all(Colors.black)
try change color in foregroundColor inside ButtonStyle.
foregroundColor: MaterialStateProperty.resolveWith(
(final states) => states.contains(MaterialState.disabled)
? Colors.grey
: Color(0xFF661F1F),

Changing OutlinedButton's child (Text widget) forecolor using ThemeData

I am using OutlinedButton widget and I want to find a specific style via themedata.
the OutlinedButton structure is like this:
OutlinedButton(
child: Text("Post"),
onPressed: () {
context
.read<PostsModel>()
.addPost(_title.text, _content.text);
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) => PostPage(),
),
);
},
)
and themedata is like this:
MaterialApp(
...
outlinedButtonTheme: OutlinedButtonThemeData(
style: ButtonStyle(
shape: MaterialStateProperty.all<OutlinedBorder>(
RoundedRectangleBorder(),
),
side: MaterialStateProperty.all<BorderSide>(
BorderSide(color: Colors.white)),
textStyle: MaterialStateProperty.all<TextStyle>(TextStyle(
fontSize: 18,
color: Colors.white,
)),
),
),
)
I was expecting to change the color like fontsize on children but it doesn't happen. Any idea?
I would recommend using the OutlinedButton.styleFrom constructor, it makes things easier:
MaterialApp(
...
outlinedButtonTheme: OutlinedButtonThemeData(
style: OutlinedButton.styleFrom(
textStyle: TextStyle(
fontSize: 18,
),
primary: Colors.white,
),
),
...
)
So for the fontSize, you should adjust the TextStyle, but if you want to change text color, primary is the property you should use.
Also, most probably you would need to restart the app in order for the Theme to be applied.

How to change disabled color of ElevatedButton and OutlinedButton

There's no such property in ElevatedButton and OutlinedButton widget to change the disabled color like a regular RaisedButton.
ElevatedButton(
onPressed: null,
disabledColor: Colors.brown, // Error
}
Use onSurface property if you only want to change the disabled color (this property is also available in OutlinedButton).
ElevatedButton(
onPressed: null,
style: ElevatedButton.styleFrom(
onSurface: Colors.brown,
),
child: Text('ElevatedButton'),
)
For more customizations, use ButtonStyle:
ElevatedButton(
onPressed: null,
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>((states) {
if (states.contains(MaterialState.disabled)) {
return Colors.brown; // Disabled color
}
return Colors.blue; // Regular color
}),
),
child: Text('ElevatedButton'),
)
To apply it for all the ElevatedButton in your app:
MaterialApp(
theme: ThemeData(
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
onSurface: Colors.brown
),
),
),
)
In situation where you want to set an elevated button theme within your app, you can use this:
ThemeData(
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
onSurface: Colors.brown
),
child: Text('ElevatedButton')
))

How to make date picker 'Ok'- and 'Cancel' button text black in Dart Flutter?

I have a date picker in Flutter. I want to make the 'Ok' and 'Cancel' button text black. But I can't find the correct theme setting.
Code displaying the date picker:
Future<void> selectDate(
BuildContext context,
DateTime initialDate,
TextEditingController controller,
Function(DateTime picked, TextEditingController controller) onDatePicked,
String label) async {
final 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: Colors.green[600],
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,
);
},
initialDate: initialDate.toLocal(),
firstDate: DateTime(2015, 8).toLocal(),
lastDate: DateTime(2101).toLocal(),
fieldLabelText: label);
if (picked != null) onDatePicked(picked, controller);
}
How it currently looks:
Thanks!
Use this in your ThemeData:
textButtonTheme: new TextButtonThemeData(style: TextButton.styleFrom(primary: Colors.black),),