Skip to content

Commit 6dd77cc

Browse files
authored
Fix error when choose noon on time picker (flutter#78478)
1 parent 26b9a01 commit 6dd77cc

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

packages/flutter/lib/src/material/time.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class TimeOfDay {
9191
DayPeriod get period => hour < hoursPerPeriod ? DayPeriod.am : DayPeriod.pm;
9292

9393
/// Which hour of the current period (e.g., am or pm) this time is.
94-
int get hourOfPeriod => hour - periodOffset;
94+
int get hourOfPeriod => hour > hoursPerPeriod ? hour - periodOffset : hour;
9595

9696
/// The hour at which the current period starts.
9797
int get periodOffset => period == DayPeriod.am ? 0 : hoursPerPeriod;

packages/flutter/test/material/time_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,24 @@ void main() {
2525
expect(await pumpTest(false), '7:00 AM');
2626
expect(await pumpTest(true), '07:00');
2727
});
28+
29+
testWidgets('return 12 hours at noon', (WidgetTester tester) async {
30+
Future<String> pumpTest(bool alwaysUse24HourFormat) async {
31+
late String formattedValue;
32+
await tester.pumpWidget(MaterialApp(
33+
home: MediaQuery(
34+
data: MediaQueryData(alwaysUse24HourFormat: alwaysUse24HourFormat),
35+
child: Builder(builder: (BuildContext context) {
36+
formattedValue = const TimeOfDay(hour: 12, minute: 0).format(context);
37+
return Container();
38+
}),
39+
),
40+
));
41+
return formattedValue;
42+
}
43+
44+
expect(await pumpTest(false), '12:00 PM');
45+
expect(await pumpTest(true), '12:00');
46+
});
2847
});
2948
}

0 commit comments

Comments
 (0)