Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose Bottom Sheet initialization error

In Jetpack compose 1.0.0-beta01, I am calling the BottomSheetScaffold like this:

BottomSheetScaffold(
    scaffoldState = bottomSheetScaffoldState,
    sheetContent = { Text("") },
    sheetShape = Shapes.large,
    backgroundColor = AppTheme.colors.uiBackground,
    modifier = modifier
    
) { (content) }

... and getting the following error:

java.lang.IllegalArgumentException: The initial value must have an associated anchor.

Any tips on fixing this?

like image 951
Marcola Carr Avatar asked Mar 06 '21 22:03

Marcola Carr


People also ask

How do I make a bottom sheet in compose?

Today we will talk about how to create bottom sheets in Jetpack Compose. To implement the bottom sheet, we need to use BottomSheetScaffold. It was annotated as an ExperimentalApi, so to use it we need to add @ExperimentalMaterialApi annotation to the method where we will use.

What is LazyColumn in jetpack compose?

A LazyColumn is a vertically scrolling list that only composes and lays out the currently visible items. It's similar to a Recyclerview in the classic Android View system.

What is mutableStateOf jetpack compose?

mutableStateOf creates an observable MutableState<T> , which is an observable type integrated with the compose runtime. Any changes to value will schedule recomposition of any composable functions that read value . In the case of ExpandingCard , whenever expanded changes, it causes ExpandingCard to be recomposed.

What is scaffold in jetpack compose?

Scaffold provides a slot for a floating action button. You can use the floatingActionButton slot and a FloatingActionButton : Scaffold(


3 Answers

Don't forget to add the following atribute:

sheetPeekHeight = 0.dp

So your code should be like this:

BottomSheetScaffold(
    scaffoldState = bottomSheetScaffoldState,
    sheetContent = { Text("") },
    sheetShape = Shapes.large,
    sheetPeekHeight = 0.dp, // <--- new line
    backgroundColor = AppTheme.colors.uiBackground,
    modifier = modifier
    
) { (content) }
like image 169
Marcola Carr Avatar answered Oct 13 '22 10:10

Marcola Carr


When bottomSheetState is expand, sheetContent must have real content to show.

You need check this.

like image 34
Muse Avatar answered Oct 13 '22 10:10

Muse


In case you end up on this page because you use the BackdropScaffold, the suggested solution does the trick as well. Simply set the peekHeight. For example like this:

BackdropScaffold(
        appBar = {},
        backLayerContent = {},
        frontLayerContent = {},
        peekHeight = 0.dp
    )

Then Preview works like a charm again.

Fun fact though: Don't set it to 56.dp which is the default init value it normally should be initialised with (value of BackdropScaffoldDefaults.PeekHeight). 56.dp results in the anchor rendering problem in my current setup. (Using compose version '1.1.1')

like image 41
jendress Avatar answered Oct 13 '22 10:10

jendress