Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Jetpack Compose how to clear all checkboxes in lazycolumn compose using a button in a different composable

$
0
0

I am looking for a solution on how to clear all checkboxes in lazycolumn compose using a button in a different composable.

So what I am trying to do it that one can check one or multiple checkboxes in the ParentItemComposable that are under the ObjectComposable. Then be able to clear all selections using the ClearAllButton composable

This is the code I have tried below

@Composablefun ParentItemComposable(    reload: Boolean,    viewModel: EtfFilterViewModel,    modifier: Modifier = Modifier) {    val itemsList by remember {        mutableStateOf(etfFilterViewModel.getEParentList)    }    Column() {        LazyColumn(            Modifier                .background(color = PaleGrey50)                .padding(bottom = 80.dp)        ) {            items(itemsList.sortedBy { it.title }) { parentItem ->                val title: String = when (parentItem.title) {"geography" -> {"Geography"                    }"assetClass" -> {"Asset Class"                    }"focusStyle" -> {"Focus / Style"                    }                    else -> {"Other"                    }                }                Box(                    modifier = Modifier                        .padding(0.dp, 0.dp, 0.dp, 5.dp)                        .fillMaxWidth()                        .background(color = White)                ) {                    Text(                        text = title,                        modifier = Modifier.padding(5.dp),                        style = MaterialTheme.typography.h6                    )                }                Column(modifier = Modifier.padding(start = 16.dp)) {                    parentItem.filterObjectList.sortBy {                        it.name                    }                    parentItem.filterObjectList.forEach { filterObject ->                        ObjectComposable(                            filterObject,                            viewModel,                            itemsList.indexOf(parentItem),                            parentItem.filterObjectList.indexOf(filterObject),                        )                    }                    parentItem.filterObjectList.filter { i -> i.isChecked }                }            }        }    }}@Composableprivate fun ObjectComposable(    filterObject: FilterObject,    viewModel: ViewModel,…..) {    Row(        verticalAlignment = Alignment.CenterVertically    ) {        val checkedState = remember { mutableStateOf(false) }        val context = LocalContext.current        Checkbox(            checked = checkedState.value,            onCheckedChange = {                checkedState.value = it….                viewModel.updateFilteredListOfStrings(filterObject.name)            },            enabled = true,            modifier = Modifier.padding(end = 8.dp)        )        Text(text = efilterObjectname)    }}@Composablefun ClearAllButton(    onButtonClick: () -> Unit,    modifier: Modifier = Modifier,) {    Row(        verticalAlignment = Alignment.Bottom    ) {        Button(            onClick = { onButtonClick() },            modifier = Modifier                .wrapContentHeight()                .fillMaxWidth()                .padding(16.dp),            contentPadding = PaddingValues(16.dp),            colors = ButtonDefaults.buttonColors(                backgroundColor = PaleGrey50,                contentColor = Secondary            ),            border = BorderStroke(1.dp, color = Secondary),            shape = RoundedCornerShape(4.dp),        ) {            Text(                text = stringResource(R.string.clear_all),                fontSize = FontSize.Small,                fontWeight = FontWeight.Bold,            )        }    }}

What I am expecting is that ClearAllButton composable will clear all Checkboxes checked in ParentItemComposable lazycolum


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>