Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 7, 2021 12:53 pm GMT

How to use Multiple Bottom-sheets in Android Compose

After trying to refactor my project from the normal view system to Android Compose, I can really feel the difference between them.

I feel Android Compose will for sure become the default way to build UI in native android, but it still have a lot of cons that Google engineers are trying to solve but till then, I am gonna write on how to solve one of them in this post and it is how to have multiple bottom-sheets in the same app.

Before reading please be careful that this post applies to compose version 1.0.0-beta05.
and is definitely related to these issues:

If these two issues are not solved yet you can continue reading my post, if any of them are solved and it will change the way to support multiple bottom-sheets, I will write another post.

1- Let's start by defining the bottom sheets that we are gonna use in our application

sealed class BottomSheetScreen() {    object Screen1: BottomSheetScreen()    object Screen2: BottomSheetScreen()    class Screen3(val argument:String):BottomSheetScreen()}

2- We will have a mutable state of our current bottom sheet screen in the top composable function that will show the bottom sheet

@Composableprivate fun MainLayout() { var currentBottomSheet: BottomSheetScreen by remember{        mutableStateOf(BottomSheetScreen.Screen1)    }}

3- We will use BottomSheetScaffold to handle opening and closing the bottom sheet

   val scaffoldState = rememberBottomSheetScaffoldState()   BottomSheetScaffold(sheetPeekHeight = 0.dp, scaffoldState = scaffoldState,        sheetShape = BottomSheetShape,        sheetContent = {            SheetLayout(currentBottomSheet,closeSheet)        }) { paddingValues ->            Box(Modifier.padding(paddingValues)){                MainContent(openSheet)            }    }

4- Now, We will define two lambdas for opening and closing the bottom sheet

val closeSheet: () -> Unit = {        scope.launch {            scaffoldState.bottomSheetState.collapse()        }    }    val openSheet: (BottomSheetScreen) -> Unit = {        currentBottomSheet = it        scope.launch { scaffoldState.bottomSheetState.expand() }    }

Summing it up, our code should be similar to that

@OptIn(ExperimentalMaterialApi::class)@Composableprivate fun MainLayout() {    val scope = rememberCoroutineScope()    val scaffoldState = rememberBottomSheetScaffoldState()    var currentBottomSheet: BottomSheetScreen by remember{        mutableStateOf(BottomSheetScreen.Screen1)    }    val closeSheet: () -> Unit = {        scope.launch {            scaffoldState.bottomSheetState.collapse()        }    }    val openSheet: (BottomSheetScreen) -> Unit = {        currentBottomSheet = it        scope.launch { scaffoldState.bottomSheetState.expand() }    }    BottomSheetScaffold(sheetPeekHeight = 0.dp, scaffoldState = scaffoldState,        sheetShape = BottomSheetShape,        sheetContent = {            SheetLayout(currentBottomSheet,closeSheet)        }) { paddingValues ->            Box(Modifier.padding(paddingValues)){                MainContent(openSheet)            }    }}

5- Now we will define our SheetLayout, as we can see from the above code that it takes two parameters

  • The current bottom sheet
  • The closeSheet lambda, because if we want to have a button to close the sheet

It should be similar to that

@Composablefun SheetLayout(currentScreen: BottomSheetScreen,onCloseBottomSheet :()->Unit) {    BottomSheetWithCloseDialog(onCloseBottomSheet){        when(currentScreen){            BottomSheetScreen.Screen1 -> Screen1()            BottomSheetScreen.Screen2 -> Screen2()            is BottomSheetScreen.Screen3 -> Screen3(argument = currentScreen.argument)        }    }}

6- Finally last step we define MainContent composable function that takes openSheet as a parameter

@Composablefun MainContent(openSheet: (BottomSheetScreen) -> Unit) {    Column(Modifier.fillMaxSize(),verticalArrangement = Arrangement.SpaceEvenly,horizontalAlignment = Alignment.CenterHorizontally) {        Text(text = "This is Main Content")        Button(onClick = { openSheet(BottomSheetScreen.Screen1) }) {            Text(text = "Open bottom sheet 1")        }        Button(onClick = { openSheet(BottomSheetScreen.Screen2) }) {            Text(text = "Open bottom sheet 2")        }        Button(onClick = { openSheet(BottomSheetScreen.Screen3("this is an argument")) }) {            Text(text = "Open bottom sheet 2")        }    }}

And now we can easily build an app that contains multiple bottom sheets as below
ss

NB: Here is the whole project used in the Demo on Github

Thanks for reading


Original Link: https://dev.to/davidibrahim/how-to-use-multiple-bottom-sheets-in-android-compose-382p

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To