问题描述
我正在使用 showDatePicker()
方法在flutter应用程序中显示日期选择器。如何自定义日期选择器的颜色?
I’m using the showDatePicker()
method to display a date picker in my flutter application. How do I customize the colors of the date picker?
这是我主题的代码:
class CustomTheme extends Theme {
/*
* Colors:
* Primary Blue: #335C81 (51, 92, 129)
* Light Blue: #74B3CE (116, 179, 206)
* Yellow: #FCA311 (252, 163, 17)
* Red: #E15554 (255, 85, 84)
* Green: #3BB273 (59, 178, 115)
*/
static int _fullAlpha = 255;
static Color blueDark = new Color.fromARGB(_fullAlpha, 51, 92, 129);
static Color blueLight = new Color.fromARGB(_fullAlpha, 116, 179, 206);
static Color yellow = new Color.fromARGB(_fullAlpha, 252, 163, 17);
static Color red = new Color.fromARGB(_fullAlpha, 255, 85, 84);
static Color green = new Color.fromARGB(_fullAlpha, 59, 178, 115);
static Color activeIconColor = yellow;
CustomTheme(Widget child): super(
child: child,
data: new ThemeData(
primaryColor: blueDark,
accentColor: yellow,
cardColor: blueLight,
backgroundColor: blueDark,
highlightColor: red,
splashColor: green
)
);
}
这是我用主题包装页面的代码:
Here is my code for wrapping the page in the theme:
@override
Widget build(BuildContext context) {
[...]
return new CustomTheme(
new Scaffold(
[...]
)
);
}
推荐答案
我假设您要自定义主题的日期选择器 。通常,日期选择器遵循您的主要主题。
I assume that you want to customize the date picker differently from your main theme. Normally, date picker follow your main theme.
如果是这样,请在 Builder
中包装触发操作的按钮。在主题
中。例如,这是一个FAB,它会弹出一个橙色的日期选择器(在轻型应用程序主题中),并继承主题的其余部分。
If so, wrap the button that triggers the action in a Builder
inside a Theme
. For example, here's a FAB that pops up an orange date picker (in a light material app theme), inheriting the rest from the main theme.
floatingActionButton: new Theme(
data: Theme.of(context).copyWith(
primaryColor: Colors.amber,
),
child: new Builder(
builder: (context) => new FloatingActionButton(
child: new Icon(Icons.date_range),
onPressed: () => showDatePicker(
context: context,
initialDate: new DateTime.now(),
firstDate:
new DateTime.now().subtract(new Duration(days: 30)),
lastDate: new DateTime.now().add(new Duration(days: 30)),
),
),
),
),
检查date_picker.dart的源代码,以了解主题的哪些部分会影响日期选择的不同方面
Check the source code of date_picker.dart to see which parts of the Theme affect different aspects of the date picker.
如果您只希望选择器遵循主主题,这是一个有效的示例
If you just want the picker to follow the main theme, here's a working example
import 'package:flutter/material.dart';
class PickerThemeDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: const Text('Picker theme demo')),
body: new Container(),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.date_range),
onPressed: () => showDatePicker(
context: context,
initialDate: new DateTime.now(),
firstDate: new DateTime.now().subtract(new Duration(days: 30)),
lastDate: new DateTime.now().add(new Duration(days: 30)),
),
),
);
}
}
Color hexToColor(int rgb) => new Color(0xFF000000 + rgb);
class CustomTheme extends Theme {
//Primary Blue: #335C81 (51, 92, 129)
//Light Blue: #74B3CE (116, 179, 206)
//Yellow: #FCA311 (252, 163, 17)
//Red: #E15554 (255, 85, 84)
//Green: #3BB273 (59, 178, 115)
static Color blueDark = hexToColor(0x335C81);
static Color blueLight = hexToColor(0x74B3CE);
static Color yellow = hexToColor(0xFCA311);
static Color red = hexToColor(0xE15554);
static Color green = hexToColor(0x3BB273);
CustomTheme(Widget child)
: super(
child: child,
data: new ThemeData(
primaryColor: blueDark,
accentColor: yellow,
cardColor: blueLight,
backgroundColor: blueDark,
highlightColor: red,
splashColor: green,
),
);
}
void main() {
runApp(
new MaterialApp(
home: new CustomTheme(new PickerThemeDemo()),
),
);
}
如果要将主题应用于整个应用程序,则可以添加主题最简洁(无需CustomTheme类)到Material应用程序:
If you want to apply the theme to the whole app, it can be added most concisely (without the need for the CustomTheme class) to the Material app:
Color hexToColor(int rgb) => new Color(0xFF000000 + rgb);
void main() {
runApp(
new MaterialApp(
theme: new ThemeData(
brightness: Brightness.light,
primaryColor: hexToColor(0x335C81),
accentColor: hexToColor(0xFCA311),
splashColor: hexToColor(0x3BB273),
),
home: new PickerThemeDemo(),
),
);
}
这篇关于如何自定义日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!