Trying to use ModalBottomSheetLayout: ``` val s...
# compose
u
Trying to use ModalBottomSheetLayout:
Copy code
val state = rememberModalBottomSheetState(ModalBottomSheetValue.HalfExpanded)
    val scope = rememberCoroutineScope()

    ModalBottomSheetLayout(
        modifier = Modifier.fillMaxSize(),
        sheetState = state,
        sheetContent = {
            Box(modifier = Modifier
                .height(120.dp)
                .background(color = Color.Red)) {
                Text(text = "Half expanded")
            }
        }) {
        //  Full screen content
    }
and keep getting:
Copy code
java.lang.IllegalArgumentException: The initial value must have an associated anchor.
What am I doing wrong? 🙏
c
Make sure your content has size https://stackoverflow.com/a/68624825
u
content is ConstraintLayout where its modifier set as fillMaxSize, isn’t that enough?
c
should be
u
Tried adding default height of 1.dp to it with not luck, tried also to wrap the content with box with min height of 1.dp, nothing works.
Copy code
val state = rememberModalBottomSheetState(ModalBottomSheetValue.HalfExpanded)
    val scope = rememberCoroutineScope()

    ModalBottomSheetLayout(
        modifier = Modifier.fillMaxSize(),
        sheetState = state,
        sheetContent = {
            Box(
                modifier = Modifier
                    .height(120.dp)
                    .defaultMinSize(minHeight = 1.dp)
                    .background(color = Color.Red)
            ) {
                Text(text = "Half expanded")
            }
        }) {
        //  Full screen content
        Box(
            modifier = Modifier
                .defaultMinSize(minHeight = 1.dp)
                .fillMaxSize()
        ) {
            Text(text = "Hello content")
        }
Cleanest and still crashes
c
is it crashing if you start from Hidden state?
u
Don’t know but I hidden is not option for my UX behavior
c
I understand. Just to try narrowing down the problem. I can see no issues with your code 🤷
u
Not crashing, but doesn’t show the content, even after removing .defaultMinSize(minHeight = 1.dp). Text (hello content) isn’t showing
I understand why text is not showing (black over black color) but the state is really shi**y workaround, will need to change state in some launched effects
Another weird thing:
Copy code
LaunchedEffect(key1 = state, block = {
        if (state.currentValue == ModalBottomSheetValue.Hidden) {
            state.animateTo(ModalBottomSheetValue.HalfExpanded)
        }
    })
crashes it with same exception 🙅
c
what if you set the width of your sheetContent to .fillMaxWidth?
u
Still crashing
Thought maybe device to render some stuff so I’ve added delay:
Copy code
LaunchedEffect(key1 = state, block = {
        if (state.currentValue == ModalBottomSheetValue.Hidden) {
            scope.launch {
                delay(2500)
                state.animateTo(ModalBottomSheetValue.HalfExpanded)
            }
        }
    })
same result. How many workarounds we need to use in order to show this component? 😂
c
something must be wrong with the content then
u
I’ll continue investiagte later, and update here. thanks anyway for your time and effort @Csaba Szugyiczki
j
ModalBottomSheetLayout/ModalBottomSheetState can have three states/values: Hidden, HalfExpanded, Expanded. HalfExpanded is only included if the sheet content is taller than 50% of the ModalBottomSheetLayout. At 120dp, given that your layout fills the entire screen, you will only have a Hidden and Expanded state as the sheet content is smaller than 50% of the container. This means you are essentially telling ModalBottomSheetState to move to a value it doesn't know, hence the exception.
c
The exception message could be more specific in this case. Is there a way to tell in advance if the HalfExpanded state exists?
j
Yes, we've recently made some changes that will allow us to specify a better message here. There's no way to know "in advance" (except if your container/sheet content bright ratio is static) as the ModalBottomSheetState gets created before the data used to calculate the anchors is available. We have an open issue about this but have other higher priority work items in the backlog.
c
Thank you for the update!
j
u
Thank you very much for this detailed explanation Jossi.