Joseph Hawkes-Cates
11/08/2021, 7:17 PMJoseph Hawkes-Cates
11/08/2021, 7:20 PM@Composable
private fun PasswordField() {
var password by rememberSaveable { mutableStateOf("")}
var passwordVisibility: Boolean by remember { mutableStateOf(false) }
val iconColor = if(isSystemInDarkTheme()) steel02 else steel08
TextField(
value = password,
onValueChange = {password = it},
visualTransformation = if (passwordVisibility) VisualTransformation.None else PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
leadingIcon = {
val image = if(passwordVisibility) Icons.Filled.Visibility else Icons.Filled.VisibilityOff
val description = if(passwordVisibility) stringResource(R.string.password_visible)
else stringResource(R.string.password_not_visible)
val iconBackground = if(isSystemInDarkTheme()) steel09 else steel02
IconButton(
onClick = { passwordVisibility = !passwordVisibility },
modifier = Modifier.background(color = iconBackground)
) {
Icon(imageVector = image, contentDescription = description)
}
},
trailingIcon = {
IconButton(onClick = { password = "" }) {
Icon(imageVector = Icons.Filled.Clear, contentDescription = "Clear password")
}
},
colors = TextFieldDefaults.textFieldColors(
backgroundColor = MaterialTheme.colors.surface,
leadingIconColor = iconColor,
trailingIconColor = iconColor
),
shape = MaterialTheme.shapes.small
)
}
Chris Sinco [G]
11/08/2021, 9:52 PMTextField
is designed to follow the Material spec and it seems some of the changes would be challenging to customize within the current slots approach (seems this way after skimming the implementation code).Chris Sinco [G]
11/08/2021, 9:52 PMI’ll lose a bunch of little default visual behaviorsWhat behaviors would you lose?
Chris Sinco [G]
11/08/2021, 9:54 PMJoseph Hawkes-Cates
11/09/2021, 3:12 AMJoseph Hawkes-Cates
11/09/2021, 3:12 AMJoseph Hawkes-Cates
11/09/2021, 3:13 AMJoseph Hawkes-Cates
11/09/2021, 4:22 AM@Composable
private fun PasswordField() {
var password by rememberSaveable { mutableStateOf("")}
var passwordVisibility: Boolean by remember { mutableStateOf(false) }
var isFocused by remember { mutableStateOf(false) }
val iconColor = if(isSystemInDarkTheme()) steel02 else steel08
val borderColor = when {
isFocused -> MaterialTheme.colors.primary
isSystemInDarkTheme() -> steel05
else -> steel03
}
Surface(
shape = MaterialTheme.shapes.small,
modifier = Modifier
.fillMaxWidth()
.border(1.dp, borderColor, MaterialTheme.shapes.small)
) {
Row(
verticalAlignment = Alignment.CenterVertically
) {
val image =
if (passwordVisibility) Icons.Filled.Visibility else Icons.Filled.VisibilityOff
val description = if (passwordVisibility) stringResource(R.string.password_visible)
else stringResource(R.string.password_not_visible)
val iconBackground = if (isSystemInDarkTheme()) steel09 else steel02
IconButton(
onClick = { passwordVisibility = !passwordVisibility },
modifier = Modifier.background(color = iconBackground)
) {
Icon(
imageVector = image,
contentDescription = description,
tint = iconColor
)
}
BasicTextField(
value = password,
onValueChange = { password = it },
visualTransformation = if (passwordVisibility) VisualTransformation.None else PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
singleLine = true,
modifier = Modifier
.weight(1f)
.padding(4.dp)
.onFocusChanged { isFocused = it.isFocused }
)
IconButton(onClick = { password = "" }) {
Icon(
imageVector = Icons.Filled.Clear,
contentDescription = stringResource(R.string.clear_password)
)
}
}
}
}
Joseph Hawkes-Cates
11/09/2021, 4:25 AMJoseph Hawkes-Cates
11/09/2021, 4:25 AMChris Sinco [G]
11/09/2021, 6:33 AMJoseph Hawkes-Cates
11/09/2021, 2:49 PMJoseph Hawkes-Cates
11/09/2021, 3:26 PMChris Sinco [G]
11/09/2021, 4:11 PMTextField
so you can customize that part.Anastasia [G]
11/09/2021, 5:34 PMBasicTextField
is exactly the thing we expect you to do in this case. Regarding your code, did you consider using decorationBox
parameter of the BasicTextField? It should give you better accessibility support, for example.
Roughly, instead of Row { Icon(); BasicTextField(); Icon() }
you could do BasicTextField() { Icon(); it(); Icon() }
Joseph Hawkes-Cates
11/09/2021, 5:37 PMJoseph Hawkes-Cates
11/09/2021, 5:59 PMJoseph Hawkes-Cates
11/09/2021, 6:02 PMAnastasia [G]
11/09/2021, 6:05 PMBox
has a propagateMinConstraints
or something like that parameter which might need to be set to true
in your caseJoseph Hawkes-Cates
11/09/2021, 6:07 PM