Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix selection not deselected when TextField loses focus #103424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

Renzo-Olivares
Copy link
Contributor

@Renzo-Olivares Renzo-Olivares commented May 10, 2022

Description

The original issue was introduced by #101954 because the selectionColor on EditableText was never null. Previously null was passed to EditableText to paint no selection when it did not have focus.

// Only show the selection highlight when the text field is focused.
This change allows the selectionColor on EditableText to be null again by only taking widget.selectionColor and not inheriting DefaultSelectionStyle.of(context).

Related Issues

Fixes #103341

Tests

Added tests to ensure selectionColor is dropped when focus is lost on TextField, and add regression test.

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • All existing and new tests are passing.

…no selection is painted when no selectionColor is passed
@flutter-dashboard flutter-dashboard bot added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. labels May 10, 2022
@flutter-dashboard flutter-dashboard bot added f: cupertino flutter/packages/flutter/cupertino repository f: material design flutter/packages/flutter/material repository. labels May 10, 2022
@Renzo-Olivares Renzo-Olivares marked this pull request as ready for review May 10, 2022 17:27
Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -660,7 +660,8 @@ void main() {
expect(focusNode.hasFocus, isFalse);
});

testWidgets('use DefaultSelectionStyle for selection color', (WidgetTester tester) async {
testWidgets('selection color can be null with DefaultSelectionStyle as parent', (WidgetTester tester) async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename to EditableText does not derive selection color from DefaultSelectionStyle

Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Just as a thought, what if we passed a transparent Color as selectionColor instead of null?

@@ -433,6 +433,62 @@ void main() {
},
);

testWidgets('Text field drops selection color when losing focus', (WidgetTester tester) async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Is it relevant to say "Regression test for #103341" here and elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Just as a thought, what if we passed a transparent Color as selectionColor instead of null?

That works too. Though if someone explicitly passes null as selectionColor to EditableText. I think they would expect no selection to be painted, where as now it defaults to the color defined by DefaultSelectionStyle.of(context).

Copy link
Contributor

@chunhtai chunhtai May 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion on this, but if I were to redesign this again, passing null as color means disabling the selection seems to be a weird API, In that case I would probably throw if the color is null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, though another small difference between null and Colors.transparent is that the TextHighlightPainter skips painting all together when the selectionColor is null vs when it is Colors.transparent it will still paint some values.

if (range == null || color == null || range.isCollapsed) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: text input Entering text in a text field or keyboard related problems f: cupertino flutter/packages/flutter/cupertino repository f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Textfield selection is not deselected after it loses focus
4 participants