Modifier Properties in KMP Compose Multiplatform Guide

Modifier properties in KMP are essential in UI design and Compose Multiplatform applications. They decorate a widget with different behaviours. In the era of Kotlin, the KMP cross-platform development modifier property gives us control over shaping an object and applying different UI styles to it. The Modifier is one of the most essential styling tools in KMP for changing the appearance of a layout. It is used in all widgets to define their sizes, apply borders and much more.

Modifier Properties in KMP Compose Multiplatform Guide

There are many modifier properties in KMP with multiple combinations. In this tutorial, we will understand each one step by step.

  1. Modifier.width(200.dp)
  2. Modifier.height(300.dp)
  3. Modifier.fillMaxWidth()
  4. Modifier.fillMaxHeight()
  5. Modifier.fillMaxSize()
  6. Modifier.padding(16.dp)
  7. Modifier.size(200.dp)
  8. Modifier.background(Color.LightGray)
  9. Modifier.border(2.dp, Color.Black)
  10. Modifier.clip(RoundedCornerShape(8.dp))
  11. Modifier.align(Alignment.Center)
  12. Modifier.padding(horizontal = 16.dp, vertical = 8.dp)
  13. Modifier .weight(1f)
  14. Modifier.padding(start, top, end, bottom)

Properties with explanation:

1. Modifier.width(): Sets a fixed width in DP for the composable widget.

Modifier.width()

Code:

2. Modifier.height(): Sets a fixed height in the DP Density pixel for the composable widget.

Modifier.height()

Code:

3. Modifier.fillMaxWidth(): The fill max width covers the whole width of its parent widget. It’s the same as Fill Parent or Match Parent attributes in Android.

Modifier.fillMaxWidth()

Code:

4. Modifier.fillMaxHeight(): The fill max-height property will assign the parent widget height to the child widget.

Modifier.fillMaxHeight()

Code:

5. Modifier.fillMaxSize(): The fill max size property occupies the entire parent width and height of its parent widget to the child widget.

Modifier.fillMaxSize()

Code:

6. Modifier.padding(): The Padding property adds padding to the widget on the top, bottom, left and right sides equally.

Modifier.padding()

Code:

7. Modifier.size(): The size property will set the width and height to the given number in DP.

Modifier.size()

Code:

8. Modifier.background(): To set the background color of a Compose Multiplatform widget.

Modifier.background()

Code:

9. Modifier.border(): To set a border around a widget with border width and border color properties.

Modifier.border()

Code:

10. Modifier.clip(): To set a widget corner rounded.

Modifier.clip()

Code:

11. Modifier.align(): To align the child widget within its parent widget.

Modifier.align():

Code:

12. Modifier.padding(horizontal = 12.dp, vertical = 12.dp): To set padding on the child widget in Vertical and Horizontal with specific values.

Modifier.padding(horizontal = 12.dp, vertical = 12.dp)

Code:

13. Modifier.weight(): To distribute the available space among the children of a Root widget within Column and Row widgets.

Modifier.weight()

Code:

14. Modifier.padding(start, top, end, bottom): To individually set different padding on all Top, bottom, left and right sides.

Modifier.padding(start, top, end, bottom)

Code:

As modifier property names, you can understand their purpose. They can be used together by connecting through the DOT(.) operator. So, we will make an example in which all modifier properties are present.

Modifier properties combine example:

Code:

Modifier Properties in KMP Compose Multiplatform Guide

Thanks for reading our article. Happy reading.

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 *