Bottom Sheet in KMP KMM Compose Multiplatform

In today’s tutorial, we will learn how to implement a Material Design Bottom Sheet in KMP KMM Compose Multiplatform for both Android & iOS platforms. This comprehensive guide explains all the code steps in detail, step by step. We are using the ModalBottomSheetLayout component, so let’s get started.

Bottom Sheet in KMP KMM Compose Multiplatform

Bottom Sheet in KMP KMM Compose Multiplatform:

Bottom Sheets are a major UI component in mobile applications. The bottom sheet can contain any element and slide from the bottom of the mobile screen to the top using the Slide animation. Bottom sheets are used to add additional information to the current screen without increasing its size. When the user clicks on a button or icon, we trigger the bottom sheet, which appears from the bottom of the device screen and contains all the additional information. It also maintains the application context.

Start coding for the app:

1. Creating a rememberModalBottomSheetState: It is used to show and hide the bottom sheet. We will set its default value as ModalBottomSheetValue.Hidden, which means the bottom sheet will be hidden by default when the screen loads.

2. Creating rememberCoroutineScope(): Will help us manage asynchronous tasks. It is required to show and hide the bottom sheet.

3. Model Bottom Sheet Layout:

Code explanation:

  • sheetState: Links the Bottom Sheet state (Hidden, Expanded).
  • sheetShape: Defines the rounded corners for the Bottom Sheet, Top Left, and Top Right.
  • sheetBackgroundColor: Sets the background color for the sheet.
  • sheetContent: Contains the content of the Bottom Sheet.

4. Designing the drag handle of the Bottom Sheet:

It is a small view that is presented at the top of the bottom sheet.

5. Implementing the Close Icon Button of the Bottom Sheet:

It is present at the top right side of the bottom sheet. On button click, we are changing the bottom sheet state to modalBottomSheetState.hide() to hide the bottom sheet.

6. Adding content in the Bottom Sheet:

For now, as per the tutorial’s purpose, I am only adding 2 Text components in the Bottom Sheet. You can put any element as per your requirement.

7. Creating a button to show the Bottom Sheet:

On the button click event, we set the State value to modalBottomSheetState.show(), which shows the bottom sheet.

Complete source code of App.kt file:

Screenshot on an Android device:

Bottom Sheet in KMP KMM Compose Multiplatform Android

Screenshot on an iOS device:

Bottom Sheet in KMP KMM Compose Multiplatform iOS

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *