https://kotlinlang.org logo
#compose-android
Title
# compose-android
a

Atul Gupta

11/15/2023, 5:30 PM
I am trying to use standard bottom sheet(which is not modal) like the code shown in the thread inside a fragment and getting the crash as shown in the thread This happen when I launch the fragment
z

Zach Klippenstein (he/him) [MOD]

11/15/2023, 5:31 PM
Please keep long code snippets to the thread to keep the main channel readable, thanks!
👍 1
a

Atul Gupta

11/15/2023, 5:53 PM
Code:
Copy code
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?,
): View
{
return ComposeView(requireContext()).apply {
    setContent {
        val sheetState =
            rememberStandardBottomSheetState(initialValue = SheetValue.Expanded, skipHiddenState = false)
        LaunchedEffect(sheetState) {
            snapshotFlow { sheetState.currentValue }.collectLatest {
                if (it == SheetValue.Hidden)
                {
                    dismiss()
                }
            }
        }
        val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(sheetState)
        // Screen content
        BottomSheetScaffold(
            modifier = Modifier.width(LocalConfiguration.current.screenHeightDp.dp),
            scaffoldState = bottomSheetScaffoldState,
            sheetPeekHeight = (LocalConfiguration.current.screenHeightDp / 2).dp,
            sheetContent = {
                val itemList by viewModel.itemList.collectAsState()
                CustomList(list = itemList, userName = viewModel.userName)
            },
        ) {
            // no content on bg
        }
    }
}
}
Crash stacktrace
Copy code
java.lang.IllegalStateException: The offset was read before being initialized. Did you access the offset in a phase before layout, like effects or composition?
                                                                                                    	at androidx.compose.material3.SwipeableV2State.requireOffset(SwipeableV2.kt:238)
                                                                                                    	at androidx.compose.material3.SheetState.requireOffset(SheetDefaults.kt:126)
                                                                                                    	at androidx.compose.material3.BottomSheetScaffoldKt$BottomSheetScaffold$3$1.invoke(BottomSheetScaffold.kt:123)
                                                                                                    	at androidx.compose.material3.BottomSheetScaffoldKt$BottomSheetScaffold$3$1.invoke(BottomSheetScaffold.kt:123)
                                                                                                    	at androidx.compose.material3.BottomSheetScaffoldKt$BottomSheetScaffoldLayout$1$1.invoke-0kLqBqw(BottomSheetScaffold.kt:337)
                                                                                                    	at androidx.compose.material3.BottomSheetScaffoldKt$BottomSheetScaffoldLayout$1$1.invoke(BottomSheetScaffold.kt:329)
                                                                                                    	at androidx.compose.ui.layout.LayoutNodeSubcompositionsState$createMeasurePolicy$1.measure-3p2s80s(SubcomposeLayout.kt:866)
                                                                                                    	at androidx.compose.ui.node.InnerNodeCoordinator.measure-BRTryo0(InnerNodeCoordinator.kt:126)
                                                                                                    	at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasure$2.invoke(LayoutNodeLayoutDelegate.kt:1499)
                                                                                                    	at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasure$2.invoke(LayoutNodeLayoutDelegate.kt:1495)
                                                                                                    	at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2299)}
This crash is only happening when
CustomList(list = itemList, userName = viewModel.userName)
doesn’t have any height modifier to it. Adding
fillMaxHeight
removes the crash
6 Views