|
24 | 24 | - [2. (Option 1) `Material` Date/Time Pickers](#2-option-1-material-datetime-pickers)
|
25 | 25 | - [2.1 Editing **date** and **time** separately](#21-editing-date-and-time-separately)
|
26 | 26 | - [2.2 Editing **date** and **time** with single button](#22-editing-date-and-time-with-single-button)
|
| 27 | + - [2.3 Forcing `24-hour` type input in `TimePicker`](#23-forcing-24-hour-type-input-in-timepicker) |
| 28 | + - [3. (Option 2) `Cupertino` Pickers](#3-option-2-cupertino-pickers) |
27 | 29 |
|
28 | 30 |
|
29 | 31 | # Why? 🤷
|
@@ -458,3 +460,40 @@ You should be able to set both *date* and *time*.
|
458 | 460 |
|
459 | 461 | Great job! 🥳
|
460 | 462 |
|
| 463 | + |
| 464 | +### 2.3 Forcing `24-hour` type input in `TimePicker` |
| 465 | + |
| 466 | +Sometimes, it may be useful to unconditionally display |
| 467 | +the `TimePicker` in a 24-hour format, |
| 468 | +instead of using the AM/PM system. |
| 469 | + |
| 470 | +To override this behaviour, |
| 471 | +we ought to use [`MediaQuery`](https://api.flutter.dev/flutter/widgets/MediaQuery-class.html) |
| 472 | +to change the locale behaviour. |
| 473 | + |
| 474 | +Open `lib/material.dart`, |
| 475 | +and locate the `pickTime` function we've implemented. |
| 476 | +Add the `builder` parameter, like so. |
| 477 | + |
| 478 | +```dart |
| 479 | + /// Opens time picker and returns possible `TimeOfDay` object. |
| 480 | + Future<TimeOfDay?> pickTime() => showTimePicker( |
| 481 | + context: context, |
| 482 | + initialTime: TimeOfDay(hour: dateTime.hour, minute: dateTime.minute), |
| 483 | + builder: (BuildContext context, Widget? child) { |
| 484 | + return MediaQuery( |
| 485 | + data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true), |
| 486 | + child: child!, |
| 487 | + ); |
| 488 | + }); |
| 489 | +``` |
| 490 | + |
| 491 | +Now, if you run the app, |
| 492 | +you should see the changes! |
| 493 | + |
| 494 | +<p align='center'> |
| 495 | + <img width="250" src="https://github.com/dwyl/flutter-date-time-tutorial/assets/17494745/40feee37-01fa-407c-8da1-660b8df179ff"> |
| 496 | +</p> |
| 497 | + |
| 498 | + |
| 499 | +## 3. (Option 2) `Cupertino` Pickers |
0 commit comments