Checkbox in KMP Kotlin Compose Multiplatform

In today’s tutorial, we will learn about Checkbox in KMP Kotlin Compose Multiplatform for both Android & iOS platforms. We will discuss creating a Checkbox in KMP(KMM), handling the Checkbox state, customizing Checkbox properties, and showing Checkbox selected items in the Text widget.

Checkbox in KMP Kotlin Compose Multiplatform

Checkbox in KMP Kotlin Compose Multiplatform:

A Checkbox is a UI widget allowing application users to select or deselect an option. It allows us to select multiple options from a list by checking and unchecking states. The checkbox is mainly used in applications for Remember Me, Accepting terms and conditions agreements, and giving the users multiple choice selection boxes.

Implement Checkbox in KMP:

In Kotlin Compose Multiplatform, a widget named Checkbox is given to create material style Checkbox for both Android & iOS.

Code explanation:

  1. isChecked: State to hold the check and uncheck result in True and false format.
  2. checked: the value of the Checkbox.
  3. onCheckedChange: Calls every time the user checks or unchecks the Checkbox.

Source code:

Screenshot

Material Checkbox in KMP

Adding Label to Checkbox in KMP:

To add a Label to CheckBox, we will wrap both Checkbox and Text into a Row widget. This will align both of them horizontally.

Source code:

Screenshot:

Labeled Checkbox

Customizing Checkbox in KMP:

The Checkbox widget has color properties, such as CheckboxDefaults.colors(). It has 5 sub-properties: checkedColor, uncheckedColor, and checkmarkColor, disabledColor and disabledIndeterminateColor.

Screenshot:

Colored Checkbox

Checkbox with Multi-Selection Enabled in KMP:

Finally, we are creating multiple checkboxes with multiple selected enabled. We have a list of Strings with the list of checkbox enabled and disabled states. We show selected items in a Text on each selection and de-selection. With this, you can easily learn to get multiple selected items into a Single object in Checkbox.

Code explanation:

  1. tasks: Checkbox list items.
  2. checkedStates: Checkbox selected items state.
  3. tasks.forEachIndexed: To render Checkbox items from the list.

Complete source code for App.kt file:

Screenshots in Android device:

Checkbox in KMP Kotlin Compose Multiplatform

Screenshot in iOS device:

Checkbox in KMP Kotlin Compose Multiplatform

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 *