Skip to content

Commit b0cb7b9

Browse files
committed
add showPlatformDatePicker global method
1 parent 296a2d9 commit b0cb7b9

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

lib/src/platform/platform_time_picker.dart

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,98 @@ Future<TimeOfDay?> showPlatformTimePicker({
7777
);
7878
}
7979
}
80+
81+
/// Shows a platform specific date picker with the given [initialDateTime] with a possible range starting with [firstDate] and ending with [lastDate].
82+
///
83+
/// On iOS and macOS a CupertinoDatePicker is used. The [builder], [initialEntryMode], [helpText], [useRootNavigator] and [rootSettings] parameters are ignored there.
84+
/// On other platforms the Material [showTimePicker] method is called with the given parameters.
85+
Future<DateTime?> showPlatformDatePicker({
86+
required BuildContext context,
87+
required DateTime initialDate,
88+
required DateTime firstDate,
89+
required DateTime lastDate,
90+
DateTime? currentDate,
91+
DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar,
92+
SelectableDayPredicate? selectableDayPredicate,
93+
String? helpText,
94+
String? cancelText,
95+
String? confirmText,
96+
Locale? locale,
97+
bool useRootNavigator = true,
98+
RouteSettings? routeSettings,
99+
TextDirection? textDirection,
100+
TransitionBuilder? builder,
101+
DatePickerMode initialDatePickerMode = DatePickerMode.day,
102+
String? errorFormatText,
103+
String? errorInvalidText,
104+
String? fieldHintText,
105+
String? fieldLabelText,
106+
}) async {
107+
if (Platform.isIOS || Platform.isMacOS) {
108+
DateTime? pickedTime;
109+
final result = await showCupertinoModalPopup<bool>(
110+
context: context,
111+
builder: (context) => SizedBox(
112+
width: MediaQuery.of(context).size.width,
113+
height: MediaQuery.of(context).size.height * 0.4,
114+
child: Container(
115+
color: CupertinoTheme.of(context).barBackgroundColor,
116+
child: Column(
117+
children: [
118+
Row(
119+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
120+
children: [
121+
CupertinoButton(
122+
child: (cancelText != null)
123+
? Text(cancelText)
124+
: Icon(CupertinoIcons.clear_circled),
125+
onPressed: () => Navigator.of(context).pop(false)),
126+
CupertinoButton(
127+
child: (confirmText != null)
128+
? Text(confirmText)
129+
: Icon(CupertinoIcons.check_mark_circled),
130+
onPressed: () => Navigator.of(context).pop(true)),
131+
],
132+
),
133+
Expanded(
134+
child: CupertinoDatePicker(
135+
mode: CupertinoDatePickerMode.date,
136+
onDateTimeChanged: (value) => pickedTime = value,
137+
use24hFormat: MediaQuery.of(context).alwaysUse24HourFormat,
138+
initialDateTime: initialDate,
139+
),
140+
),
141+
],
142+
),
143+
),
144+
),
145+
);
146+
if (result == true && pickedTime != null) {
147+
return pickedTime;
148+
}
149+
return null;
150+
} else {
151+
return showDatePicker(
152+
context: context,
153+
initialDate: initialDate,
154+
firstDate: firstDate,
155+
lastDate: lastDate,
156+
currentDate: currentDate,
157+
initialEntryMode: initialEntryMode,
158+
selectableDayPredicate: selectableDayPredicate,
159+
helpText: helpText,
160+
cancelText: cancelText,
161+
confirmText: confirmText,
162+
locale: locale,
163+
useRootNavigator: useRootNavigator,
164+
routeSettings: routeSettings,
165+
textDirection: textDirection,
166+
builder: builder,
167+
initialDatePickerMode: initialDatePickerMode,
168+
errorFormatText: errorFormatText,
169+
errorInvalidText: errorInvalidText,
170+
fieldHintText: fieldHintText,
171+
fieldLabelText: fieldLabelText,
172+
);
173+
}
174+
}

0 commit comments

Comments
 (0)