diff --git a/packages/flutter/lib/src/material/time_picker_theme.dart b/packages/flutter/lib/src/material/time_picker_theme.dart index 30983f9ec1..aa6a3e20fc 100644 --- a/packages/flutter/lib/src/material/time_picker_theme.dart +++ b/packages/flutter/lib/src/material/time_picker_theme.dart @@ -8,6 +8,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; import 'button_style.dart'; +import 'colors.dart'; import 'input_decorator.dart'; import 'material_state.dart'; import 'theme.dart'; @@ -43,7 +44,7 @@ class TimePickerThemeData with Diagnosticable { this.cancelButtonStyle, this.confirmButtonStyle, this.dayPeriodBorderSide, - this.dayPeriodColor, + Color? dayPeriodColor, this.dayPeriodShape, this.dayPeriodTextColor, this.dayPeriodTextStyle, @@ -61,7 +62,7 @@ class TimePickerThemeData with Diagnosticable { this.inputDecorationTheme, this.padding, this.shape, - }); + }) : _dayPeriodColor = dayPeriodColor; /// The background color of a time picker. /// @@ -102,7 +103,21 @@ class TimePickerThemeData with Diagnosticable { /// brightness is [Brightness.dark]. /// If the segment is not selected, [Colors.transparent] is used to allow the /// [Dialog]'s color to be used. - final Color? dayPeriodColor; + Color? get dayPeriodColor { + if (_dayPeriodColor == null || _dayPeriodColor is MaterialStateColor) { + return _dayPeriodColor; + } + return MaterialStateColor.resolveWith((Set states) { + if (states.contains(MaterialState.selected)) { + return _dayPeriodColor; + } + // The unselected day period should match the overall picker dialog color. + // Making it transparent enables that without being redundant and allows + // the optional elevation overlay for dark mode to be visible. + return Colors.transparent; + }); + } + final Color? _dayPeriodColor; /// The shape of the day period that the time picker uses. /// diff --git a/packages/flutter/test/material/time_picker_theme_test.dart b/packages/flutter/test/material/time_picker_theme_test.dart index 721989ffb9..a2085920f8 100644 --- a/packages/flutter/test/material/time_picker_theme_test.dart +++ b/packages/flutter/test/material/time_picker_theme_test.dart @@ -18,7 +18,7 @@ void main() { expect(identical(TimePickerThemeData.lerp(data, data, 0.5), data), true); }); - test('TimePickerThemeData null fields by default', () { + test('TimePickerThemeData has null fields by default', () { const TimePickerThemeData timePickerTheme = TimePickerThemeData(); expect(timePickerTheme.backgroundColor, null); expect(timePickerTheme.cancelButtonStyle, null); @@ -64,7 +64,7 @@ void main() { cancelButtonStyle: ButtonStyle(foregroundColor: MaterialStatePropertyAll(Color(0xfffffff1))), confirmButtonStyle: ButtonStyle(foregroundColor: MaterialStatePropertyAll(Color(0xfffffff2))), dayPeriodBorderSide: BorderSide(color: Color(0xfffffff3)), - dayPeriodColor: Color(0xfffffff4), + dayPeriodColor: Color(0x00000000), dayPeriodShape: RoundedRectangleBorder( side: BorderSide(color: Color(0xfffffff5)), ), @@ -102,7 +102,7 @@ void main() { 'cancelButtonStyle: ButtonStyle#00000(foregroundColor: MaterialStatePropertyAll(Color(0xfffffff1)))', 'confirmButtonStyle: ButtonStyle#00000(foregroundColor: MaterialStatePropertyAll(Color(0xfffffff2)))', 'dayPeriodBorderSide: BorderSide(color: Color(0xfffffff3))', - 'dayPeriodColor: Color(0xfffffff4)', + 'dayPeriodColor: Color(0x00000000)', 'dayPeriodShape: RoundedRectangleBorder(BorderSide(color: Color(0xfffffff5)), BorderRadius.zero)', 'dayPeriodTextColor: Color(0xfffffff6)', 'dayPeriodTextStyle: TextStyle(inherit: true, color: Color(0xfffffff7))', @@ -764,6 +764,41 @@ void main() { final InputDecoration hourDecoration = _textField(tester, '7').decoration!; expect(hourDecoration.fillColor?.value, timePickerTheme.hourMinuteColor?.value); }); + + testWidgetsWithLeakTracking('Time picker dayPeriodColor does the right thing with non-MaterialStateColor', (WidgetTester tester) async { + final TimePickerThemeData timePickerTheme = _timePickerTheme().copyWith(dayPeriodColor: Colors.red); + final ThemeData theme = ThemeData(timePickerTheme: timePickerTheme); + await tester.pumpWidget(_TimePickerLauncher(themeData: theme, entryMode: TimePickerEntryMode.input)); + await tester.tap(find.text('X')); + await tester.pumpAndSettle(const Duration(seconds: 1)); + + final Material amMaterial = _textMaterial(tester, 'AM'); + expect(amMaterial.color, Colors.red); + + final Material pmMaterial = _textMaterial(tester, 'PM'); + expect(pmMaterial.color, Colors.transparent); + }); + + testWidgetsWithLeakTracking('Time picker dayPeriodColor does the right thing with MaterialStateColor', (WidgetTester tester) async { + final MaterialStateColor testColor = MaterialStateColor.resolveWith((Set states) { + if (states.contains(MaterialState.selected)) { + return Colors.green; + } + return Colors.blue; + }); + + final TimePickerThemeData timePickerTheme = _timePickerTheme().copyWith(dayPeriodColor: testColor); + final ThemeData theme = ThemeData(timePickerTheme: timePickerTheme); + await tester.pumpWidget(_TimePickerLauncher(themeData: theme, entryMode: TimePickerEntryMode.input)); + await tester.tap(find.text('X')); + await tester.pumpAndSettle(const Duration(seconds: 1)); + + final Material amMaterial = _textMaterial(tester, 'AM'); + expect(amMaterial.color, Colors.green); + + final Material pmMaterial = _textMaterial(tester, 'PM'); + expect(pmMaterial.color, Colors.blue); + }); } final Color _selectedColor = Colors.green[100]!;