KMP Surface Widget Guide in Compose Multiplatform

Today’s tutorial teaches how to use the Surface widget in Kotlin Compose Multiplatform. It is a comprehensive guide for the Surface widget. It explains each property step-by-step with sample examples, which will improve our UI capabilities in KMP(Kotlin Multiplatform) or KMM(Kotlin Multiplatform Mobile) development.

KMP Surface Widget Guide in Compose Multiplatform

KMP Surface Widget Guide in Compose Multiplatform

In Kotin Compose Multiplatform, we can create shared UI for different platforms. In KMP, you’ll find many widgets for designing UI elements, among them the Surface widget. The Surface widget is a type of container widget with custom styling options. It provides custom shapes, shadows, custom backgrounds, and more. It can be used as a root container to create multiple child containers with a surface.

Properties of Surface widget:

  1. Modifier
  2. Shape
  3. color
  4. contentColor
  5. border
  6. elevation

1. Modifier: The modifier property allows us to set width, height, padding, clickable, Z-index, and more.

Screenshot:

KMP Surface Widget Guide in Compose Multiplatform

 

2. Shape: The Surface’s shape property allows us to define different types of UI shapes as components. The Surface widget offers five types of shape properties. We will create a single program and define all the below shapes.

  1. Rounded Corner Shape
  2. Cut Corner Shape
  3. Circle Shape
  4. Rectangle Shape
  5. Custom shape

Screenshot:

Surface Shapes properties

3. Color: The color property sets the background color of the Surface widget in Compose Multiplatform. It supports the HEX, RGB  and color constants predefined with Compose Multiplatform.

Screenshot:

4. Content Color: The content Color property sets the Color of the inner content of the Surface widget, like the Text Color. Thus, you don’t have to manually define the inner Text Color. We set up the inner text content color in this example as RED.

Screenshot:

Surface inner content color

5. Border: The Border Stroke property allows us to set a simple border around a Surface widget. It supports two properties: Border width and border color.

Screenshot:

Surface Widget with border

6. Elevation: The elevation property sets the shadow effect on the Surface widget. The material style automatically handles the shadow AXIS.

Screenshot:

Surface with Shadow effect

Conclusion:

I have tried my best to explain each property of the Surface widget in Compose Multiplatform. Using the surface and its properties, we can create any type of custom container view in KMP. So keep practising each of my codes, and you will learn something new in KMP. Happy coding.

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 *