Flutter DatePicker选中日期颜色修改及输入框文字颜色调整
解决Flutter DatePicker键盘输入文字颜色问题
你遇到的键盘输入日期文字为黑色的问题,是因为ColorScheme无法覆盖输入框的编辑文本样式,需要在ThemeData中同时配置datePickerTheme来针对性修改输入框相关样式,同时保留你原有的配色方案。
修改后的代码如下:
final DateTime? pickedDate = await showDatePicker( context: context, initialDate: DateTime.now(), firstDate: DateTime(2004), lastDate: DateTime(2101), builder: (context, child) { return Theme( data: ThemeData().copyWith( colorScheme: const ColorScheme( brightness: Brightness.light, primary: Color.fromARGB(255, 212, 89, 83), onPrimary: Colors.white, secondary: Color.fromARGB(255, 0, 28, 49), onSecondary: Colors.white, error: Colors.red, onError: Colors.white, surface: Color.fromARGB(255, 0, 28, 49), onSurface: Colors.white, ), // 配置DatePicker专属主题,解决输入框文字颜色问题 datePickerTheme: const DatePickerThemeData( // 直接指定输入框内编辑文本的颜色 textStyle: TextStyle(color: Colors.white), // 统一输入框的提示、标签及下划线样式 inputDecorationTheme: InputDecorationTheme( hintStyle: TextStyle(color: Colors.white), labelStyle: TextStyle(color: Colors.white), enabledBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.white), ), focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.white), ), ), ), // 补充全局文本主题,避免其他文本颜色不统一 textTheme: const TextTheme( bodyLarge: TextStyle(color: Colors.white), bodyMedium: TextStyle(color: Colors.white), ), ), child: child!, ); }, );
关键修改点说明:
datePickerTheme.textStyle:直接强制输入框内的编辑文字为白色,这是解决你问题的核心配置。inputDecorationTheme:同步修改输入框的提示文字、标签文字颜色,以及下划线边框颜色,确保和你设置的深色背景surface配色一致。textTheme:补充全局文本样式,防止DatePicker内其他辅助文本出现颜色错位的情况。
这样配置后,既保留了你原有的ColorScheme样式,又能把键盘输入的日期文字改成白色。
内容的提问来源于stack exchange,提问作者danimtnz
相关产品推荐
相关产品推荐

