Skip to content

Instantly share code, notes, and snippets.

@SEAbdulbasit
Created February 10, 2024 16:31
Show Gist options
  • Save SEAbdulbasit/6074ccbd7da827dd008b355eb7e76581 to your computer and use it in GitHub Desktop.
Save SEAbdulbasit/6074ccbd7da827dd008b355eb7e76581 to your computer and use it in GitHub Desktop.
@Composable
fun AgeCalculator() {
var startDate by remember { mutableStateOf(LocalDate.now()) }
var endDate by remember { mutableStateOf(LocalDate.now()) }
var showDialogStartDate by remember { mutableStateOf(false) }
var showDialogEndDate by remember { mutableStateOf(false) }
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
OutlinedTextField(
value = startDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")),
onValueChange = {},
label = { Text("Date of birth") },
modifier = Modifier.clickable { showDialogStartDate = true },
readOnly = true,
)
if (showDialogStartDate) {
DatePickerDialog(startDate) { newDate ->
startDate = newDate
showDialogStartDate = false
}
}
Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField(
value = endDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")),
onValueChange = {},
label = { Text("End date") },
modifier = Modifier.clickable { showDialogEndDate = true },
readOnly = true,
)
if (showDialogEndDate) {
DatePickerDialog(endDate) { newDate ->
endDate = newDate
showDialogEndDate = false
}
}
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = {
// val daysBetween = Date..between(startDate, endDate)
// Use an appropriate method to show the difference in days
// For example, if you have a SnackbarHostState, you could use:
// snackbarHostState.showSnackbar("Duration is $daysBetween days")
// println("Duration is $daysBetween days")
}) {
Text("Calculate age")
}
}
}
@Composable
fun DatePickerDialog(initialDate: LocalDate, onDateSelected: (LocalDate) -> Unit) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment