如何在Flutter中自定义TimePicker小部件

3

enter image description here

您如何更改输入时间选择器小部件中的紫色选择颜色、紫色边框颜色、时间文本颜色以及 Hour Minute 颜色?似乎找不到 TimePickerThemeData 中的属性。


如果我的回答对你在原始帖子中提到的问题有所帮助,请接受它。如果没有帮助,请提供更多详细信息说明原因。 - void
2个回答

9

你也可以在您的主题中使用状态!当执行某些操作(例如选定或聚焦操作)时,可以控制样式。

这里是一个例子:

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

enter image description here enter image description here enter image description here


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

结果:

在此输入图片描述


1
谢谢!有没有办法直接改变“小时”和“分钟”的文本颜色以及白天/黑夜选择的颜色? - Hector
1
有些“主题颜色”是相互关联的,所以几乎不可能直接更改一个而不影响其他的。 - void
你是否知道如何在不降级Flutter的情况下获取旧版的showTimePicker小部件? - Hector
我觉得如果没有降级选项,这是不可能的。你能否提供一个屏幕截图,展示你想要实现的颜色组合? - void
没问题,只是好奇,你的回答已经足够了,再次感谢。 - Hector

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接