From 3e273b14e2ac22ea6bc2b04ac56e0d3b1fc22032 Mon Sep 17 00:00:00 2001 From: Nils Reichardt Date: Sat, 6 Jun 2020 00:18:01 +0200 Subject: [PATCH] [web & desktop] Hide all characters in a TextField, when obscureText is true on web & desktop (#56794) --- .../lib/src/widgets/editable_text.dart | 14 ++++-- .../test/material/text_field_test.dart | 43 ++++++++++++++++++- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index a08b603571..96afec890b 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -2129,10 +2129,16 @@ class EditableTextState extends State with AutomaticKeepAliveClien if (widget.obscureText) { String text = _value.text; text = widget.obscuringCharacter * text.length; - final int o = - _obscureShowCharTicksPending > 0 ? _obscureLatestCharIndex : null; - if (o != null && o >= 0 && o < text.length) - text = text.replaceRange(o, o + 1, _value.text.substring(o, o + 1)); + // Reveal the latest character in an obscured field only on mobile. + if ((defaultTargetPlatform == TargetPlatform.android || + defaultTargetPlatform == TargetPlatform.iOS || + defaultTargetPlatform == TargetPlatform.fuchsia) && + !kIsWeb) { + final int o = + _obscureShowCharTicksPending > 0 ? _obscureLatestCharIndex : null; + if (o != null && o >= 0 && o < text.length) + text = text.replaceRange(o, o + 1, _value.text.substring(o, o + 1)); + } return TextSpan(style: widget.style, text: text); } // Read only mode should not paint text composing. diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 9e1b009219..526e5f5e7b 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -758,7 +758,7 @@ void main() { expect(cursorOffsetSpaces.dx, inputWidth - _kCaretGap); }); - testWidgets('obscureText control test', (WidgetTester tester) async { + testWidgets('mobile obscureText control test', (WidgetTester tester) async { await tester.pumpWidget( overlay( child: const TextField( @@ -796,7 +796,46 @@ void main() { editText = findRenderEditable(tester).text.text; expect(editText.substring(editText.length - 1), '\u2022'); - }); + }, variant: const TargetPlatformVariant({ TargetPlatform.iOS, TargetPlatform.android })); + + testWidgets('desktop obscureText control test', (WidgetTester tester) async { + await tester.pumpWidget( + overlay( + child: const TextField( + obscureText: true, + decoration: InputDecoration( + hintText: 'Placeholder', + ), + ), + ), + ); + await tester.showKeyboard(find.byType(TextField)); + + const String testValue = 'ABC'; + tester.testTextInput.updateEditingValue(const TextEditingValue( + text: testValue, + selection: TextSelection.collapsed(offset: testValue.length), + )); + + await tester.pump(); + + // Enter a character into the obscured field and verify that the character + // isn't shown to the user. + const String newChar = 'X'; + tester.testTextInput.updateEditingValue(const TextEditingValue( + text: testValue + newChar, + selection: TextSelection.collapsed(offset: testValue.length + 1), + )); + + await tester.pump(); + + final String editText = findRenderEditable(tester).text.text; + expect(editText.substring(editText.length - 1), '\u2022'); + }, variant: const TargetPlatformVariant({ + TargetPlatform.macOS, + TargetPlatform.linux, + TargetPlatform.windows, + })); testWidgets('Caret position is updated on tap', (WidgetTester tester) async { final TextEditingController controller = TextEditingController();