Skip to content
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

TextField border gap padding improvement #92940

Merged
merged 10 commits into from
Nov 19, 2021

Conversation

justinmc
Copy link
Contributor

@justinmc justinmc commented Nov 2, 2021

Previously, the calculations for the border gap weren't quite right in all cases, and would sometimes result in extra unwanted padding. Here's an example where gapPadding is set to zero, meaning that the border should just touch the label, but it didn't:

          child: TextField(
            decoration: InputDecoration(
              labelText: 'Hello label',
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(32.0),
                gapPadding: 0.0,
             ),
            ),
          ),

Screen Shot 2021-11-02 at 3 31 14 PM

With this PR, the same code gives the correct result:

Screen Shot 2021-11-02 at 3 30 25 PM

Todos

  • Test.
  • Make sure the current math doesn't break any other cases.
  • Check other code paths and make sure they don't need their math updated as well.

Related issues

Fixes #82321
Old PR that may have caused this bug: #34515

@justinmc justinmc self-assigned this Nov 2, 2021
@flutter-dashboard flutter-dashboard bot added f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. labels Nov 2, 2021
@google-cla google-cla bot added the cla: yes label Nov 2, 2021
@@ -418,12 +418,11 @@ class OutlineInputBorder extends InputBorder {

const double cornerArcSweep = math.pi / 2.0;
final double tlCornerArcSweep = start < scaledRRect.tlRadiusX
? math.asin((start / scaledRRect.tlRadiusX).clamp(-1.0, 1.0))
? math.acos(1 - start / scaledRRect.tlRadiusX)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's the PR where this line originally came from: #34515. The new calculation appears to work for that case as well.

Copy link
Member

@werainkhatri werainkhatri Nov 2, 2021

Choose a reason for hiding this comment

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

A suggestion

final double tlCornerArcSweep = math.acos((1 - start / scaledRRect.tlRadiusX).clamp(0.0, 1.0));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I was worried about not having the clamp in there anymore. Will try it now.

@@ -432,7 +431,7 @@ class OutlineInputBorder extends InputBorder {
const double trCornerArcSweep = cornerArcSweep;
if (start + extent < scaledRRect.width - scaledRRect.trRadiusX) {
path
..relativeMoveTo(extent, 0.0)
..moveTo(scaledRRect.left + start + extent, scaledRRect.top)
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 change and the previous changes near line 425 make the gap the correct width. Before, it was too wide.

@skia-gold
Copy link

Gold has detected about 2 new digest(s) on patchset 1.
View them at https://flutter-gold.skia.org/cl/github/92940

@justinmc
Copy link
Contributor Author

justinmc commented Nov 3, 2021

The broken goldens are practically unnoticeable 1 pixel differences, so that's good. I'll plan to just accept those once I finalize this PR. I should also run the Google tests on this and watch out for breaking other visual diff tests.

@werainkhatri
Copy link
Member

I have raised a pull request on your fork with the working solution I wrote. See if that help 😬.

@skia-gold
Copy link

Gold has detected about 2 new digest(s) on patchset 3.
View them at https://flutter-gold.skia.org/cl/github/92940

@skia-gold
Copy link

Gold has detected about 4 new digest(s) on patchset 6.
View them at https://flutter-gold.skia.org/cl/github/92940

@skia-gold
Copy link

Gold has detected about 12 new digest(s) on patchset 8.
View them at https://flutter-gold.skia.org/cl/github/92940

@skia-gold
Copy link

Gold has detected about 12 new digest(s) on patchset 9.
View them at https://flutter-gold.skia.org/cl/github/92940

@skia-gold
Copy link

Gold has detected about 1 new digest(s) on patchset 10.
View them at https://flutter-gold.skia.org/cl/github/92940

@justinmc justinmc marked this pull request as ready for review November 13, 2021 00:38
@flutter-dashboard
Copy link

Golden file changes have been found for this pull request. Click here to view and triage (e.g. because this is an intentional change).

If you are still iterating on this change and are not ready to resolve the images on the Flutter Gold dashboard, consider marking this PR as a draft pull request above. You will still be able to view image results on the dashboard, commenting will be silenced, and the check will not try to resolve itself until marked ready for review.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Changes reported for pull request #92940 at sha 4224a9f

@flutter-dashboard flutter-dashboard bot added the will affect goldens Changes to golden files label Nov 13, 2021
@justinmc
Copy link
Contributor Author

I've accepted the goldens changes, which as I expected were just two unnoticeable 1 pixel color changes plus the new golden tests I created.

@justinmc
Copy link
Contributor Author

I ran the Google test and got a few failures, but they were all the same as the goldens: a pixel or two that's a slightly different color. I'll change it to pass and plan to accept the changes after merge.

? math.asin((start / scaledRRect.tlRadiusX).clamp(-1.0, 1.0))
: math.pi / 2.0;
final double tlCornerArcSweep = math.acos(
(1 - start / scaledRRect.tlRadiusX).clamp(0.0, 1.0),
Copy link
Contributor

@LongCatIsLooong LongCatIsLooong Nov 17, 2021

Choose a reason for hiding this comment

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

question: is it guaranteed that the radiusX can't be 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I just tried this out and it can in fact be zero, but the clamp will make sure that it gets back on track and the behavior is correct. No errors are thrown.

Copy link
Contributor

Choose a reason for hiding this comment

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

TIL! So NaN.clamp(0, 1) == 1? But shouldn't the 1 - NaN be evaluated first and throw an error?

Copy link
Member

Choose a reason for hiding this comment

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

turns out, 1 - NaN = -NaN. So it finally clamps to 0.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And 1/0 is actually Infinity in Dart and not NaN, so it makes a little more sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. will affect goldens Changes to golden files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TextField with rounded corners - floating label shows extra whitespace
5 participants