英文:
Change the selected color in the material library YearPicker in Flutter
问题
我想要更改Yearpicker的选中颜色。
我想要将选中的颜色更改为绿色。对于简单的日期选择器,有一个可用于更改主题的构建方法,但对于Yearpicker,我找不到任何方法。
return AlertDialog(
title: const Text("选择年份"),
content: SizedBox(
width: 300,
height: 300,
child: YearPicker(
firstDate: DateTime(DateTime.now().year - 23, 1),
lastDate: DateTime(DateTime.now().year),
initialDate: DateTime.now(),
selectedDate: selectedDate,
onChanged: (DateTime dateTime) {
Navigator.pop(context);
},
),
),
);
英文:
I want to change Selected color for Yearpicker
I want to change selected color to green
In simple Date picker there is Build method available for change in theme but For Yearpicker i could not find any way.
return AlertDialog(
title: const Text("Select Year"),
content: SizedBox(
width: 300,
height: 300,
child: YearPicker(
firstDate: DateTime(DateTime.now().year - 23, 1),
lastDate: DateTime(DateTime.now().year),
initialDate: DateTime.now(),
selectedDate: selectedDate,
onChanged: (DateTime dateTime) {
Navigator.pop(context);
},
),
),
);
答案1
得分: 2
你可以像这样更改颜色
floatingActionButton: 新主题(
数据: Theme.of(context).copyWith(
primaryColor: Colors.amber,
),
子项: 新Builder(
builder: (context) => 新浮动操作按钮(
子项: 新图标(Icons.date_range),
onPressed: () => showDatePicker(
context: context,
initialDate: 新DateTime.now(),
firstDate: 新DateTime.now().subtract(新Duration(days: 30)),
lastDate: 新DateTime.now().add(新Duration(days: 30)),
),
),
),
),
英文:
You can change the color like this
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)),
),
),
),
),
答案2
得分: 0
用主题小部件包装它
return AlertDialog(
title: const Text("选择年份"),
content: SizedBox(
width: 300,
height: 300,
child: Theme(
data: ThemeData.light().copyWith(
colorScheme: ColorScheme.light(
primary: Colors.green, // 这会改变年份选择器的颜色
),
),
child: YearPicker(
firstDate: DateTime(DateTime.now().year - 23, 1),
lastDate: DateTime(DateTime.now().year),
initialDate: DateTime.now(),
selectedDate: selectedDate,
onChanged: (DateTime dateTime) {
Navigator.pop(context);
},
),
),
),
)
英文:
By wrapping it with a Theme Widget
return AlertDialog(
title: const Text("Select Year"),
content: SizedBox(
width: 300,
height: 300,
child: Theme(
data: ThemeData.light().copyWith(
colorScheme: ColorScheme.light(
primary: Colors
.green, // This changes the color of the year picker
),
),
child: YearPicker(
firstDate: DateTime(DateTime.now().year - 23, 1),
lastDate: DateTime(DateTime.now().year),
initialDate: DateTime.now(),
selectedDate: selectedDate,
onChanged: (DateTime dateTime) {
Navigator.pop(context);
},
),
),
),
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论