17
17
package com.example.compose.snippets.components
18
18
19
19
import androidx.compose.foundation.background
20
+ import androidx.compose.foundation.gestures.awaitEachGesture
21
+ import androidx.compose.foundation.gestures.awaitFirstDown
22
+ import androidx.compose.foundation.gestures.waitForUpOrCancellation
20
23
import androidx.compose.foundation.layout.Arrangement
21
24
import androidx.compose.foundation.layout.Box
22
25
import androidx.compose.foundation.layout.Column
@@ -49,12 +52,24 @@ import androidx.compose.runtime.setValue
49
52
import androidx.compose.ui.Alignment
50
53
import androidx.compose.ui.Modifier
51
54
import androidx.compose.ui.draw.shadow
55
+ import androidx.compose.ui.input.pointer.PointerEventPass
56
+ import androidx.compose.ui.input.pointer.pointerInput
57
+ import androidx.compose.ui.tooling.preview.Preview
52
58
import androidx.compose.ui.unit.dp
53
59
import androidx.compose.ui.window.Popup
60
+ import com.example.compose.snippets.touchinput.userinteractions.MyAppTheme
54
61
import java.text.SimpleDateFormat
55
62
import java.util.Date
56
63
import java.util.Locale
57
64
65
+ @Preview
66
+ @Composable
67
+ private fun DatePickerPreview () {
68
+ MyAppTheme {
69
+ DatePickerExamples ()
70
+ }
71
+ }
72
+
58
73
// [START android_compose_components_datepicker_examples]
59
74
// [START_EXCLUDE]
60
75
@Composable
@@ -77,6 +92,9 @@ fun DatePickerExamples() {
77
92
Text (" Docked date picker:" )
78
93
DatePickerDocked ()
79
94
95
+ Text (" Open modal picker on click" )
96
+ DatePickerFieldToModal ()
97
+
80
98
Text (" Modal date pickers:" )
81
99
Button (onClick = { showModal = true }) {
82
100
Text (" Show Modal Date Picker" )
@@ -259,6 +277,43 @@ fun DatePickerDocked() {
259
277
}
260
278
}
261
279
280
+ @Composable
281
+ fun DatePickerFieldToModal (modifier : Modifier = Modifier ) {
282
+ var selectedDate by remember { mutableStateOf<Long ?>(null ) }
283
+ var showModal by remember { mutableStateOf(false ) }
284
+
285
+ OutlinedTextField (
286
+ value = selectedDate?.let { convertMillisToDate(it) } ? : " " ,
287
+ onValueChange = { },
288
+ label = { Text (" DOB" ) },
289
+ placeholder = { Text (" MM/DD/YYYY" ) },
290
+ trailingIcon = {
291
+ Icon (Icons .Default .DateRange , contentDescription = " Select date" )
292
+ },
293
+ modifier = modifier
294
+ .fillMaxWidth()
295
+ .pointerInput(selectedDate) {
296
+ awaitEachGesture {
297
+ // Modifier.clickable doesn't work for text fields, so we use Modifier.pointerInput
298
+ // in the Initial pass to observe events before the text field consumes them
299
+ // in the Main pass.
300
+ awaitFirstDown(pass = PointerEventPass .Initial )
301
+ val upEvent = waitForUpOrCancellation(pass = PointerEventPass .Initial )
302
+ if (upEvent != null ) {
303
+ showModal = true
304
+ }
305
+ }
306
+ }
307
+ )
308
+
309
+ if (showModal) {
310
+ DatePickerModal (
311
+ onDateSelected = { selectedDate = it },
312
+ onDismiss = { showModal = false }
313
+ )
314
+ }
315
+ }
316
+
262
317
fun convertMillisToDate (millis : Long ): String {
263
318
val formatter = SimpleDateFormat (" MM/dd/yyyy" , Locale .getDefault())
264
319
return formatter.format(Date (millis))
0 commit comments