@@ -30,8 +30,13 @@ import androidx.compose.foundation.layout.Column
30
30
import androidx.compose.foundation.layout.Row
31
31
import androidx.compose.foundation.layout.fillMaxWidth
32
32
import androidx.compose.foundation.layout.padding
33
+ import androidx.compose.foundation.layout.requiredSize
33
34
import androidx.compose.foundation.layout.width
35
+ import androidx.compose.foundation.shape.RoundedCornerShape
36
+ import androidx.compose.foundation.text.BasicSecureTextField
34
37
import androidx.compose.foundation.text.KeyboardOptions
38
+ import androidx.compose.foundation.text.input.TextFieldState
39
+ import androidx.compose.foundation.text.input.TextObfuscationMode
35
40
import androidx.compose.foundation.text.selection.DisableSelection
36
41
import androidx.compose.foundation.text.selection.SelectionContainer
37
42
import androidx.compose.material.icons.Icons
@@ -812,7 +817,7 @@ fun PhoneNumber() {
812
817
// [END android_compose_text_auto_format_phone_number_textfieldconfig]
813
818
814
819
// [START android_compose_text_auto_format_phone_number_transformtext]
815
- class NanpVisualTransformation () : VisualTransformation {
820
+ class NanpVisualTransformation : VisualTransformation {
816
821
817
822
override fun filter (text : AnnotatedString ): TransformedText {
818
823
val trimmed = if (text.text.length >= 10 ) text.text.substring(0 .. 9 ) else text.text
@@ -862,31 +867,43 @@ val Purple = Color(0xFF800080)
862
867
// [START android_compose_text_showhidepassword]
863
868
@Composable
864
869
fun PasswordTextField () {
865
- var password by rememberSaveable { mutableStateOf( " " ) }
870
+ val state = remember { TextFieldState ( ) }
866
871
var showPassword by remember { mutableStateOf(false ) }
867
- val passwordVisualTransformation = remember { PasswordVisualTransformation () }
868
-
869
- TextField (
870
- value = password,
871
- onValueChange = { password = it },
872
- label = { Text (" Enter password" ) },
873
- visualTransformation = if (showPassword) {
874
- VisualTransformation .None
872
+ BasicSecureTextField (
873
+ state = state,
874
+ textObfuscationMode =
875
+ if (showPassword) {
876
+ TextObfuscationMode .Visible
875
877
} else {
876
- passwordVisualTransformation
878
+ TextObfuscationMode . RevealLastTyped
877
879
},
878
- keyboardOptions = KeyboardOptions (keyboardType = KeyboardType .Password ),
879
- modifier = Modifier .fillMaxWidth(),
880
- trailingIcon = {
881
- Icon (
882
- if (showPassword) {
883
- Icons .Filled .Visibility
884
- } else {
885
- Icons .Filled .VisibilityOff
886
- },
887
- contentDescription = " Toggle password visibility" ,
888
- modifier = Modifier .clickable { showPassword = ! showPassword }
889
- )
880
+ modifier = Modifier
881
+ .fillMaxWidth()
882
+ .padding(6 .dp)
883
+ .border(1 .dp, Color .LightGray , RoundedCornerShape (6 .dp))
884
+ .padding(6 .dp),
885
+ decorator = { innerTextField ->
886
+ Box (modifier = Modifier .fillMaxWidth()) {
887
+ Box (
888
+ modifier = Modifier
889
+ .align(Alignment .CenterStart )
890
+ .padding(start = 16 .dp, end = 48 .dp)
891
+ ) {
892
+ innerTextField()
893
+ }
894
+ Icon (
895
+ if (showPassword) {
896
+ Icons .Filled .Visibility
897
+ } else {
898
+ Icons .Filled .VisibilityOff
899
+ },
900
+ contentDescription = " Toggle password visibility" ,
901
+ modifier = Modifier
902
+ .align(Alignment .CenterEnd )
903
+ .requiredSize(48 .dp).padding(16 .dp)
904
+ .clickable { showPassword = ! showPassword }
905
+ )
906
+ }
890
907
}
891
908
)
892
909
}
0 commit comments