Skip to content

Conversation

@ksokolovskyi
Copy link
Contributor

This PR contributes to #155313

Description

  • Adds example for WidgetStateProperty
  • Adds tests for examples/api/lib/widgets/widget_state/widget_state_property.0.dart

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].
  • I followed the [breaking change policy] and added [Data Driven Fixes] where supported.
  • All existing and new tests are passing.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos labels Sep 17, 2024
@nate-thegrate nate-thegrate self-requested a review September 18, 2024 03:26
Copy link
Contributor

@nate-thegrate nate-thegrate left a comment

Choose a reason for hiding this comment

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

Thanks very much for adding this!

I think the conversation from #89127 is relevant here—the WidgetStateProperty.resolveWith() constructor unfortunately doesn't support stable equality checks, so in some scenarios it can lead to a lot of unnecessary rebuilding.

Additionally, the callback it uses is kind of dense and unintuitive:

WidgetStateProperty.resolveWith((states) {
  if (states.contains(WidgetState.hovered) || states.contains(WidgetState.pressed)) {
    return Colors.blue;
  }
  return Colors.black;
})

I was recently able to get #154695 merged, so now the .fromMap() constructor supports accurate equality checks. It's also quite a bit more concise:

WidgetStateProperty.fromMap({
  WidgetState.hovered | WidgetState.pressed: Colors.blue,
  WidgetState.any: Colors.black,
})

Perhaps we could help new developers avoid the pitfalls of "broken ThemeData equality" by promoting this constructor in the example file.

Let me know what you think :)

@ksokolovskyi
Copy link
Contributor Author

ksokolovskyi commented Sep 18, 2024

Hi @nate-thegrate

Thanks for the review and your work on WidgetStateProperty.fromMap!

I applied your suggestions in the latest commit, could you please take a look again?

Additionally, I have taken a look at the implementation of the fromMap and noticed that it relies on the order of the map entries. When using the regular map I didn't notice any bugs, as the order of the entries equals the order in the code. But when using e.g. HashMap (I don't know why someone may decide to use it in WidgetStateProperty, but still) the order of the entries may change from launch to launch which may cause some hard-to-spot bugs. Don't you think that it is worth mentioning that users must pass the regular map into the fromMap?

To reproduce the behavior you can modify the widget_state_property.0.dart code with the following foregroundColor and run widget_state_property.0_test.dart tests a couple of times.

foregroundColor: WidgetStateProperty<Color>.fromMap(
  HashMap<WidgetStatesConstraint, Color>.from(
    <WidgetStatesConstraint, Color>{
      WidgetState.pressed | WidgetState.hovered | WidgetState.focused:
          Colors.blue,
      WidgetState.any: Colors.red,
    },
  ),
),

The recording of the mentioned behavior:

widget_state_property_from_map.mov

@nate-thegrate
Copy link
Contributor

nate-thegrate commented Sep 18, 2024

When using the regular map I didn't notice any bugs, as the order of the entries equals the order in the code. But when using e.g. HashMap (I don't know why someone may decide to use it in WidgetStateProperty, but still) the order of the entries may change from launch to launch which may cause some hard-to-spot bugs. Don't you think that it is worth mentioning that users must pass the regular map into the fromMap?

I also don't know why someone would go out of their way to make their map not work, but this is a good point regardless!

As luck would have it, I have a PR open to configure each fromMap constructor so it inherits from the same class. I just pushed a commit there that adds an assert to that base class. Thanks very much for helping me out here!

edit: had to revert the commit since it was causing errors ☹️
(My guess is that depending on platform, LinkedHashMap sometimes counts as a HashMap)

@ksokolovskyi
Copy link
Contributor Author

When using the regular map I didn't notice any bugs, as the order of the entries equals the order in the code. But when using e.g. HashMap (I don't know why someone may decide to use it in WidgetStateProperty, but still) the order of the entries may change from launch to launch which may cause some hard-to-spot bugs. Don't you think that it is worth mentioning that users must pass the regular map into the fromMap?

I also don't know why someone would go out of their way to make their map not work, but this is a good point regardless!

As luck would have it, I have a PR open to configure each fromMap constructor so it inherits from the same class. I just pushed a commit there that adds an assert to that base class. Thanks very much for helping me out here!

Look great!

Copy link
Contributor

@nate-thegrate nate-thegrate left a comment

Choose a reason for hiding this comment

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

Flutter_LGTM

Thanks for your work here!

Feel free to take a look at the optional suggestion below if you'd like :)

@ksokolovskyi
Copy link
Contributor Author

Flutter_LGTM

Thanks for your work here!

Feel free to take a look at the optional suggestion below if you'd like :)

Thanks for the review!
Nice suggestions, so I applied them in the latest commit.

@ksokolovskyi ksokolovskyi force-pushed the add-widget-state-property-example-and-tests branch from a0655e1 to dfac98d Compare September 22, 2024 07:14
Copy link
Contributor

@bleroux bleroux left a comment

Choose a reason for hiding this comment

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

Thanks for adding this example and its tests.
Just some minor comments.

@ksokolovskyi
Copy link
Contributor Author

@bleroux thanks a lot for the review!
I applied your suggestions in the latest commit.
Could you please review PR again?

Copy link
Contributor

@bleroux bleroux left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for your contribution 🙏

Just one minor change required on the documentation and an optional one related to format.

/// material state properties when their value is needed.
///
/// {@tool dartpad}
/// This example shows how you can override the default text and icon
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noticed the "you", from the style guide it should be avoided.
Maybe something such as "This example shows how the default text and icon color (the "foreground color") of a [TextButton] can be overridden with a [WidgetStateProperty]."

Comment on lines 36 to 37
final TestGesture gesture =
await tester.createGesture(kind: PointerDeviceKind.mouse);
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like autoformat was applied there, feel free to change it or not.

@ksokolovskyi
Copy link
Contributor Author

@bleroux thanks for the additional suggestions, I have just applied them in the latest commit.

Copy link
Contributor

@bleroux bleroux left a comment

Choose a reason for hiding this comment

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

LGTM!

@nate-thegrate nate-thegrate added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 23, 2024
@auto-submit auto-submit bot merged commit 96ba3c5 into flutter:master Sep 24, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 24, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 24, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 24, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 24, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 24, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 25, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 25, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 25, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 25, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 25, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 25, 2024
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Sep 25, 2024
Roll Flutter from 4ca51a1 to 538e742 (40 revisions)

flutter/flutter@4ca51a1...538e742

2024-09-25 [email protected] Marks Linux build_aar_module_test to be unflaky (flutter/flutter#155349)
2024-09-25 [email protected] Roll Packages from 4926c0f to 7da2374 (3 revisions) (flutter/flutter#155701)
2024-09-25 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Normalize TabBarTheme (#155476)" (flutter/flutter#155698)
2024-09-25 [email protected] Normalize TabBarTheme (flutter/flutter#155476)
2024-09-25 [email protected] increase both linux & windows tool integration test shards (flutter/flutter#155631)
2024-09-25 [email protected] Roll Flutter Engine from c7cd559e483b to d6d5fdba6ae1 (1 revision) (flutter/flutter#155693)
2024-09-25 [email protected] Add WidgetStateBorderSide example and tests for it. (flutter/flutter#155559)
2024-09-25 [email protected] Roll Flutter Engine from b9dd7a39dd58 to c7cd559e483b (1 revision) (flutter/flutter#155686)
2024-09-25 [email protected] Roll Flutter Engine from 87c1667dfd1e to b9dd7a39dd58 (1 revision) (flutter/flutter#155684)
2024-09-25 [email protected] Roll Flutter Engine from 05211f9d2267 to 87c1667dfd1e (1 revision) (flutter/flutter#155681)
2024-09-25 [email protected] Roll Flutter Engine from 8d1eb7410b49 to 05211f9d2267 (1 revision) (flutter/flutter#155672)
2024-09-25 [email protected] Roll Flutter Engine from ad3dd0df0fe7 to 8d1eb7410b49 (2 revisions) (flutter/flutter#155662)
2024-09-25 [email protected] Roll Flutter Engine from 746ce6124844 to ad3dd0df0fe7 (2 revisions) (flutter/flutter#155653)
2024-09-25 [email protected] Add PrivacyInfo.xcprivacy to macOS plugin template (flutter/flutter#155570)
2024-09-25 [email protected] Roll Flutter Engine from 559f2ff31c74 to 746ce6124844 (14 revisions) (flutter/flutter#155648)
2024-09-25 [email protected] fix `SearchAnchor` disposing `SearchController` while it is still used (flutter/flutter#155219)
2024-09-25 [email protected] Roll pub packages (flutter/flutter#155640)
2024-09-24 [email protected] Preserve transform when using *Gradient:withOpacity (flutter/flutter#154908)
2024-09-24 [email protected] fixed keyboardDismissBehavior on scroll without a drag (flutter/flutter#154675)
2024-09-24 [email protected] Roll Flutter Engine from 7cd3d0b1bb2e to 559f2ff31c74 (3 revisions) (flutter/flutter#155629)
2024-09-24 [email protected] Roll Flutter Engine from 2a13c3a27e1f to 7cd3d0b1bb2e (4 revisions) (flutter/flutter#155625)
2024-09-24 [email protected] Misc docs cleanup and fixes (flutter/flutter#155501)
2024-09-24 [email protected] Roll Flutter Engine from dc44f95b7027 to 2a13c3a27e1f (1 revision) (flutter/flutter#155619)
2024-09-24 [email protected] Roll Flutter Engine from 2745b8797025 to dc44f95b7027 (1 revision) (flutter/flutter#155616)
2024-09-24 [email protected] Roll Flutter Engine from 8a54cc56d4b9 to 2745b8797025 (2 revisions) (flutter/flutter#155610)
2024-09-24 [email protected] Roll Flutter Engine from c07812775255 to 8a54cc56d4b9 (2 revisions) (flutter/flutter#155607)
2024-09-24 [email protected] Roll Packages from 9de72be to 4926c0f (4 revisions) (flutter/flutter#155605)
2024-09-24 [email protected] Fix some broken links in DAP readme (flutter/flutter#155600)
2024-09-24 [email protected] Roll Flutter Engine from 22e4f015cc99 to c07812775255 (2 revisions) (flutter/flutter#155599)
2024-09-24 [email protected] [flutter_tools] Fix encoded stderr in "dart.log" from debug adapter to client (flutter/flutter#155249)
2024-09-24 [email protected] Roll Flutter Engine from 309468cfd1bb to 22e4f015cc99 (2 revisions) (flutter/flutter#155591)
2024-09-24 [email protected] [native assets] Roll dependencies (flutter/flutter#155432)
2024-09-24 [email protected] Roll Flutter Engine from 4013dc28a48b to 309468cfd1bb (2 revisions) (flutter/flutter#155588)
2024-09-24 [email protected] Roll Flutter Engine from 8a5af19a43f3 to 4013dc28a48b (1 revision) (flutter/flutter#155585)
2024-09-24 [email protected] Roll Flutter Engine from 95c5a0940ad9 to 8a5af19a43f3 (10 revisions) (flutter/flutter#155583)
2024-09-24 [email protected] Add `WidgetStateProperty` example and tests for it. (flutter/flutter#155315)
2024-09-24 [email protected] Redo flutter engine flutter autoroll bd3d1990 485b 419c 8c55 b27e3eeb15ed 1727117767 (flutter/flutter#155579)
2024-09-23 [email protected] Roll Flutter Engine from 61f0a3fbabbe to 9bb0ece79ae2 (2 revisions) (flutter/flutter#155549)
2024-09-23 [email protected] Assert macOS framework artifact contains xcprivacy manifest (flutter/flutter#155189)
2024-09-23 [email protected] Roll Packages from f54fe93 to 9de72be (1 revision) (flutter/flutter#155540)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.
...
thejitenpatel pushed a commit to thejitenpatel/flutter that referenced this pull request Sep 26, 2024
This PR contributes to flutter#155313

### Description
- Adds example for `WidgetStateProperty`
- Adds tests for `examples/api/lib/widgets/widget_state/widget_state_property.0.dart`
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Dec 11, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants