Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram: / ky.emrah
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Can I use observerAsState() on a MutableLiveData?
I have ran into an issue while building a small movie library application. My structure is as follows: I have a MovieSearchScreen function which calls a SearchBar function as well as a MovieSearchResults function. In the Searchbar function, I have a TextField, a leadingIcon and a trailingIcon which has a clickable modifier which calls my updateMovieResource function which is in my viewModel:
@Composable
fun Create(navController: NavHostController, viewModel: MovieViewModel) {
Column(
modifier = Modifier
.fillMaxSize()
.background(color = Color.DarkGray)
) {
SearchBar(viewModel = viewModel)
MovieSearchResults(navController = navController)
}
}
@Composable
fun Create(navController: NavHostController, viewModel: MovieViewModel) {
Column(
modifier = Modifier
.fillMaxSize()
.background(color = Color.DarkGray)
) {
SearchBar(viewModel = viewModel)
MovieSearchResults(navController = navController)
}
}
@Composable
private fun SearchBar(viewModel: MovieViewModel) {
var userInput: String by remember {
mutableStateOf("")
}
TextField(
value = userInput,
onValueChange = {
userInput = it
},
modifier = Modifier
.fillMaxWidth(),
colors = TextFieldDefaults.colors(
focusedContainerColor = Color.Gray,
unfocusedContainerColor = Color.Gray,
focusedTextColor = Color.White,
unfocusedTextColor = Color.White
),
placeholder = {
Text(
text = stringResource(id = R.string.search_bar),
color = Color.White
)
},
leadingIcon = {
Icon(
imageVector = Icons.Rounded.Clear,
contentDescription = "Clear",
modifier = Modifier
.clickable {
userInput = ""
},
tint = Color.White
)
},
trailingIcon = {
Icon(
imageVector = Icons.Rounded.Search,
contentDescription = "Search",
tint = Color.White,
modifier = Modifier.clickable {
if (userInput.isNotBlank()) {
viewModel.updateMovieResource(userInput)
}
}
)
}
)
}
@Composable
private fun SearchBar(viewModel: MovieViewModel) {
var userInput: String by remember {
mutableStateOf("")
}
TextField(
value = userInput,
onValueChange = {
userInput = it
},
modifier = Modifier
.fillMaxWidth(),
colors = TextFieldDefaults.colors(
focusedContainerColor = Color.Gray,
unfocusedContainerColor = Color.Gray,
focusedTextColor = Color.White,
unfocusedTextColor = Color.White
),Source of the question:
https://stackoverflow.com/questions/7...
Question and source license information:
https://meta.stackexchange.com/help/l...
https://stackoverflow.com/
Информация по комментариям в разработке