在Flutter中更改材料库YearPicker中的选定颜色。

huangapple go评论63阅读模式
英文:

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

Without any changes.

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);
                  },
                ),
              ),
            ),
          )

huangapple
  • 本文由 发表于 2023年6月15日 19:04:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定