I have a screen with a Textfield
as a Search bar which is autofocused when the screen is first displayed. The problem is, after screen rotation
or system theme change
(Dark mode/light mode), the cursor
moves at the beginning of TextField even if the value of TextField is not empty and the keyboard dismisses itself. Please help, I've been searching for two days. Some code samples:
Compose version: 1.3.0 Material 3: 1.0.0
SearchBookScreen.kt
val myViewModel = SearchBookViewModel()
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SearchBookScreen(
modifier: Modifier = Modifier,
navController: NavController,
viewModel: SearchBookViewModel = myViewModel,
filters: Map<SearchFilterType, FilterOptions> = FILTERS
) {
val focusRequester = remember { FocusRequester() }
val searchInputValue = viewModel.searchInputValue.value
val filtersState = viewModel.filtersState
LaunchedEffect(Unit) { this.coroutineContext.job.invokeOnCompletion { focusRequester.requestFocus() } }
Scaffold(
topBar = {
Column {
TextField(
singleLine = true,
placeholder = {
Text(text = stringResource(id = R.string.search_bar))
},
value = searchInputValue.text,
modifier = Modifier
.fillMaxWidth()
.focusRequester(focusRequester)
,
onValueChange = {
viewModel.onEvent(SearchBookEvent.EnteredSearchValue(it))
},
leadingIcon = {
IconButton(onClick = {
viewModel.onEvent(SearchBookEvent.ClearSearchInput)
navController.navigateUp()
}
) {
Icon(
imageVector = Icons.Default.ArrowBack,
contentDescription = "Go back"
)
}
},
colors = TextFieldDefaults.textFieldColors(
unfocusedIndicatorColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent
),
trailingIcon = {
IconButton(onClick = { viewModel.onEvent(SearchBookEvent.ClearSearchInput) }) {
Icon(imageVector = Icons.Default.Clear, contentDescription = "clear")
}
},
)
SearchBookViewModel.kt
class SearchBookViewModel: ViewModel() {
private val _searchInputValue = mutableStateOf(TextFieldValue("", selection = TextRange.Zero))
val searchInputValue: State<TextFieldValue> = _searchInputValue
fun onEvent(event: SearchBookEvent) {
when (event) {
is SearchBookEvent.EnteredSearchValue -> {
_searchInputValue.value = searchInputValue.value.copy(text = event.value, selection = TextRange(event.value.length))
}
}
}