OutlinedTextField in DropdownMenu causes crash

59 views Asked by At

After long press on OutLinedTextFIeld app is crashing.

The main need of this code is to be able to search for an item from the menu by typing something into the OutlinedTextField

I tried to change location of OutlinedTextField, but it didn't help. From another hand I tried block the possibility to long press on it. In Terminal after the crash, I had information that something cannot be null.

@Composable
fun EatList() {

    var sequenceList: MutableList<String> = mutableListOf("")
    val foodList: MutableList<String> = mutableListOf(
        "bazylia suszona ", //0
        "bułka tarta ",
        "cukier brązowy ",
        "cukier biały ",
        "cukier puder ",
        "cukier waniliowy ", //5
        "cynamon ",
        "drożdże suche ",
        "herbata ",
        "kakao ",
        "kasza jęczmienna ",//10
        "kasza manna ",
        "kasza mielona ",
        "kasza gryczana ",
        "koncentrat pomidorowy ",
        "mak ",//15
        "margaryna ",
        "majonez ",
        "masło ",
        "masło orzechowe ",
        "mąka kukurydziana ",//20
        "mąka orkiszowa 1850 ",
        "mąka pszenna ",
        "mąka pszenna pełna ",
        "mąka tortowa ",
        "mąka ziemniaczana ",//25
        "mąka żytnia ",
        "miód ",
        "mleko w proszku ",
        "ocet ",
        "olej roślinny ",//30
        "oliwa z oliwek ",
        "owies ",
        "papryka przyprawa ",
        "pietruszka przyprawa ",
        "pietruszka świeża ",//35
        "pieprz mielony ",
        "płatki kukurydziane ",
        "płatki owsiane ",
        "proszek do pieczenia ",
        "pszenica ",//40
        "skrobia kukurydziana ",
        "soda oczyszczona ",
        "sól ",
        "smalec ",
        "śmietana 12% ",//45
        "śmietana 18% ",
        "śmietana 36% ",
        "olej kokosowy ",
        "twaróg chudy ",
        "twaróg półtłusty ",//50
        "twaróg tłusty ",
        "żelatyna ",
        "żurawina ",
        //  "mleko "
    )
    var textt by remember { mutableStateOf("") }
    var currentValue by remember { mutableStateOf("Choose product") } //displays a particular string depending on what the user choose
    val expanded = remember { mutableStateOf(false) } //is responsible for opening and closing Menu

    val n = textt.length

    var i=0
if(n==0){
    sequenceList=foodList
}
    else{
        sequenceList.removeAt(0)
    while(i<53){
        if (textt == foodList[i].take(n)) {
            sequenceList.add(foodList[i])
}
        i++
    }

}

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 140.dp)
            .padding(start = 150.dp)
        ,
        horizontalAlignment = Alignment.Start
    )
    {

        Row(modifier = Modifier
            .clip(RoundedCornerShape(10.dp))
            .clickable { expanded.value = !expanded.value }
            .background(color = Color.White, shape = RoundedCornerShape(10.dp))
            .border(
                width = 1.5.dp,
                color = Color.Black,
                shape = RoundedCornerShape(10.dp)
            )
            .padding(8.dp))
        {
            Text(
                text = currentValue,
                fontStyle = FontStyle.Italic,
                fontFamily = FontFamily.Serif,
            )
            Icon(imageVector = Icons.Filled.ArrowDropDown, contentDescription = null)
        }

        DropdownMenu(
            expanded = expanded.value,
            onDismissRequest = {
                expanded.value = false
            },
            modifier = Modifier
                .height(220.dp)
                .border(
                    width = 1.5.dp,
                    color = Color.Black,
                    shape = RoundedCornerShape(4.dp)
                )
                .width(200.dp)
        ) {

            val focusManager = LocalFocusManager.current//remove cursor when user click Done
            val maxLng2=31

            OutlinedTextField(
                modifier = Modifier
                    .border(
                        width = 1.dp,
                        color = Color.Black,
                        shape = RoundedCornerShape(4.dp)
                    )
                    .width(200.dp),
                value = textt,
                onValueChange = { newTextt ->
                    if(newTextt.length<maxLng2) {
                        textt = newTextt.toLowerCase(Locale.current)
                    }
                    textt=textt


                },
                placeholder = { Text(text = "Search") },
                keyboardOptions = KeyboardOptions(autoCorrect = true,imeAction = ImeAction.Done),
                keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
                maxLines = 1
            )


            sequenceList.forEach {

                DropdownMenuItem(
                    onClick = {
                        currentValue = it
                        expanded.value = false
                    },
                    modifier = Modifier.border(
                        width = 0.5.dp,
                        color = Color.Black,
                    )
                ) {
                    Text(
                        text = it,
                        fontStyle = FontStyle.Italic,
                        fontFamily = FontFamily.Serif,
                        fontSize = 18.sp
                    )

                }

            }
        }


    }
}
0

There are 0 answers