When I launch/close the modal bottom sheet it causes my lazy Column list to reload, why?
I think var showBottomSheet by remember { mutableStateOf(false) }
is causing a problem.
In this code showBottomSheet = true
this causes refresh only once when I click on the row.
If I set it to showBottomSheet = !showBottomSheet
, then every time I click it recomposes the lazy column.
What else can I do to avoid this recomposition?
fun TrainListContent() {
val viewModel = CCViewModel()
val sheetState = rememberStandardBottomSheetState()
var showBottomSheet by remember { mutableStateOf(false) }
var clickedTrainNumber by remember { mutableStateOf("") }
val trainList = viewModel.filteredTrains.collectAsLazyPagingItems()
LazyColumn(modifier = Modifier.background(Color.LightGray))
{
if (trainList.loadState.refresh is LoadState.Loading ||
trainList.loadState.refresh is LoadState.Error
) {
item {
HandleLoadState(
loadState = trainList.loadState.refresh,
onRetry = { trainList.retry() }
)
}
}
items(
count = trainList.itemCount,
) { index ->
val train = trainList[index]
if (train != null) {
TrainListRow(
train = train,
onClick = {
clickedTrainNumber = it
showBottomSheet = true
}
)
}
Spacer(
modifier = Modifier
.height(5.dp)
)
}
if (trainList.loadState.append == LoadState.Loading) {
item {
CircularProgressIndicator(
modifier = Modifier
.fillMaxWidth()
.wrapContentWidth(Alignment.CenterHorizontally)
)
}
}
}
if (showBottomSheet) {
ModalBottomSheet(
onDismissRequest = { showBottomSheet = false },
sheetState = sheetState,
modifier = Modifier.height(700.dp)
) {
viewModel.setTrainNumber(clickedTrainNumber)
val trainSchStat = viewModel.trainSchPaged.collectAsLazyPagingItems()
Sample(trainList = trainSchStat)
}
}
}