0

When I press button with onSendClicked is not adding text from textfield. I don't know where not catching text. I guess is somewhere mistake with viewmodel, becouse viewmodel don't get new value.

fun AddBar(
    onSendClicked: () -> Unit
){
    Row(Modifier.padding(5.dp)) {
        var title by remember {
            mutableStateOf("")
        }
        TextField(
            value = title,
            onValueChange = { title = it }
        )
        IconButton(onClick = {
            onSendClicked()})
        {
            Icon(imageVector = Icons.Filled.ArrowForward, contentDescription = "Send Icon")
        }
    }
}

@Composable
fun MainScreen(
    basketViewModel: BasketViewModel,
){
   AddBar(onSendClicked = { basketViewModel.addToBasket() })

 }

And viewModel

    val id: MutableState<Int> = mutableStateOf(0)
    val title: MutableState<String> = mutableStateOf("")

    fun addToBasket(){
        viewModelScope.launch(Dispatchers.IO) {
            val basket = Basket(
                title = title.value,
                isChecked = false
            )
            repository.addToBasket(basket = basket)
        }
    }

Help....

Matt
  • 33
  • 5

3 Answers3

1

You are never using the title state of the ViewModel. You are only updating the local title. For this to you have to stop using local title and just replace it with the title from viewModel. Something like that:

fun AddBar(
    title: MutableState<String>,
    onSendClicked: () -> Unit
){
    Row(Modifier.padding(5.dp)) {
        TextField(
            value = title.value,
            onValueChange = { title.value = it }
        )
        IconButton(onClick = {
            onSendClicked()})
        {
            Icon(imageVector = Icons.Filled.ArrowForward, contentDescription = "Send Icon")
        }
    }
}

@Composable
fun MainScreen(
    basketViewModel: BasketViewModel,
){
   AddBar(
      title = basketViewModel.title,
      onSendClicked = { basketViewModel.addToBasket() }
   )

 }
Jakoss
  • 4,647
  • 2
  • 26
  • 40
0

You define the title both in view model and your main screen. Use the one in your view model.

fun AddBar(
    title: String,
    onValueChange: (String) -> Unit,
    onSendClicked: () -> Unit
){
    Row(Modifier.padding(5.dp)) {
        TextField(
            value = title,
            onValueChange = { onValueChange(it) }
        )
 
        IconButton(
            onClick = { onSendClicked() }
        ) {
            Icon(
                imageVector = Icons.Filled.ArrowForward,
                contentDescription = "Send Icon"
            )
        }
    }
}

@Composable
fun MainScreen(
    basketViewModel: BasketViewModel,
){
    AddBar(
        title = basketViewModel.title,
        onValueChange = { basketViewModel.changeTitle(it) }
        onSendClicked = { basketViewModel.addToBasket() }
    )
}

class BasketViewModel : ViewModel() {
 
    var title by mutableStateOf("")
        private set

    fun changeTitle(value: String) {
        title = value
    }

    fun addToBasket(){
        viewModelScope.launch(Dispatchers.IO) {
            val basket = Basket(
                title = title.value,
                isChecked = false
            )
            repository.addToBasket(basket = basket)
        }
    }
}
mucahid-erdogan
  • 276
  • 2
  • 10
0

This is how you create a textfield in StackOverflow: val focusManager = LocalFocusManager.current EditNumberField(label = R.string.bill_amount, value = amountInput, onValueChange = { amountInput = it}, keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number, imeAction = ImeAction.Next),keyboardActions = KeyboardActions(onNext ={focusManager.moveFocus(FocusDirection.Down)})) EditNumberField(label = R.string.how_was_the_service, value = tipInput, onValueChange = { tipInput = it}, keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number, imeAction = ImeAction.Done),keyboardActions = KeyboardActions(onDone ={focusManager.clearFocus()}))

fun Topics(){LazyVerticalGrid(columns = GridCells.Fixed(2), verticalArrangement = Arrangement.spacedBy(dimensionResource(id = R.dimen.padding_small)),horizontalArrangement = Arrangement.spacedBy(dimensionResource(id = R.dimen.padding_small)) modifier = Modifier.padding(dimensionResource(id = R.dimen.padding_small))){items(DataSource.topics) {topic -> TopicCard(topic) }}}