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