Created
March 8, 2021 12:56
-
-
Save hoshi-takanori/ac50dfe518bf2795188e99c6757182af to your computer and use it in GitHub Desktop.
CustomDialog with TextField in Jetpack Compose 1.0.0-beta01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.foundation.background | |
import androidx.compose.foundation.clickable | |
import androidx.compose.foundation.interaction.MutableInteractionSource | |
import androidx.compose.foundation.layout.Arrangement | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.foundation.layout.Spacer | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.height | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.layout.width | |
import androidx.compose.foundation.shape.RoundedCornerShape | |
import androidx.compose.material.Button | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.material.Scaffold | |
import androidx.compose.material.Text | |
import androidx.compose.material.TextField | |
import androidx.compose.material.TopAppBar | |
import androidx.compose.material.contentColorFor | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.MutableState | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.remember | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.draw.clip | |
import androidx.compose.ui.unit.dp | |
import androidx.compose.ui.unit.sp | |
@Composable | |
fun ComposeEditModal() { | |
val message = remember { mutableStateOf("Edit Me") } | |
val openDialog = remember { mutableStateOf(false) } | |
val editMessage = remember { mutableStateOf("") } | |
Box { | |
Scaffold( | |
topBar = { | |
TopAppBar( | |
title = { | |
Text("ComposeEditModal") | |
} | |
) | |
} | |
) { | |
Column( | |
modifier = Modifier.fillMaxSize(), | |
verticalArrangement = Arrangement.Center, | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
Text( | |
text = message.value, | |
fontSize = 24.sp | |
) | |
Spacer(modifier = Modifier.height(32.dp)) | |
Button( | |
onClick = { | |
editMessage.value = message.value | |
openDialog.value = true | |
} | |
) { | |
Text("Open Dialog") | |
} | |
} | |
} | |
if (openDialog.value) { | |
Box( | |
modifier = Modifier | |
.fillMaxSize() | |
.background( | |
color = contentColorFor(MaterialTheme.colors.background) | |
.copy(alpha = 0.6f) | |
) | |
.clickable( | |
interactionSource = remember { MutableInteractionSource() }, | |
indication = null, | |
onClick = { | |
openDialog.value = false | |
} | |
), | |
contentAlignment = Alignment.Center | |
) { | |
CustomDialog(message, openDialog, editMessage) | |
} | |
} | |
} | |
} | |
@Composable | |
fun CustomDialog( | |
message: MutableState<String>, | |
openDialog: MutableState<Boolean>, | |
editMessage: MutableState<String> | |
) { | |
Column( | |
modifier = Modifier | |
.clip(RoundedCornerShape(4.dp)) | |
.background(MaterialTheme.colors.background) | |
.padding(8.dp), | |
) { | |
Column( | |
modifier = Modifier.padding(16.dp), | |
) { | |
Text(text = "Input Message") | |
Spacer(modifier = Modifier.height(8.dp)) | |
TextField( | |
value = editMessage.value, | |
onValueChange = { editMessage.value = it }, | |
singleLine = true | |
) | |
} | |
Spacer(modifier = Modifier.height(8.dp)) | |
Row( | |
modifier = Modifier.align(Alignment.End) | |
) { | |
Button( | |
onClick = { | |
openDialog.value = false | |
} | |
) { | |
Text("Cancel") | |
} | |
Spacer(modifier = Modifier.width(8.dp)) | |
Button( | |
onClick = { | |
message.value = editMessage.value | |
openDialog.value = false | |
} | |
) { | |
Text("OK") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment