[web & desktop] Hide all characters in a TextField, when obscureText is true on web & desktop (#56794)

This commit is contained in:
Nils Reichardt
2020-06-06 00:18:01 +02:00
committed by GitHub
parent ab68721a5d
commit 3e273b14e2
2 changed files with 51 additions and 6 deletions

View File

@@ -2129,10 +2129,16 @@ class EditableTextState extends State<EditableText> 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.

View File

@@ -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>{ 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>{
TargetPlatform.macOS,
TargetPlatform.linux,
TargetPlatform.windows,
}));
testWidgets('Caret position is updated on tap', (WidgetTester tester) async {
final TextEditingController controller = TextEditingController();