A CMPCountryCodePicker is country picker library for Compose Multiplatform for Android, iOS and Desktop App.
Add the dependency to your build.gradle.kts file:
commonMain.dependencies {
implementation("network.chaintech:cmp-country-code-picker:1.0.1")
}
@Composable
fun CountryPickerBasicTextField(
mobileNumber: String,
defaultCountryCode: String,
onMobileNumberChange: (String) -> Unit,
onCountrySelected: (country: CountryDetails) -> Unit,
modifier: Modifier = Modifier,
defaultPaddingValues: PaddingValues = PaddingValues(4.dp, 6.dp),
showCountryFlag: Boolean = true,
showCountryPhoneCode: Boolean = true,
showCountryName: Boolean = false,
showCountryCode: Boolean = false,
showArrowDropDown: Boolean = true,
spaceAfterCountryFlag: Dp = 8.dp,
spaceAfterCountryPhoneCode: Dp = 6.dp,
spaceAfterCountryName: Dp = 6.dp,
spaceAfterCountryCode: Dp = 6.dp,
countryFlagSize: Dp = 26.dp,
showVerticalDivider: Boolean = true,
spaceAfterVerticalDivider: Dp = 4.dp,
verticalDividerColor: Color = MaterialTheme.colorScheme.onSurface,
verticalDividerHeight: Dp = 26.dp,
countryPhoneCodeTextStyle: TextStyle = TextStyle(),
countryNameTextStyle: TextStyle = TextStyle(),
countryCodeTextStyle: TextStyle = TextStyle(),
enabled: Boolean = true,
readOnly: Boolean = false,
textStyle: TextStyle = LocalTextStyle.current,
label: @Composable() (() -> Unit)? = null,
placeholder: @Composable() (() -> Unit)? = null,
trailingIcon: @Composable() (() -> Unit)? = null,
prefix: @Composable() (() -> Unit)? = null,
suffix: @Composable() (() -> Unit)? = null,
supportingText: @Composable() (() -> Unit)? = null,
isError: Boolean = false,
visualTransformation: VisualTransformation = VisualTransformation.None,
singleLine: Boolean = false,
maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
minLines: Int = 1,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
shape: Shape = RoundedCornerShape(12.dp),
colors: TextFieldColors = OutlinedTextFieldDefaults.colors(),
focusedBorderThickness: Dp = 2.dp,
unfocusedBorderThickness: Dp = 1.dp,
onDone: () -> Unit = {},
)mobileNumberThe mobile number to be shown in the text field.onMobileNumberChangeThe callback that is triggered when the input service updates the mobile number. An updated mobile number comes as a parameter of the callback.onCountrySelectedThe callback function is triggered each time a country is selected within the picker. Additionally, it is also invoked when the picker is first displayed on the screen with the default selected country.modifierThe Modifier to be applied to this text field.defaultPaddingValuesThe spacing values to apply internally between the container and the contentshowCountryFlagDetermines whether to show the country flag within the picker.showCountryPhoneCodeDetermines whether to show the country phone code within the picker.showCountryNameDetermines whether to show the country name within the picker.showCountryCodeDetermines whether to show the country code within the picker.showArrowDropDownDetermines whether to show the arrow drop-down icon within the picker.spaceAfterCountryFlagThe space to add after the country flag.spaceAfterCountryPhoneCodeThe space to add after the country phone code.spaceAfterCountryNameThe space to add after the country name.spaceAfterCountryCodeThe space to add after the country code.countryFlagSizeThe size of the country flag.showVerticalDividerDetermines whether to show the vertical divider within the picker.spaceAfterVerticalDividerThe space to add after the vertical divider.verticalDividerColorThe color of the vertical divider.verticalDividerHeightThe height of the vertical divider.countryPhoneCodeTextStyleThe text style for the country phone code.countryNameTextStyleThe text style for the country name.countryCodeTextStyleThe text style for the country code.enabledControls the enabled state of this text field. When false, this component will not respond to user input, and it will appear visually disabled and disabled to accessibility services.readOnlyControls the editable state of the text field. When true, the text field cannot be modified. However, a user can focus it and copy text from it. Read-only text fields are usually used to display pre-filled forms that a user cannot edit.textStyleThe style to be applied to the input text. Defaults to LocalTextStyle.labelThe optional label to be displayed inside the text field container.placeholderThe optional placeholder to be displayed when the text field is in focus and the input text is empty.trailingIconThe optional trailing icon to be displayed at the end of the text field container.prefixThe optional prefix to be displayed before the input text in the text field.suffixThe optional suffix to be displayed after the input text in the text field.supportingTextThe optional supporting text to be displayed below the text field.isErrorIndicates if the text field's current value is in error.visualTransformationTransforms the visual representation of the input value.singleLineWhen true, this text field becomes a single horizontally scrolling text field instead of wrapping onto multiple lines.maxLinesThe maximum height in terms of maximum number of visible lines.minLinesThe minimum height in terms of minimum number of visible lines.interactionSourceThe MutableInteractionSource representing the stream of Interactions for this text field.shapeDefines the shape of this text field's border.colorsTextFieldColors that will be used to resolve the colors used for this text field in differentfocusedBorderThicknessRepresents the border thickness for focused state.unfocusedBorderThicknessRepresents the border thickness for unfocused state.onDoneThe callback is triggered when the user clicks the Done button on the keyboard, as the default IME action is set to Done.
@Composable
fun CountryPickerBasicText() {
val selectedCountryState: MutableState<CountryDetails?> = remember {
mutableStateOf(null)
}
var mobileNumber by remember {
mutableStateOf("")
}
CountryPickerBasicTextField(
mobileNumber = mobileNumber,
defaultCountryCode = "ad",
onMobileNumberChange = {
mobileNumber = it
},
onCountrySelected = {
selectedCountryState.value = it
},
modifier = Modifier.fillMaxWidth(),
defaultPaddingValues = PaddingValues(6.dp),
showCountryFlag = true,
showCountryPhoneCode = true,
showCountryName = false,
showCountryCode = false,
showArrowDropDown = true,
spaceAfterCountryFlag = 8.dp,
spaceAfterCountryPhoneCode = 6.dp,
spaceAfterCountryName = 6.dp,
spaceAfterCountryCode = 6.dp,
label = {
Text(text = "Mobile Number")
},
focusedBorderThickness = 2.dp,
unfocusedBorderThickness = 1.dp,
shape = RoundedCornerShape(10.dp),
verticalDividerColor = Color(0XFFDDDDDD),
colors = OutlinedTextFieldDefaults.colors(
focusedBorderColor = Color(0XFFDDDDDD),
unfocusedBorderColor = Color(0XFFDDDDDD)
)
)
}-
For Demo Checkout This Class
-
Medium Article for detailed explaination.
-
Connect us on LinkedIn
