Skip to content

Toggling TextField.obscureText can show the last character #184483

Description

@loic-sharma

Steps to reproduce

  1. Run the sample app on a mobile device
  2. Type some text in the password field and immediately press the Show Text button twice

Expected results

When TextField.obscureText is toggled quickly from true → false → true, all characters should be obscured.

Actual results

When TextField.obscureText is toggled quickly from true → false → true, the most recently entered character is briefly visible.

Code sample

Code sample
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Padding(
            padding: .all(20.0),
            child: PasswordToggleField(),
          ),
        ),
      ),
    );
  }
}

class PasswordToggleField extends StatefulWidget {
  const PasswordToggleField({super.key});

  @override
  State<PasswordToggleField> createState() => _PasswordToggleFieldState();
}

class _PasswordToggleFieldState extends State<PasswordToggleField> {
  bool _isObscured = true;

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: .min,
      spacing: 20,
      children: [
        TextField(
          obscureText: _isObscured,
          decoration: const InputDecoration(
            border: OutlineInputBorder(),
            labelText: 'Password',
          ),
        ),
        ElevatedButton(
          onPressed: () {
            // 3. Toggle the state and rebuild the UI
            setState(() {
              _isObscured = !_isObscured;
            });
          },
          child: Text(_isObscured ? 'Show Text' : 'Hide Text'),
        ),
      ],
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration
obscureText_2.mov

Logs

N/A

Flutter Doctor output

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemsf: material designflutter/packages/flutter/material repository.team-text-inputOwned by Text Input teamtriaged-text-inputTriaged by Text Input team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions