Skip to content

Instantly share code, notes, and snippets.

@manojbhadane
Created April 21, 2022 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save manojbhadane/7bd7927e2f55887bf133a8dd4d3de032 to your computer and use it in GitHub Desktop.
Save manojbhadane/7bd7927e2f55887bf133a8dd4d3de032 to your computer and use it in GitHub Desktop.
@Composable
fun CustomDialog(value: String, setShowDialog: (Boolean) -> Unit, setValue: (String) -> Unit) {
val txtFieldError = remember { mutableStateOf("") }
val txtField = remember { mutableStateOf(value) }
Dialog(onDismissRequest = { setShowDialog(false) }) {
Surface(
shape = RoundedCornerShape(16.dp),
color = Color.White
) {
Box(
contentAlignment = Alignment.Center
) {
Column(modifier = Modifier.padding(20.dp)) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "Set value",
style = TextStyle(
fontSize = 24.sp,
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Bold
)
)
Icon(
imageVector = Icons.Filled.Cancel,
contentDescription = "",
tint = colorResource(android.R.color.darker_gray),
modifier = Modifier
.width(30.dp)
.height(30.dp)
.clickable { setShowDialog(false) }
)
}
Spacer(modifier = Modifier.height(20.dp))
TextField(
modifier = Modifier
.fillMaxWidth()
.border(
BorderStroke(
width = 2.dp,
color = colorResource(id = if (txtFieldError.value.isEmpty()) android.R.color.holo_green_light else android.R.color.holo_red_dark)
),
shape = RoundedCornerShape(50)
),
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent
),
leadingIcon = {
Icon(
imageVector = Icons.Filled.Money,
contentDescription = "",
tint = colorResource(android.R.color.holo_green_light),
modifier = Modifier
.width(20.dp)
.height(20.dp)
)
},
placeholder = { Text(text = "Enter value") },
value = txtField.value,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
onValueChange = {
txtField.value = it.take(10)
})
Spacer(modifier = Modifier.height(20.dp))
Box(modifier = Modifier.padding(40.dp, 0.dp, 40.dp, 0.dp)) {
Button(
onClick = {
if (txtField.value.isEmpty()) {
txtFieldError.value = "Field can not be empty"
return@Button
}
setValue(txtField.value)
setShowDialog(false)
},
shape = RoundedCornerShape(50.dp),
modifier = Modifier
.fillMaxWidth()
.height(50.dp)
) {
Text(text = "Done")
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment