1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter - Textfield lose focus when text is visible

Discussão em 'Mobile' iniciado por Stack, Setembro 27, 2024 às 23:52.

  1. Stack

    Stack Membro Participativo

    In my project I made a textfield for the password, and I made a method to show and hide the password. What happens is that when I press the icon to show the text, the keyboard and the cursor disappear, I mean, the focus is lost.

    How do I make that when I press the icon it doesn't lose the focus? Thanks

    class ExampleLoginFocus extends StatefulWidget {
    @override
    _ExampleLoginFocusState createState() => _ExampleLoginFocusState();
    }

    class _ExampleLoginFocusState extends State<ExampleLoginFocus> {
    FocusNode focusPass;

    var pass = '';
    bool passwordVisible;

    @override
    void initState() {
    super.initState();
    passwordVisible = true;
    focusPass = FocusNode();
    }

    Widget password() {
    return TextField(
    obscureText: passwordVisible,
    focusNode: focusPass,
    decoration: InputDecoration(
    suffixIcon: Padding(
    padding: EdgeInsets.only(top: 20, bottom: 0),
    child: IconButton(
    icon: Icon(
    passwordVisible ? Icons.visibility_off : Icons.visibility,
    color: Theme.of(context).primaryColorDark,
    ),
    onPressed: () {
    setState(() {
    passwordVisible = !passwordVisible;
    });
    },
    )),
    ),
    onChanged: (String tex) {
    pass = tex;
    print(pass);
    },
    );
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    body: Container(
    child: password(),
    ),
    );
    }
    }

    Continue reading...

Compartilhe esta Página