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

Skip to content

Add default selection style #100719

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

Merged
merged 11 commits into from
Apr 13, 2022
Merged

Conversation

chunhtai
Copy link
Contributor

based on #99576

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.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard flutter-dashboard bot added 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. labels Mar 24, 2022
@chunhtai chunhtai force-pushed the default-selection branch from 4686f4f to 66a4845 Compare March 24, 2022 23:39
Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

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

This looks good, just some trivial feedback to prove that I read the code :-).

@@ -712,7 +712,8 @@ class CupertinoTextField extends StatefulWidget {

/// The color to use when painting the cursor.
///
/// Defaults to the [CupertinoThemeData.primaryColor] of the ambient theme,
/// Defaults to the [DefaultSelectionStyle.cursorColor]. If that color is
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume that we have a test that checks this default for the light/dark themes; likewise for selectionColor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The existing tests covered it

child: Builder(
builder: _buildWidgetApp,
child: DefaultSelectionStyle(
selectionColor: effectiveThemeData.primaryColor.withOpacity(0.2),
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the 0.2 opacity based on?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the value that the CupertinoTextField uses. Since CupertinoTextField needs to be under CupertinoApp, so I put the value up to the CupertinoApp. This will however change the behavior for TextField that is under CupertinoApp, I am not sure whether that will be a problem

}) : assert(data != null),
super(key: key, child: _wrapsWithDefaultSelectionStyle(data, child));

static Widget _wrapsWithDefaultSelectionStyle(TextSelectionThemeData data, Widget child) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function could be a widget with a const constructor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how would that help with making TextSelectionTheme const? the use the const widget in the initializer, the input also needs to be const value.

so I can't do

super(key: key, child: const MyWidget(data: data, child: child));

because the data and child is runtime value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although i can make it const by doing some hack, but that will need to rely on the implementation detail of ProxyWidget

Copy link
Contributor

Choose a reason for hiding this comment

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

Even if it's not possible to use a const constructor for a _WithDefaultSelectionStyle() widget, it seems clearer and more in-line with Flutter conventions than using a function that has the same effect.

@chunhtai chunhtai force-pushed the default-selection branch from 564abee to 641a282 Compare April 11, 2022 20:32
@chunhtai chunhtai requested a review from HansMuller April 12, 2022 19:44
///
/// Defaults to the theme's `cursorColor` when null.
/// The cursor indicates the current location of the text insertion point in
Copy link
Contributor

Choose a reason for hiding this comment

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

The cursor indicates the current location of the text insertion point in the field. =>
The cursor indicates the current text insertion point.

/// The cursor indicates the current location of the text insertion point in
/// the field.
///
/// If this is null it will default to the ambient
Copy link
Contributor

Choose a reason for hiding this comment

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

If null then [DefaultSelectionStyle.cursorColor] is used. If that is also null and [ThemeData.platform] is [TargetPlatform.iOS] or [TargetPlatform.macOS], then [CupertinoThemeData.primaryColor] is used. Otherwise [ColorScheme.primary] of [ThemeData.colorScheme] is used.


/// The properties for descendant [TextField] and [SelectableText] widgets.
final TextSelectionThemeData data;

// Overriding the getter to insert `DefaultSelectionStyle` into the subtree
// without a breaking change. This should be avoid in general since this
Copy link
Contributor

Choose a reason for hiding this comment

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

breaking => breaking API

In general, this approach should be avoided because it relies on an implementation detail of ProxyWidget. This workaround is necessary because TextSelectionTheme is const.

// without a breaking change. This should be avoid in general since this
// relies on the implementation detail of ProxyWidget.
//
// The `TextSelectionTheme` should be non-const if it were to start
Copy link
Contributor

Choose a reason for hiding this comment

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

You can leave this out

Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

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

LGTM

}) : assert(data != null), super(key: key, child: child);
}) : assert(data != null),
_child = child,
super(key: key, child: const _NullWidget());
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe include a warning comment here:

// See `get child` override below

engine-flutter-autoroll added a commit to engine-flutter-autoroll/plugins that referenced this pull request Apr 13, 2022
@Sylith1231
Copy link

Should the documentation (https://api.flutter.dev/flutter/material/TextSelectionThemeData/selectionHandleColor.html) be edited to reflect the suggested approach above?

@chunhtai
Copy link
Contributor Author

@Sylith1231 The TextSelectionTheme still works, it will create a defaultSelectionStyle as part of its widget build.

chunhtai added a commit to chunhtai/flutter that referenced this pull request Apr 14, 2022
chunhtai added a commit that referenced this pull request Apr 14, 2022
* Revert "Add default selection style (#100719)"

This reverts commit c8057bc.

* update

* update
chunhtai added a commit to chunhtai/flutter that referenced this pull request Apr 15, 2022
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.

4 participants