Skip to content

Commit 469b97b

Browse files
[material_ui] Fix todayBorder color being overridden by todayForegroundColor in YearPicker (flutter#12697)
This PR ports flutter#189345 from flutter/flutter to flutter/packages, as part of flutter#188444. Fixes flutter#189298 ## Pre-Review Checklist
1 parent 47a2016 commit 469b97b

4 files changed

Lines changed: 90 additions & 6 deletions

File tree

‎packages/material_ui/lib/src/calendar_date_picker.dart‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,10 +1523,11 @@ class _YearPickerState extends State<YearPicker> {
15231523

15241524
BorderSide? borderSide;
15251525
if (isCurrentYear) {
1526-
borderSide = datePickerTheme.todayBorder ?? defaults.todayBorder;
1527-
if (borderSide != null) {
1528-
borderSide = borderSide.copyWith(color: textColor);
1529-
}
1526+
final bool hasCustomBorderColor =
1527+
datePickerTheme.todayBorder != null && datePickerTheme.todayBorder!.color.opacity != 0.0;
1528+
borderSide = hasCustomBorderColor
1529+
? datePickerTheme.todayBorder
1530+
: (datePickerTheme.todayBorder ?? defaults.todayBorder)?.copyWith(color: textColor);
15301531
}
15311532
final decoration = ShapeDecoration(
15321533
color: background,

‎packages/material_ui/lib/src/date_picker_theme.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ class DatePickerThemeData with Diagnosticable {
285285
final WidgetStateProperty<Color?>? todayBackgroundColor;
286286

287287
/// Overrides the border used to paint the
288-
/// [DatePickerDialog.currentDate] label in the grid of the date
289-
/// picker.
288+
/// [DatePickerDialog.currentDate] label in both the day grid and the year
289+
/// selector of the date picker.
290290
///
291291
/// If the border side's [BorderSide.color] is transparent (has 0 opacity),
292292
/// [todayForegroundColor] is used instead. Otherwise, the border's color
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes `DatePickerThemeData.todayBorder` color being overridden by `todayForegroundColor` in the year selector.
3+
version: patch

‎packages/material_ui/test/calendar_date_picker_test.dart‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ void main() {
7171
DateTime? currentDate,
7272
ValueChanged<DateTime>? onChanged,
7373
TextDirection textDirection = TextDirection.ltr,
74+
ThemeData? theme,
7475
}) {
7576
return MaterialApp(
77+
theme: theme,
7678
home: Material(
7779
child: Directionality(
7880
textDirection: textDirection,
@@ -2102,6 +2104,84 @@ void main() {
21022104
});
21032105
});
21042106

2107+
ShapeDecoration? findYearDecoration(WidgetTester tester, String year) {
2108+
final Container container = tester.widget<Container>(
2109+
find.ancestor(of: find.text(year), matching: find.byType(Container)).first,
2110+
);
2111+
2112+
return container.decoration as ShapeDecoration?;
2113+
}
2114+
2115+
// Regression test for https://github.com/flutter/flutter/issues/189298
2116+
testWidgets('Non-null todayBorder color should be respected over todayForegroundColor', (
2117+
WidgetTester tester,
2118+
) async {
2119+
const Color customBorderColor = Colors.red;
2120+
await tester.pumpWidget(
2121+
yearPicker(
2122+
theme: ThemeData(
2123+
datePickerTheme: DatePickerThemeData(
2124+
todayBorder: const BorderSide(color: customBorderColor),
2125+
todayForegroundColor: WidgetStateProperty.all(Colors.blue),
2126+
),
2127+
),
2128+
),
2129+
);
2130+
2131+
// The current year should be painted with custom border color.
2132+
final ShapeDecoration? decoration = findYearDecoration(tester, '2016');
2133+
final shape = decoration!.shape as OutlinedBorder;
2134+
expect(shape.side.color, customBorderColor);
2135+
});
2136+
2137+
// Regression test for https://github.com/flutter/flutter/issues/189298.
2138+
testWidgets('Non-null todayBorder color is used even when disabled', (WidgetTester tester) async {
2139+
const Color customBorderColor = Colors.red;
2140+
await tester.pumpWidget(
2141+
yearPicker(
2142+
firstDate: DateTime(2018, DateTime.june, 9),
2143+
lastDate: DateTime(2030, DateTime.december, 15),
2144+
selectedDate: DateTime(2020),
2145+
currentDate: DateTime(2016), // Not between first and last date.
2146+
theme: ThemeData(
2147+
datePickerTheme: DatePickerThemeData(
2148+
todayBorder: const BorderSide(color: customBorderColor),
2149+
todayForegroundColor: WidgetStateProperty.all(Colors.blue),
2150+
),
2151+
),
2152+
),
2153+
);
2154+
2155+
// The current year should be painted with the custom border color,
2156+
// not with foreground color opacity applied, even if it's disabled.
2157+
final ShapeDecoration? decoration = findYearDecoration(tester, '2016');
2158+
final shape = decoration!.shape as OutlinedBorder;
2159+
expect(shape.side.color, customBorderColor);
2160+
});
2161+
2162+
// Regression test for https://github.com/flutter/flutter/issues/189298.
2163+
testWidgets('Transparent todayBorder should fall back to foreground color', (
2164+
WidgetTester tester,
2165+
) async {
2166+
const Color customForegroundColor = Colors.green;
2167+
await tester.pumpWidget(
2168+
yearPicker(
2169+
theme: ThemeData(
2170+
datePickerTheme: DatePickerThemeData(
2171+
todayBorder: const BorderSide(color: Color(0x00000000)),
2172+
todayForegroundColor: WidgetStateProperty.all(customForegroundColor),
2173+
),
2174+
),
2175+
),
2176+
);
2177+
2178+
// The current year should use the foreground color since
2179+
// todayBorder color is transparent.
2180+
final ShapeDecoration? decoration = findYearDecoration(tester, '2016');
2181+
final shape = decoration!.shape as OutlinedBorder;
2182+
expect(shape.side.color, customForegroundColor);
2183+
});
2184+
21052185
group('Calendar Delegate', () {
21062186
testWidgets('Defaults to Gregorian calendar system', (WidgetTester tester) async {
21072187
await tester.pumpWidget(

0 commit comments

Comments
 (0)