Repository navigation
Expand file tree
/
Copy pathtime_picker.dart
More file actions
3967 lines (3594 loc) · 134 KB
/
Copy pathtime_picker.dart
File metadata and controls
3967 lines (3594 loc) · 134 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// @docImport 'date_picker.dart';
/// @docImport 'text_field.dart';
library;
import 'dart:async';
import 'dart:math' as math;
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'button_style.dart';
import 'color_scheme.dart';
import 'colors.dart';
import 'constants.dart';
import 'curves.dart';
import 'debug.dart';
import 'dialog.dart';
import 'icon_button.dart';
import 'icons.dart';
import 'ink_well.dart';
import 'input_border.dart';
import 'input_decorator.dart';
import 'material.dart';
import 'material_localizations.dart';
import 'material_state.dart';
import 'text_button.dart';
import 'text_form_field.dart';
import 'text_theme.dart';
import 'theme.dart';
import 'time.dart';
import 'time_picker_theme.dart';
// Examples can assume:
// late BuildContext context;
const Duration _kDialogSizeAnimationDuration = Duration(milliseconds: 200);
const Duration _kDialAnimateDuration = Duration(milliseconds: 200);
const double _kTwoPi = 2 * math.pi;
const Duration _kVibrateCommitDelay = Duration(milliseconds: 100);
const double _kTimePickerHeaderLandscapeWidth = 216;
const double _kTimePickerInnerDialOffset = 28;
const double _kTimePickerDialMinRadius = 50;
const double _kTimePickerDialPadding = 28;
/// Interactive input mode of the time picker dialog.
///
/// In [TimePickerEntryMode.dial] mode, a clock dial is displayed and the user
/// taps or drags the time they wish to select. In TimePickerEntryMode.input]
/// mode, [TextField]s are displayed and the user types in the time they wish to
/// select.
///
/// See also:
///
/// * [showTimePicker], a function that shows a [TimePickerDialog] and returns
/// the selected time as a [Future].
enum TimePickerEntryMode {
/// User picks time from a clock dial.
///
/// Can switch to [input] by activating a mode button in the dialog.
dial,
/// User can input the time by typing it into text fields.
///
/// Can switch to [dial] by activating a mode button in the dialog.
input,
/// User can only pick time from a clock dial.
///
/// There is no user interface to switch to another mode.
dialOnly,
/// User can only input the time by typing it into text fields.
///
/// There is no user interface to switch to another mode.
inputOnly,
}
// Whether the dial-mode time picker is currently selecting the hour or the
// minute.
enum _HourMinuteMode { hour, minute }
// Aspects of _TimePickerModel that can be depended upon.
enum _TimePickerAspect {
use24HourFormat,
useMaterial3,
entryMode,
hourMinuteMode,
onHourMinuteModeChanged,
onHourDoubleTapped,
onMinuteDoubleTapped,
hourDialType,
selectedTime,
onSelectedTimeChanged,
orientation,
theme,
defaultTheme,
}
class _TimePickerModel extends InheritedModel<_TimePickerAspect> {
const _TimePickerModel({
required this.entryMode,
required this.hourMinuteMode,
required this.onHourMinuteModeChanged,
required this.onHourDoubleTapped,
required this.onMinuteDoubleTapped,
required this.selectedTime,
required this.onSelectedTimeChanged,
required this.use24HourFormat,
required this.useMaterial3,
required this.hourDialType,
required this.orientation,
required this.theme,
required this.defaultTheme,
required super.child,
});
final TimePickerEntryMode entryMode;
final _HourMinuteMode hourMinuteMode;
final ValueChanged<_HourMinuteMode> onHourMinuteModeChanged;
final GestureTapCallback onHourDoubleTapped;
final GestureTapCallback onMinuteDoubleTapped;
final TimeOfDay selectedTime;
final ValueChanged<TimeOfDay> onSelectedTimeChanged;
final bool use24HourFormat;
final bool useMaterial3;
final _HourDialType hourDialType;
final Orientation orientation;
final TimePickerThemeData theme;
final _TimePickerDefaults defaultTheme;
static _TimePickerModel of(BuildContext context, [_TimePickerAspect? aspect]) =>
InheritedModel.inheritFrom<_TimePickerModel>(context, aspect: aspect)!;
static TimePickerEntryMode entryModeOf(BuildContext context) =>
of(context, _TimePickerAspect.entryMode).entryMode;
static _HourMinuteMode hourMinuteModeOf(BuildContext context) =>
of(context, _TimePickerAspect.hourMinuteMode).hourMinuteMode;
static TimeOfDay selectedTimeOf(BuildContext context) =>
of(context, _TimePickerAspect.selectedTime).selectedTime;
static bool use24HourFormatOf(BuildContext context) =>
of(context, _TimePickerAspect.use24HourFormat).use24HourFormat;
static bool useMaterial3Of(BuildContext context) =>
of(context, _TimePickerAspect.useMaterial3).useMaterial3;
static _HourDialType hourDialTypeOf(BuildContext context) =>
of(context, _TimePickerAspect.hourDialType).hourDialType;
static Orientation orientationOf(BuildContext context) =>
of(context, _TimePickerAspect.orientation).orientation;
static TimePickerThemeData themeOf(BuildContext context) =>
of(context, _TimePickerAspect.theme).theme;
static _TimePickerDefaults defaultThemeOf(BuildContext context) =>
of(context, _TimePickerAspect.defaultTheme).defaultTheme;
static void setSelectedTime(BuildContext context, TimeOfDay value) =>
of(context, _TimePickerAspect.onSelectedTimeChanged).onSelectedTimeChanged(value);
static void setHourMinuteMode(BuildContext context, _HourMinuteMode value) =>
of(context, _TimePickerAspect.onHourMinuteModeChanged).onHourMinuteModeChanged(value);
@override
bool updateShouldNotifyDependent(
_TimePickerModel oldWidget,
Set<_TimePickerAspect> dependencies,
) {
if (use24HourFormat != oldWidget.use24HourFormat &&
dependencies.contains(_TimePickerAspect.use24HourFormat)) {
return true;
}
if (useMaterial3 != oldWidget.useMaterial3 &&
dependencies.contains(_TimePickerAspect.useMaterial3)) {
return true;
}
if (entryMode != oldWidget.entryMode && dependencies.contains(_TimePickerAspect.entryMode)) {
return true;
}
if (hourMinuteMode != oldWidget.hourMinuteMode &&
dependencies.contains(_TimePickerAspect.hourMinuteMode)) {
return true;
}
if (onHourMinuteModeChanged != oldWidget.onHourMinuteModeChanged &&
dependencies.contains(_TimePickerAspect.onHourMinuteModeChanged)) {
return true;
}
if (onHourMinuteModeChanged != oldWidget.onHourDoubleTapped &&
dependencies.contains(_TimePickerAspect.onHourDoubleTapped)) {
return true;
}
if (onHourMinuteModeChanged != oldWidget.onMinuteDoubleTapped &&
dependencies.contains(_TimePickerAspect.onMinuteDoubleTapped)) {
return true;
}
if (hourDialType != oldWidget.hourDialType &&
dependencies.contains(_TimePickerAspect.hourDialType)) {
return true;
}
if (selectedTime != oldWidget.selectedTime &&
dependencies.contains(_TimePickerAspect.selectedTime)) {
return true;
}
if (onSelectedTimeChanged != oldWidget.onSelectedTimeChanged &&
dependencies.contains(_TimePickerAspect.onSelectedTimeChanged)) {
return true;
}
if (orientation != oldWidget.orientation &&
dependencies.contains(_TimePickerAspect.orientation)) {
return true;
}
if (theme != oldWidget.theme && dependencies.contains(_TimePickerAspect.theme)) {
return true;
}
if (defaultTheme != oldWidget.defaultTheme &&
dependencies.contains(_TimePickerAspect.defaultTheme)) {
return true;
}
return false;
}
@override
bool updateShouldNotify(_TimePickerModel oldWidget) {
return use24HourFormat != oldWidget.use24HourFormat ||
useMaterial3 != oldWidget.useMaterial3 ||
entryMode != oldWidget.entryMode ||
hourMinuteMode != oldWidget.hourMinuteMode ||
onHourMinuteModeChanged != oldWidget.onHourMinuteModeChanged ||
onHourDoubleTapped != oldWidget.onHourDoubleTapped ||
onMinuteDoubleTapped != oldWidget.onMinuteDoubleTapped ||
hourDialType != oldWidget.hourDialType ||
selectedTime != oldWidget.selectedTime ||
onSelectedTimeChanged != oldWidget.onSelectedTimeChanged ||
orientation != oldWidget.orientation ||
theme != oldWidget.theme ||
defaultTheme != oldWidget.defaultTheme;
}
}
/// The header for the time picker in dial mode.
class _DialTimePickerHeader extends StatelessWidget {
const _DialTimePickerHeader({required this.helpText});
final String helpText;
@override
Widget build(BuildContext context) {
assert(_debugDialTimePickerEntryMode(context));
final TimeOfDayFormat timeOfDayFormat = MaterialLocalizations.of(context)
.timeOfDayFormat(alwaysUse24HourFormat: _TimePickerModel.use24HourFormatOf(context));
final _TimePickerDefaults defaultTheme = _TimePickerModel.defaultThemeOf(context);
final Orientation orientation = _TimePickerModel.orientationOf(context);
final double dayPeriodHeight = orientation == Orientation.portrait
? defaultTheme.dayPeriodPortraitSize.height
: defaultTheme.dayPeriodLandscapeSize.height;
final double minInteractiveVerticalPadding = orientation == Orientation.portrait
? math.max(0, 2 * kMinInteractiveDimension - dayPeriodHeight)
: math.max(0, kMinInteractiveDimension - dayPeriodHeight);
final _HourDialType hourDialType = _TimePickerModel.hourDialTypeOf(context);
final RenderObjectWidget orientationSpecificHeader = switch (orientation) {
Orientation.portrait => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsetsDirectional.only(
bottom:
(_TimePickerModel.useMaterial3Of(context) ? 20 : 24) -
minInteractiveVerticalPadding / 2,
),
child: Text(
helpText,
style: _TimePickerModel.themeOf(context).helpTextStyle ?? defaultTheme.helpTextStyle,
),
),
Row(
textDirection: timeOfDayFormat == TimeOfDayFormat.a_space_h_colon_mm
? TextDirection.rtl
: TextDirection.ltr,
spacing: 12,
children: <Widget>[
Expanded(
child: Row(
// Hour/minutes should not change positions in RTL locales.
textDirection: TextDirection.ltr,
children: <Widget>[
const Expanded(child: _DialHourControl()),
_TimeSelectorSeparator(timeOfDayFormat: timeOfDayFormat),
const Expanded(child: _DialMinuteControl()),
],
),
),
if (hourDialType == _HourDialType.twelveHour) const _DayPeriodControl(),
],
),
],
),
Orientation.landscape => SizedBox(
width: _kTimePickerHeaderLandscapeWidth,
child: Stack(
children: <Widget>[
Text(
helpText,
style: _TimePickerModel.themeOf(context).helpTextStyle ?? defaultTheme.helpTextStyle,
),
Column(
verticalDirection: timeOfDayFormat == TimeOfDayFormat.a_space_h_colon_mm
? VerticalDirection.up
: VerticalDirection.down,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
spacing: math.max(0, 16 - minInteractiveVerticalPadding / 2),
children: <Widget>[
Row(
// Hour/minutes should not change positions in RTL locales.
textDirection: TextDirection.ltr,
children: <Widget>[
const Expanded(child: _DialHourControl()),
_TimeSelectorSeparator(timeOfDayFormat: timeOfDayFormat),
const Expanded(child: _DialMinuteControl()),
],
),
if (hourDialType == _HourDialType.twelveHour) const _DayPeriodControl(),
],
),
],
),
),
};
return Semantics(
label: MaterialLocalizations.of(context).formatTimeOfDay(
_TimePickerModel.selectedTimeOf(context),
alwaysUse24HourFormat: MediaQuery.alwaysUse24HourFormatOf(context),
),
child: orientationSpecificHeader,
);
}
}
/// The control label for the time selector in dial mode.
class _DialTimeSelectorControl extends StatelessWidget {
const _DialTimeSelectorControl({
required this.text,
required this.onTap,
required this.onDoubleTap,
required this.isSelected,
});
final String text;
final GestureTapCallback onTap;
final GestureTapCallback onDoubleTap;
final bool isSelected;
@override
Widget build(BuildContext context) {
assert(_debugDialTimePickerEntryMode(context));
final TimePickerThemeData timePickerTheme = _TimePickerModel.themeOf(context);
final _TimePickerDefaults defaultTheme = _TimePickerModel.defaultThemeOf(context);
final Color backgroundColor = timePickerTheme.hourMinuteColor ?? defaultTheme.hourMinuteColor;
final ShapeBorder shape = timePickerTheme.hourMinuteShape ?? defaultTheme.hourMinuteShape;
final states = <WidgetState>{if (isSelected) WidgetState.selected};
final Color effectiveTextColor = WidgetStateProperty.resolveAs<Color>(
_TimePickerModel.themeOf(context).hourMinuteTextColor ??
_TimePickerModel.defaultThemeOf(context).hourMinuteTextColor,
states,
);
final TextStyle effectiveStyle = WidgetStateProperty.resolveAs<TextStyle>(
timePickerTheme.hourMinuteTextStyle ?? defaultTheme.hourMinuteTextStyle,
states,
).copyWith(color: effectiveTextColor);
return SizedBox(
height: defaultTheme.hourMinuteSize.height,
child: Material(
color: WidgetStateProperty.resolveAs(backgroundColor, states),
clipBehavior: Clip.antiAlias,
shape: shape,
child: InkWell(
onTap: onTap,
onDoubleTap: isSelected ? onDoubleTap : null,
child: Center(
child: Text(text, style: effectiveStyle, textScaler: TextScaler.noScaling),
),
),
),
);
}
}
/// Displays the hour fragment in dial mode.
///
/// When tapped changes time picker dial mode to [_HourMinuteMode.hour].
class _DialHourControl extends StatelessWidget {
const _DialHourControl();
@override
Widget build(BuildContext context) {
assert(_debugDialTimePickerEntryMode(context));
assert(debugCheckHasMediaQuery(context));
final bool alwaysUse24HourFormat = MediaQuery.alwaysUse24HourFormatOf(context);
final TimeOfDay selectedTime = _TimePickerModel.selectedTimeOf(context);
final MaterialLocalizations localizations = MaterialLocalizations.of(context);
final String formattedHour = localizations.formatHour(
selectedTime,
alwaysUse24HourFormat: _TimePickerModel.use24HourFormatOf(context),
);
TimeOfDay hoursFromSelected(int hoursToAdd) {
switch (_TimePickerModel.hourDialTypeOf(context)) {
case _HourDialType.twentyFourHour:
case _HourDialType.twentyFourHourDoubleRing:
final int selectedHour = selectedTime.hour;
return selectedTime.replacing(hour: (selectedHour + hoursToAdd) % TimeOfDay.hoursPerDay);
case _HourDialType.twelveHour:
// Cycle 1 through 12 without changing day period.
final int periodOffset = selectedTime.periodOffset;
final int hours = selectedTime.hourOfPeriod;
return selectedTime.replacing(
hour: periodOffset + (hours + hoursToAdd) % TimeOfDay.hoursPerPeriod,
);
}
}
final TimeOfDay nextHour = hoursFromSelected(1);
final String formattedNextHour = localizations.formatHour(
nextHour,
alwaysUse24HourFormat: alwaysUse24HourFormat,
);
final TimeOfDay previousHour = hoursFromSelected(-1);
final String formattedPreviousHour = localizations.formatHour(
previousHour,
alwaysUse24HourFormat: alwaysUse24HourFormat,
);
return Semantics(
value: '${localizations.timePickerHourModeAnnouncement} $formattedHour',
excludeSemantics: true,
increasedValue: formattedNextHour,
onIncrease: () {
_TimePickerModel.setSelectedTime(context, nextHour);
},
decreasedValue: formattedPreviousHour,
onDecrease: () {
_TimePickerModel.setSelectedTime(context, previousHour);
},
child: _DialTimeSelectorControl(
isSelected: _TimePickerModel.hourMinuteModeOf(context) == _HourMinuteMode.hour,
text: formattedHour,
onTap: () => _TimePickerModel.setHourMinuteMode(context, _HourMinuteMode.hour),
onDoubleTap: _TimePickerModel.of(
context,
_TimePickerAspect.onHourDoubleTapped,
).onHourDoubleTapped,
),
);
}
}
/// A passive fragment showing a string value.
///
/// Used to display the appropriate separator between the input fields.
class _TimeSelectorSeparator extends StatelessWidget {
const _TimeSelectorSeparator({required this.timeOfDayFormat});
final TimeOfDayFormat timeOfDayFormat;
String _timeSelectorSeparatorValue(TimeOfDayFormat timeOfDayFormat) => switch (timeOfDayFormat) {
TimeOfDayFormat.h_colon_mm_space_a ||
TimeOfDayFormat.a_space_h_colon_mm ||
TimeOfDayFormat.H_colon_mm ||
TimeOfDayFormat.HH_colon_mm => ':',
TimeOfDayFormat.HH_dot_mm => '.',
TimeOfDayFormat.frenchCanadian => 'h',
};
@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
final TimePickerThemeData timePickerTheme = TimePickerTheme.of(context);
final _TimePickerDefaults defaultTheme = theme.useMaterial3
? _TimePickerDefaultsM3(context)
: _TimePickerDefaultsM2(context);
final states = <WidgetState>{};
final Color effectiveTextColor = WidgetStateProperty.resolveAs<Color>(
timePickerTheme.timeSelectorSeparatorColor?.resolve(states) ??
timePickerTheme.hourMinuteTextColor ??
defaultTheme.timeSelectorSeparatorColor?.resolve(states) ??
defaultTheme.hourMinuteTextColor,
states,
);
final TextStyle effectiveStyle = WidgetStateProperty.resolveAs<TextStyle>(
timePickerTheme.timeSelectorSeparatorTextStyle?.resolve(states) ??
timePickerTheme.hourMinuteTextStyle ??
defaultTheme.timeSelectorSeparatorTextStyle?.resolve(states) ??
defaultTheme.hourMinuteTextStyle,
states,
).copyWith(color: effectiveTextColor, height: 1.0);
final double height = switch (_TimePickerModel.entryModeOf(context)) {
TimePickerEntryMode.dial ||
TimePickerEntryMode.dialOnly => defaultTheme.hourMinuteSize.height,
TimePickerEntryMode.input ||
TimePickerEntryMode.inputOnly => defaultTheme.hourMinuteInputSize.height,
};
return ExcludeSemantics(
child: SizedBox(
width: timeOfDayFormat == TimeOfDayFormat.frenchCanadian ? 36 : 24,
height: height,
child: Center(
child: Text(
_timeSelectorSeparatorValue(timeOfDayFormat),
style: effectiveStyle,
textScaler: TextScaler.noScaling,
),
),
),
);
}
}
/// Displays the minute fragment in dial mode.
///
/// When tapped changes time picker dial mode to [_HourMinuteMode.minute].
class _DialMinuteControl extends StatelessWidget {
const _DialMinuteControl();
@override
Widget build(BuildContext context) {
assert(_debugDialTimePickerEntryMode(context));
final MaterialLocalizations localizations = MaterialLocalizations.of(context);
final TimeOfDay selectedTime = _TimePickerModel.selectedTimeOf(context);
final String formattedMinute = localizations.formatMinute(selectedTime);
final TimeOfDay nextMinute = selectedTime.replacing(
minute: (selectedTime.minute + 1) % TimeOfDay.minutesPerHour,
);
final String formattedNextMinute = localizations.formatMinute(nextMinute);
final TimeOfDay previousMinute = selectedTime.replacing(
minute: (selectedTime.minute - 1) % TimeOfDay.minutesPerHour,
);
final String formattedPreviousMinute = localizations.formatMinute(previousMinute);
return Semantics(
excludeSemantics: true,
value: '${localizations.timePickerMinuteModeAnnouncement} $formattedMinute',
increasedValue: formattedNextMinute,
onIncrease: () {
_TimePickerModel.setSelectedTime(context, nextMinute);
},
decreasedValue: formattedPreviousMinute,
onDecrease: () {
_TimePickerModel.setSelectedTime(context, previousMinute);
},
child: _DialTimeSelectorControl(
isSelected: _TimePickerModel.hourMinuteModeOf(context) == _HourMinuteMode.minute,
text: formattedMinute,
onTap: () => _TimePickerModel.setHourMinuteMode(context, _HourMinuteMode.minute),
onDoubleTap: _TimePickerModel.of(
context,
_TimePickerAspect.onMinuteDoubleTapped,
).onMinuteDoubleTapped,
),
);
}
}
/// Displays the am/pm fragment and provides controls for switching between am
/// and pm.
class _DayPeriodControl extends StatelessWidget {
const _DayPeriodControl({this.onPeriodChanged});
final ValueChanged<TimeOfDay>? onPeriodChanged;
void _togglePeriod(BuildContext context) {
final TimeOfDay selectedTime = _TimePickerModel.selectedTimeOf(context);
final int newHour = (selectedTime.hour + TimeOfDay.hoursPerPeriod) % TimeOfDay.hoursPerDay;
final TimeOfDay newTime = selectedTime.replacing(hour: newHour);
if (onPeriodChanged != null) {
onPeriodChanged!(newTime);
} else {
_TimePickerModel.setSelectedTime(context, newTime);
}
}
void _setAm(BuildContext context) {
final TimeOfDay selectedTime = _TimePickerModel.selectedTimeOf(context);
if (selectedTime.period == DayPeriod.am) {
return;
}
_togglePeriod(context);
}
void _setPm(BuildContext context) {
final TimeOfDay selectedTime = _TimePickerModel.selectedTimeOf(context);
if (selectedTime.period == DayPeriod.pm) {
return;
}
_togglePeriod(context);
}
@override
Widget build(BuildContext context) {
final MaterialLocalizations materialLocalizations = MaterialLocalizations.of(context);
final TimePickerThemeData timePickerTheme = _TimePickerModel.themeOf(context);
final _TimePickerDefaults defaultTheme = _TimePickerModel.defaultThemeOf(context);
final TimeOfDay selectedTime = _TimePickerModel.selectedTimeOf(context);
final amSelected = selectedTime.period == DayPeriod.am;
final bool pmSelected = !amSelected;
final BorderSide resolvedSide =
timePickerTheme.dayPeriodBorderSide ?? defaultTheme.dayPeriodBorderSide;
final OutlinedBorder resolvedShape =
(timePickerTheme.dayPeriodShape ?? defaultTheme.dayPeriodShape).copyWith(
side: resolvedSide,
);
Size dayPeriodSize;
final Orientation orientation;
switch (_TimePickerModel.entryModeOf(context)) {
case TimePickerEntryMode.dial:
case TimePickerEntryMode.dialOnly:
orientation = _TimePickerModel.orientationOf(context);
dayPeriodSize = switch (orientation) {
Orientation.portrait => defaultTheme.dayPeriodPortraitSize,
Orientation.landscape => defaultTheme.dayPeriodLandscapeSize,
};
case TimePickerEntryMode.input:
case TimePickerEntryMode.inputOnly:
orientation = Orientation.portrait;
dayPeriodSize = defaultTheme.dayPeriodInputSize;
}
var amShape = resolvedShape;
var pmShape = resolvedShape;
final bool hasRoundedBorder =
resolvedShape is RoundedRectangleBorder && resolvedShape.borderRadius is BorderRadius;
// In order to respect Material touch target guidelines, the Semantics for
// AM and PM buttons needs to expand out of the bounds of the buttons
// (Similarly to Google Agenda).
// To achieve this, instead of using a parent Material which clips
// the Semantics, each button should manage its own shape.
// The logic below "cuts" the given period selector shape in two parts,
// one for the AM button, the other for the PM button. Each sub-shape
// is obtained by removing some rounded corners from the original shape.
switch (orientation) {
case Orientation.portrait:
if (hasRoundedBorder) {
final borderRadius = resolvedShape.borderRadius as BorderRadius;
amShape = resolvedShape.copyWith(
borderRadius: BorderRadius.only(
topLeft: borderRadius.topLeft,
topRight: borderRadius.topRight,
),
);
pmShape = resolvedShape.copyWith(
borderRadius: BorderRadius.only(
bottomLeft: borderRadius.bottomLeft,
bottomRight: borderRadius.bottomRight,
),
);
}
final minInteractiveSize = Size(
dayPeriodSize.width,
math.max(dayPeriodSize.height, 2 * kMinInteractiveDimension),
);
final Widget amButton = _AmPmButton(
selected: amSelected,
onPressed: () => _setAm(context),
label: materialLocalizations.anteMeridiemAbbreviation,
padding: EdgeInsets.only(top: (minInteractiveSize.height - dayPeriodSize.height) / 2),
shape: amShape,
);
final Widget pmButton = _AmPmButton(
selected: pmSelected,
onPressed: () => _setPm(context),
label: materialLocalizations.postMeridiemAbbreviation,
padding: EdgeInsets.only(bottom: (minInteractiveSize.height - dayPeriodSize.height) / 2),
shape: pmShape,
);
return _DayPeriodInputPadding(
minSize: minInteractiveSize,
orientation: orientation,
child: SizedBox.fromSize(
size: minInteractiveSize,
child: Column(
children: <Widget>[
Expanded(child: amButton),
Expanded(child: pmButton),
],
),
),
);
case Orientation.landscape:
if (hasRoundedBorder) {
final borderRadius = resolvedShape.borderRadius as BorderRadius;
amShape = resolvedShape.copyWith(
borderRadius: BorderRadius.only(
topLeft: borderRadius.topLeft,
bottomLeft: borderRadius.bottomLeft,
),
);
pmShape = resolvedShape.copyWith(
borderRadius: BorderRadius.only(
topRight: borderRadius.topRight,
bottomRight: borderRadius.bottomRight,
),
);
}
final minInteractiveSize = Size(
dayPeriodSize.width,
math.max(dayPeriodSize.height, kMinInteractiveDimension),
);
final Widget amButton = _AmPmButton(
selected: amSelected,
onPressed: () => _setAm(context),
label: materialLocalizations.anteMeridiemAbbreviation,
padding: EdgeInsets.symmetric(
vertical: (minInteractiveSize.height - dayPeriodSize.height) / 2,
),
shape: amShape,
);
final Widget pmButton = _AmPmButton(
selected: pmSelected,
onPressed: () => _setPm(context),
label: materialLocalizations.postMeridiemAbbreviation,
padding: EdgeInsets.symmetric(
vertical: (minInteractiveSize.height - dayPeriodSize.height) / 2,
),
shape: pmShape,
);
return _DayPeriodInputPadding(
minSize: minInteractiveSize,
orientation: orientation,
child: SizedBox(
height: minInteractiveSize.height,
child: Row(
children: <Widget>[
Expanded(child: amButton),
Expanded(child: pmButton),
],
),
),
);
}
}
}
class _AmPmButton extends StatelessWidget {
const _AmPmButton({
required this.onPressed,
required this.selected,
required this.label,
required this.padding,
required this.shape,
});
final bool selected;
final VoidCallback onPressed;
final String label;
final EdgeInsets padding;
final OutlinedBorder shape;
@override
Widget build(BuildContext context) {
final states = <WidgetState>{if (selected) WidgetState.selected};
final TimePickerThemeData timePickerTheme = _TimePickerModel.themeOf(context);
final _TimePickerDefaults defaultTheme = _TimePickerModel.defaultThemeOf(context);
final Color resolvedBackgroundColor = WidgetStateProperty.resolveAs<Color>(
timePickerTheme.dayPeriodColor ?? defaultTheme.dayPeriodColor,
states,
);
final Color resolvedTextColor = WidgetStateProperty.resolveAs<Color>(
timePickerTheme.dayPeriodTextColor ?? defaultTheme.dayPeriodTextColor,
states,
);
final TextStyle? resolvedTextStyle = WidgetStateProperty.resolveAs<TextStyle?>(
timePickerTheme.dayPeriodTextStyle ?? defaultTheme.dayPeriodTextStyle,
states,
)?.copyWith(color: resolvedTextColor);
final TextScaler buttonTextScaler = MediaQuery.textScalerOf(context).clamp(maxScaleFactor: 2.0);
return Semantics(
// Platform-specific semantics vary slightly here on iOS.
selected: defaultTargetPlatform == TargetPlatform.iOS ? selected : null,
checked: defaultTargetPlatform == TargetPlatform.iOS ? null : selected,
inMutuallyExclusiveGroup: true,
button: true,
child: Padding(
padding: padding,
child: Material(
clipBehavior: Clip.antiAlias,
color: resolvedBackgroundColor,
shape: shape,
child: InkWell(
onTap: onPressed,
child: Center(
child: Text(label, style: resolvedTextStyle, textScaler: buttonTextScaler),
),
),
),
),
);
}
}
/// A widget to pad the area around the [_DayPeriodControl]'s inner [Material].
class _DayPeriodInputPadding extends SingleChildRenderObjectWidget {
const _DayPeriodInputPadding({
required Widget super.child,
required this.minSize,
required this.orientation,
});
final Size minSize;
final Orientation orientation;
@override
RenderObject createRenderObject(BuildContext context) {
return _RenderInputPadding(minSize, orientation);
}
@override
void updateRenderObject(BuildContext context, covariant _RenderInputPadding renderObject) {
renderObject
..minSize = minSize
..orientation = orientation;
}
}
class _RenderInputPadding extends RenderShiftedBox {
_RenderInputPadding(this._minSize, this._orientation, [RenderBox? child]) : super(child);
Size get minSize => _minSize;
Size _minSize;
set minSize(Size value) {
if (_minSize == value) {
return;
}
_minSize = value;
markNeedsLayout();
}
Orientation get orientation => _orientation;
Orientation _orientation;
set orientation(Orientation value) {
if (_orientation == value) {
return;
}
_orientation = value;
markNeedsLayout();
}
@override
double computeMinIntrinsicWidth(double height) {
if (child != null) {
return math.max(child!.getMinIntrinsicWidth(height), minSize.width);
}
return 0;
}
@override
double computeMinIntrinsicHeight(double width) {
if (child != null) {
return math.max(child!.getMinIntrinsicHeight(width), minSize.height);
}
return 0;
}
@override
double computeMaxIntrinsicWidth(double height) {
if (child != null) {
return math.max(child!.getMaxIntrinsicWidth(height), minSize.width);
}
return 0;
}
@override
double computeMaxIntrinsicHeight(double width) {
if (child != null) {
return math.max(child!.getMaxIntrinsicHeight(width), minSize.height);
}
return 0;
}
Size _computeSize({required BoxConstraints constraints, required ChildLayouter layoutChild}) {
if (child != null) {
final Size childSize = layoutChild(child!, constraints);
final double width = math.max(childSize.width, minSize.width);
final double height = math.max(childSize.height, minSize.height);
return constraints.constrain(Size(width, height));
}
return Size.zero;
}
@override
Size computeDryLayout(BoxConstraints constraints) {
return _computeSize(constraints: constraints, layoutChild: ChildLayoutHelper.dryLayoutChild);
}
@override
double? computeDryBaseline(covariant BoxConstraints constraints, TextBaseline baseline) {
final RenderBox? child = this.child;
if (child == null) {
return null;
}
final double? result = child.getDryBaseline(constraints, baseline);
if (result == null) {
return null;
}
// Calculate the size and child offset using the same logic as performLayout
final Size drySize = getDryLayout(constraints);
final Size childSize = child.getDryLayout(constraints);
final Offset childOffset = Alignment.center.alongOffset(drySize - childSize as Offset);
return result + childOffset.dy;
}
@override
void performLayout() {
size = _computeSize(constraints: constraints, layoutChild: ChildLayoutHelper.layoutChild);
if (child != null) {
final childParentData = child!.parentData! as BoxParentData;
childParentData.offset = Alignment.center.alongOffset(size - child!.size as Offset);
}
}
@override
bool hitTest(BoxHitTestResult result, {required Offset position}) {
if (super.hitTest(result, position: position)) {
return true;
}
if (position.dx < 0 ||
position.dx > math.max(child!.size.width, minSize.width) ||
position.dy < 0 ||
position.dy > math.max(child!.size.height, minSize.height)) {
return false;
}
Offset newPosition = child!.size.center(Offset.zero);
newPosition += switch (orientation) {
Orientation.portrait when position.dy > newPosition.dy => const Offset(0, 1),
Orientation.landscape when position.dx > newPosition.dx => const Offset(1, 0),
Orientation.portrait => const Offset(0, -1),
Orientation.landscape => const Offset(-1, 0),
};
return result.addWithRawTransform(
transform: MatrixUtils.forceToPoint(newPosition),
position: newPosition,
hitTest: (BoxHitTestResult result, Offset position) {
assert(position == newPosition);
return child!.hitTest(result, position: newPosition);
},
);
}
}
class _TappableLabel {
_TappableLabel({
required this.value,
required this.inner,
required this.painter,
required this.onTap,
});
/// The value this label is displaying.
final int value;
/// This value is part of the "inner" ring of values on the dial, used for 24
/// hour input.
final bool inner;
/// Paints the text of the label.
final TextPainter painter;
/// Called when a tap gesture is detected on the label.
final VoidCallback onTap;
}
class _DialPainter extends CustomPainter {
_DialPainter({
required this.primaryLabels,
required this.selectedLabels,
required this.backgroundColor,
required this.handColor,
required this.handWidth,
required this.dotColor,