Composables
Docs Login
Pairel

Phone number

package com.composables.pairel

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.union
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import com.composables.compose.ripple.rememberRippleIndication
import com.composables.pairel.components.Button
import com.composables.pairel.components.CenteredAppBar
import com.composables.pairel.components.Icon
import com.composables.pairel.components.IconButton
import com.composables.pairel.components.ScreenScaffold
import com.composables.pairel.components.Text
import com.composables.pairel.components.TextField
import com.composables.pairel.theme.ArrowLeft
import com.composables.pairel.theme.Icons
import com.composables.pairel.theme.Theme

private const val PhoneDigits = 10

@Composable
fun PhoneNumberScreen(
    navigateBack: () -> Unit,
    navigateToVerification: (phoneNumber: String) -> Unit,
    navigateToCountryCodeSelection: () -> Unit,
    modifier: Modifier = Modifier,
    purpose: SignInPurpose = SignInPurpose.CreateAccount,
) {
    val number = remember { TextFieldState("") }
    val digits = number.text.filter { it.isDigit() }
    val complete = digits.length == PhoneDigits

    ScreenScaffold(
        modifier = modifier,
        topBar = {
            CenteredAppBar(
                title = {
                    Text(
                        when (purpose) {
                            SignInPurpose.CreateAccount -> "Create account"
                            SignInPurpose.SignIn -> "Sign in"
                        }
                    )
                },
                navigation = {
                    IconButton(onClick = navigateBack) {
                        Icon(
                            Icons.ArrowLeft,
                            contentDescription = "Back",
                            modifier = Modifier.size(24.dp),
                        )
                    }
                },
            )
        },
    ) {
        Column(
            Modifier.fillMaxSize()
                .windowInsetsPadding(
                    WindowInsets.navigationBars
                        .union(WindowInsets.ime)
                        .union(WindowInsets.safeDrawing.only(WindowInsetsSides.Horizontal))
                ),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Column(
                Modifier.weight(1f)
                    .verticalScroll(rememberScrollState())
                    .widthIn(max = 560.dp)
                    .fillMaxWidth()
                    .padding(horizontal = 20.dp, vertical = 8.dp),
                verticalArrangement = Arrangement.spacedBy(12.dp),
            ) {
                Text(
                    when (purpose) {
                        SignInPurpose.CreateAccount -> "What's your number?"
                        SignInPurpose.SignIn -> "Welcome back"
                    },
                    style = Theme.typography.headline,
                )
                Text(
                    when (purpose) {
                        SignInPurpose.CreateAccount ->
                            "We'll text you a code to make sure it's really you. Your number isn't shown on your profile."
                        SignInPurpose.SignIn ->
                            "Enter the number you joined with and we'll text you a code."
                    },
                    color = Theme.colors.onBackgroundMuted,
                )
                Row(
                    Modifier.fillMaxWidth().padding(top = 12.dp),
                    horizontalArrangement = Arrangement.spacedBy(10.dp),
                    verticalAlignment = Alignment.Bottom,
                ) {
                    Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
                        Text(
                            "Country",
                            style = Theme.typography.caption,
                            color = Theme.colors.onBackgroundMuted,
                        )
                        Text(
                            "US +1",
                            style = Theme.typography.label,
                            modifier =
                                Modifier.clip(Theme.shapes.small)
                                    .background(Theme.colors.surfaceVariant)
                                    .clickable(
                                        interactionSource = remember { MutableInteractionSource() },
                                        indication =
                                            rememberRippleIndication(
                                                color = Theme.colors.onSurface
                                            ),
                                        role = Role.Button,
                                        onClick = navigateToCountryCodeSelection,
                                    )
                                    .padding(horizontal = 16.dp, vertical = 12.dp),
                        )
                    }
                    TextField(
                        label = "Phone number",
                        state = number,
                        placeholder = "(555) 123-4567",
                        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone),
                        modifier = Modifier.weight(1f),
                    )
                }
            }
            Button(
                onClick = { navigateToVerification("+1 $digits") },
                enabled = complete,
                modifier =
                    Modifier.widthIn(max = 560.dp)
                        .fillMaxWidth()
                        .padding(horizontal = 20.dp, vertical = 16.dp),
            ) {
                Text("Send code")
            }
        }
    }
}

More screens from Pairel

by Alex Styl

Search screens and components

Screen flows

Screens

UI Elements

One Android lesson, every Wednesday

Building Android apps?

What Alex learned building Android apps for 14 years, so you don't have to. Reads in under a minute.

Join 3,500+ Android developers. Unsubscribe any time.

You're subscribed

The next lesson lands in on Wednesday.