fix: 带输入框的对话框请求焦点时闪退
This commit is contained in:
parent
170a4f8049
commit
6983063838
|
|
@ -6,7 +6,14 @@ import androidx.compose.foundation.layout.padding
|
|||
import androidx.compose.material.ExperimentalMaterialApi
|
||||
import androidx.compose.material.MaterialTheme
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.ExperimentalComposeUiApi
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.geometry.Size
|
||||
|
|
@ -119,19 +126,20 @@ fun EditTextPref(
|
|||
}
|
||||
|
||||
PromptDialog(
|
||||
dialogState = dialogState,
|
||||
modifier = Modifier.onGloballyPositioned {
|
||||
dialogSize = it.size.toSize()
|
||||
},
|
||||
value = textVal,
|
||||
onConfirm = {
|
||||
textVal = it
|
||||
onValueChange(it)
|
||||
edit()
|
||||
},
|
||||
onValueChange = {
|
||||
textVal = it
|
||||
onValueChange(it)
|
||||
modifier = Modifier.onGloballyPositioned {
|
||||
dialogSize = it.size.toSize()
|
||||
},
|
||||
dialogState = dialogState,
|
||||
initialValue = textVal,
|
||||
onValueChange = { newVal, _ ->
|
||||
textVal = newVal
|
||||
onValueChange(newVal)
|
||||
true
|
||||
},
|
||||
title = {
|
||||
if (dialogTitle != null) {
|
||||
|
|
|
|||
|
|
@ -215,24 +215,29 @@ fun ConfirmDialog(
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 带输入框的对话框
|
||||
*
|
||||
* @param onValueChange 输入框内容变化时的回调,返回true表示允许变化,false表示不允许变化
|
||||
*/
|
||||
@OptIn(ExperimentalComposeUiApi::class)
|
||||
@Composable
|
||||
fun PromptDialog(
|
||||
dialogState: DialogState = rememberDialogState(),
|
||||
modifier: Modifier = Modifier,
|
||||
value: String = "",
|
||||
onConfirm: (String) -> Unit,
|
||||
onValueChange: ((String) -> Unit)? = null,
|
||||
modifier: Modifier = Modifier,
|
||||
dialogState: DialogState = rememberDialogState(),
|
||||
initialValue: String = "",
|
||||
onValueChange: (newVal: String, oldVal: String) -> Boolean = { _, _ -> true },
|
||||
onCancel: (() -> Unit)? = null,
|
||||
confirmText: String = stringResource(id = R.string.button_sure_default),
|
||||
cancelText: String = stringResource(id = R.string.button_cancel),
|
||||
title: @Composable (DialogScope.() -> Unit) = {},
|
||||
content: @Composable (DialogScope.() -> Unit) = {},
|
||||
) {
|
||||
var textVal by remember { mutableStateOf(value) }
|
||||
var textVal by remember { mutableStateOf(initialValue) }
|
||||
// 每次显示时重置输入框内容
|
||||
LaunchedEffect(dialogState.show) {
|
||||
textVal = value
|
||||
textVal = initialValue
|
||||
}
|
||||
Dialog(
|
||||
modifier = modifier,
|
||||
|
|
@ -244,7 +249,7 @@ fun PromptDialog(
|
|||
DialogNegativeButton(text = cancelText, onClick = onCancel)
|
||||
},
|
||||
) {
|
||||
val focusRequester = FocusRequester.Default
|
||||
val focusRequester = remember { FocusRequester() }
|
||||
val softwareKeyboardController = LocalSoftwareKeyboardController.current
|
||||
Column(
|
||||
modifier = Modifier
|
||||
|
|
@ -260,8 +265,7 @@ fun PromptDialog(
|
|||
OutlinedTextField(
|
||||
value = textVal,
|
||||
onValueChange = {
|
||||
textVal = it
|
||||
onValueChange?.invoke(it)
|
||||
if (onValueChange.invoke(it, textVal)) textVal = it
|
||||
},
|
||||
maxLines = 1,
|
||||
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
|
||||
|
|
@ -288,7 +292,6 @@ fun PromptDialog(
|
|||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalComposeUiApi::class)
|
||||
@Composable
|
||||
fun Dialog(
|
||||
modifier: Modifier = Modifier,
|
||||
|
|
|
|||
Loading…
Reference in New Issue