Página·Grátis
Tela de login (Compose)
Tela de e-mail e senha no Material 3. Verifica os dois campos quando você toca em Sign in, mostra o erro que o seu servidor devolve e pode incluir os botões de senha esquecida e de digital. Você chama o BiometricPrompt.
Instalação grátis.
Se o PocketCode estiver instalado no seu Android, isto abre a página no app.
Prévia
LoginScreen.kt · 176 linhas
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.semantics.LiveRegionMode
import androidx.compose.ui.semantics.liveRegion
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
/**
* Email and password sign-in screen in Material 3.
*
* - Checks the email format and a minimum password length when the user taps Sign in, then shows
* the error under the field.
* - [errorMessage] is for the server's answer ("Wrong password"); screen readers announce it.
* - [isLoading] shows a spinner in the button and blocks a second tap.
* - [onBiometricSignIn]: pass it and a "Use fingerprint or face" button appears. This file does
* not call BiometricPrompt: do that in your callback with androidx.biometric.
*
* The texts are in English inside the code; move them to strings.xml for your app.
* Only needs androidx.compose.material3.
*/
@Composable
fun LoginScreen(
onSignIn: (email: String, password: String) -> Unit,
modifier: Modifier = Modifier,
isLoading: Boolean = false,
errorMessage: String? = null,
onForgotPassword: (() -> Unit)? = null,
onBiometricSignIn: (() -> Unit)? = null,
) {
var email by rememberSaveable { mutableStateOf("") }
var password by rememberSaveable { mutableStateOf("") }
var passwordVisible by rememberSaveable { mutableStateOf(false) }
var submitted by rememberSaveable { mutableStateOf(false) }
val focusManager = LocalFocusManager.current
val emailError = submitted && !isValidEmail(email)
val passwordError = submitted && password.length < MIN_PASSWORD_LENGTH
val submit: () -> Unit = {
submitted = true
if (isValidEmail(email) && password.length >= MIN_PASSWORD_LENGTH && !isLoading) {
focusManager.clearFocus()
onSignIn(email.trim(), password)
}
}
Column(Esta prévia está encurtada. O conteúdo completo vem com a instalação.
Mudanças na 1.0.0
Primeira versão.