Skip to content

Conversation

@nate-thegrate
Copy link
Contributor

@nate-thegrate nate-thegrate commented Mar 30, 2024

This pull request implements enhanced enum features for the new WidgetState enum, in order to improve the developer experience when creating and using WidgetStateProperty objects.

WidgetState now has a .matchesSet() method:

// identical to "states.contains(WidgetState.error)"
final bool hasError = WidgetState.error.isSatisfiedBy(states);

This addition allows for wide variety of WidgetStateProperty objects to be constructed in a simple manner.



// before
final style = MaterialStateTextStyle.resolveWith((states) {
  if (states.contains(MaterialState.error)) {
    return const TextStyle(color: Colors.red);
  } else if (states.contains(MaterialState.focused)) {
    return const TextStyle(color: Colors.blue);
  }
  return const TextStyle(color: Colors.black);
});


// after
const style = WidgetStateTextStyle.fromMap({
  WidgetState.error:   TextStyle(color: Colors.red),
  WidgetState.focused: TextStyle(color: Colors.blue),
  WidgetState.any:     TextStyle(color: Colors.black),
});
// before
final color = MaterialStateProperty.resolveWith((states) {
  if (states.contains(MaterialState.focused)) {
    return Colors.blue;
  } else if (!states.contains(MaterialState.disabled)) {
    return Colors.black;
  }
  return null;
});


// after
final color = WidgetStateProperty<Color?>.fromMap({
  WidgetState.focused:   Colors.blue,
  ~WidgetState.disabled: Colors.black,
});
// before
const activeStates = [MaterialState.selected, MaterialState.focused, MaterialState.scrolledUnder];

final color = MaterialStateColor.resolveWith((states) {
  if (activeStates.any(states.contains)) {
    if (states.contains(MaterialState.hovered) {
      return Colors.blueAccent;
    }
    return Colors.blue;
  }
  return Colors.black;
});


// after
final active = WidgetState.selected | WidgetState.focused | WidgetState.scrolledUnder;

final color = WidgetStateColor.fromMap({
  active & WidgetState.hovered: Colors.blueAccent,
  active:  Colors.blue,
  ~active: Colors.black,
});

(fixes #146042, and also fixes #143488)

@github-actions github-actions bot added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. f: scrolling Viewports, list views, slivers, etc. and removed f: scrolling Viewports, list views, slivers, etc. a: text input Entering text in a text field or keyboard related problems f: material design flutter/packages/flutter/material repository. labels Mar 30, 2024
@cedvdb
Copy link
Contributor

cedvdb commented Apr 1, 2024

imo WidgetState.isError(states) would be more intuitive than WidgetState.error(states)

@nate-thegrate
Copy link
Contributor Author

nate-thegrate commented Apr 1, 2024

@cedvdb this is a really good point.

In order to make the mapping work, I added a "call" method there's no longer a call method, it is now .isSatisfiedBy() 🙂

Apologies for not making this change sooner!

Copy link
Contributor

@MitchellGoodwin MitchellGoodwin left a comment

Choose a reason for hiding this comment

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

Thank you for this PR! I think this is a great solution, that covers simple cases and complicated combinations well. I have some questions I'd like to talk about, but no complaints otherwise.

@goderbauer goderbauer requested a review from MitchellGoodwin May 7, 2024 22:01
Copy link
Contributor

@MitchellGoodwin MitchellGoodwin left a comment

Choose a reason for hiding this comment

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

LGTM with one follow up on naming. Thanks again for this PR, it's a great addition.

@nate-thegrate nate-thegrate added the autosubmit Merge PR when tree becomes green via auto submit App label May 9, 2024
@nate-thegrate nate-thegrate added autosubmit Merge PR when tree becomes green via auto submit App and removed autosubmit Merge PR when tree becomes green via auto submit App labels May 9, 2024
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label May 10, 2024
@auto-submit

This comment was marked as outdated.

@MitchellGoodwin
Copy link
Contributor

MitchellGoodwin commented May 10, 2024

The Google testing looks related. In one spot, there is a set of only a few WidgetStates.
Pseudo code it looks like this.

const certainStates = <WidgetState>{
   WidgetState.pressed,
   WidgetState.hovered,
}

It threw this error over this:
Error: Constant evaluation error: const certainStates = {

Context: The element 'WidgetState {}' conflicts with another existing element in the set.

  • 'WidgetState' is from 'package:flutter/src/widgets/widget_state.dart'
    WidgetState.focused,

Context: While analyzing: const interactiveStates = {

@MitchellGoodwin

This comment was marked as resolved.

@nate-thegrate

This comment was marked as resolved.

@MitchellGoodwin

This comment was marked as resolved.

@nate-thegrate

This comment was marked as resolved.

@nate-thegrate

This comment was marked as resolved.

@github-actions github-actions bot added the a: text input Entering text in a text field or keyboard related problems label Jun 21, 2024
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jul 18, 2024
@nate-thegrate
Copy link
Contributor Author

Third time's the charm! 🤞

@nate-thegrate nate-thegrate added the autosubmit Merge PR when tree becomes green via auto submit App label Jul 18, 2024
@auto-submit auto-submit bot merged commit db80f4e into flutter:master Jul 18, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 18, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 18, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 18, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 18, 2024
@nate-thegrate nate-thegrate deleted the widget-state-mapping branch July 18, 2024 23:04
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 19, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 19, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 19, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 19, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 19, 2024
LouiseHsu pushed a commit to flutter/packages that referenced this pull request Jul 20, 2024
Roll Flutter from 58068d8 to 7d5c1c0 (104 revisions)

flutter/flutter@58068d8...7d5c1c0

2024-07-19 [email protected] Enhances
intuitiveness of RawMagnifier's example (flutter/flutter#150308)
2024-07-19 [email protected]
Roll pub packages (flutter/flutter#151992)
2024-07-19 [email protected] Add test for
scrollbar.1.dart (flutter/flutter#151463)
2024-07-19 [email protected] Roll Flutter Engine from
ea1e53a4e810 to 969fb7abc449 (3 revisions) (flutter/flutter#152018)
2024-07-19 [email protected] docimports for rendering library
(flutter/flutter#151958)
2024-07-19 [email protected] Roll Flutter Engine from
b65c93ea948e to ea1e53a4e810 (2 revisions) (flutter/flutter#152012)
2024-07-19 [email protected] painting: drop deprecated
(exported) hashList and hashValues functions (flutter/flutter#151677)
2024-07-18 [email protected] Roll Flutter Engine from
766f7bed7185 to b65c93ea948e (2 revisions) (flutter/flutter#152004)
2024-07-18 [email protected] Update TESTOWNERS (flutter/flutter#151907)
2024-07-18 [email protected] chore: fix test name & add description of
tests (flutter/flutter#151959)
2024-07-18 [email protected] Roll Flutter Engine from
564ded4c4742 to 766f7bed7185 (2 revisions) (flutter/flutter#151998)
2024-07-18 [email protected] Fix SelectionArea scrolling
conflicts (flutter/flutter#151138)
2024-07-18 [email protected] Fix:
BaseTapAndDragGestureRecognizer should reset drag state after losing
gesture arena (flutter/flutter#151989)
2024-07-18 [email protected]
Roll pub packages (flutter/flutter#151975)
2024-07-18 [email protected] Roll Flutter Engine from
8bcf638eb893 to 564ded4c4742 (2 revisions) (flutter/flutter#151986)
2024-07-18 [email protected] Fix
WidgetStateTextStyle's doc (flutter/flutter#151935)
2024-07-18 [email protected] Roll Flutter Engine from
d58ba74250ce to 8bcf638eb893 (2 revisions) (flutter/flutter#151977)
2024-07-18 [email protected] Adds 3.22.3 changelog
(flutter/flutter#151974)
2024-07-18 [email protected] Roll Packages from
d03b1b4 to c7f0526 (8 revisions) (flutter/flutter#151971)
2024-07-18 [email protected] `WidgetState` mapping
(flutter/flutter#146043)
2024-07-18 [email protected] Fix AppBar doc to keep diagram next to its
description (flutter/flutter#151937)
2024-07-18 [email protected] Small fixes to Image docs: NNBD, and add a
cross-reference (flutter/flutter#151938)
2024-07-18 [email protected] Roll Flutter Engine from
b043fe447bb3 to d58ba74250ce (1 revision) (flutter/flutter#151964)
2024-07-18 [email protected]
Roll pub packages (flutter/flutter#151946)
2024-07-18 [email protected]
Roll pub packages (flutter/flutter#151904)
2024-07-18 [email protected] Roll Flutter Engine from
e3abca2d8105 to b043fe447bb3 (1 revision) (flutter/flutter#151942)
2024-07-18 [email protected] Roll Flutter Engine from
8073523b4623 to e3abca2d8105 (1 revision) (flutter/flutter#151936)
2024-07-18 [email protected] Roll Flutter Engine from
dfe22e3acc19 to 8073523b4623 (1 revision) (flutter/flutter#151934)
2024-07-18 [email protected] Roll Flutter Engine from
184c3f0de6b3 to dfe22e3acc19 (1 revision) (flutter/flutter#151930)
2024-07-18 [email protected] Roll Flutter Engine from
00f0f6b74da7 to 184c3f0de6b3 (1 revision) (flutter/flutter#151928)
2024-07-18 [email protected] Roll Flutter Engine from
d194a2f0e5da to 00f0f6b74da7 (1 revision) (flutter/flutter#151927)
2024-07-18 [email protected] Roll Flutter Engine from
a5a93bb80bd1 to d194a2f0e5da (3 revisions) (flutter/flutter#151925)
2024-07-17 [email protected] Roll Flutter Engine from
e9dc62074c2b to a5a93bb80bd1 (1 revision) (flutter/flutter#151918)
2024-07-17 [email protected] [web] use the new backlog Github project
in triage links (flutter/flutter#151920)
2024-07-17 [email protected] Update Flutter-Web-Triage.md
(flutter/flutter#151607)
2024-07-17 [email protected] Reland fix InputDecorator hint default
text style on M3 (flutter/flutter#150835)
2024-07-17 [email protected] Roll Flutter Engine from
39ee1a549581 to e9dc62074c2b (3 revisions) (flutter/flutter#151915)
2024-07-17 [email protected] Constrain `CupertinoContextMenu`
animation to safe area (flutter/flutter#151860)
2024-07-17 [email protected] Create
`CarouselView` widget - Part 2 (flutter/flutter#149775)
2024-07-17 [email protected] Roll Flutter Engine from
45b722b661f0 to 39ee1a549581 (3 revisions) (flutter/flutter#151905)
2024-07-17 [email protected] docs: Fix typo
in data driven fixes test folder section (flutter/flutter#151836)
2024-07-17 [email protected] Stop running flaky mac
tests in presubmit (flutter/flutter#151870)
2024-07-17 [email protected] Roll Flutter Engine from
7e2579634027 to 45b722b661f0 (1 revision) (flutter/flutter#151895)
2024-07-17 [email protected] fix(Flutter Web
App): fixes html lang typo (flutter/flutter#151866)
2024-07-17 [email protected] Delete `docs/engine`
directory (flutter/flutter#151616)
2024-07-17 [email protected] Make
`CupertinoSlidingSegmentedControl` type parameter non-null
(flutter/flutter#151803)
...
TytaniumDev pushed a commit to TytaniumDev/flutter that referenced this pull request Aug 7, 2024
This pull request implements [enhanced enum](https://dart.dev/language/enums#declaring-enhanced-enums) features for the new `WidgetState` enum, in order to improve the developer experience when creating and using `WidgetStateProperty` objects.

`WidgetState` now has a `.matchesSet()` method:

```dart
// identical to "states.contains(WidgetState.error)"
final bool hasError = WidgetState.error.isSatisfiedBy(states);
```

This addition allows for wide variety of `WidgetStateProperty` objects to be constructed in a simple manner.

<br><br>

```dart
// before
final style = MaterialStateTextStyle.resolveWith((states) {
  if (states.contains(MaterialState.error)) {
    return TextStyle(color: Colors.red);
  } else if (states.contains(MaterialState.focused)) {
    return TextStyle(color: Colors.blue);
  }
  return TextStyle(color: Colors.black);
});

// after
final style = WidgetStateTextStyle.fromMap({
  WidgetState.error:   TextStyle(color: Colors.red),
  WidgetState.focused: TextStyle(color: Colors.blue),
  WidgetState.any:     TextStyle(color: Colors.black), // "any" is a static const member, not an enum value
});
```

```dart
// before
final color = MaterialStateProperty.resolveWith((states) {
  if (states.contains(MaterialState.focused)) {
    return Colors.blue;
  } else if (!states.contains(MaterialState.disabled)) {
    return Colors.black;
  }
  return null;
});

// after
final color = WidgetStateProperty<Color?>.fromMap({
  WidgetState.focused:   Colors.blue,
  ~WidgetState.disabled: Colors.black,
});
```

```dart
// before
const activeStates = [MaterialState.selected, MaterialState.focused, MaterialState.scrolledUnder];

final color = MaterialStateColor.resolveWith((states) {
  if (activeStates.any(states.contains)) {
    if (states.contains(MaterialState.hovered) {
      return Colors.blueAccent;
    }
    return Colors.blue;
  }
  return Colors.black;
});

// after
final active = WidgetState.selected | WidgetState.focused | WidgetState.scrolledUnder;

final color = WidgetStateColor.fromMap({
  active & WidgetState.hovered: Colors.blueAccent,
  active:  Colors.blue,
  ~active: Colors.black,
});
```

<br>

(fixes flutter#146042, and also fixes flutter#143488)
Buchimi pushed a commit to Buchimi/flutter that referenced this pull request Sep 2, 2024
This pull request implements [enhanced enum](https://dart.dev/language/enums#declaring-enhanced-enums) features for the new `WidgetState` enum, in order to improve the developer experience when creating and using `WidgetStateProperty` objects.

`WidgetState` now has a `.matchesSet()` method:

```dart
// identical to "states.contains(WidgetState.error)"
final bool hasError = WidgetState.error.isSatisfiedBy(states);
```

This addition allows for wide variety of `WidgetStateProperty` objects to be constructed in a simple manner.

<br><br>

```dart
// before
final style = MaterialStateTextStyle.resolveWith((states) {
  if (states.contains(MaterialState.error)) {
    return TextStyle(color: Colors.red);
  } else if (states.contains(MaterialState.focused)) {
    return TextStyle(color: Colors.blue);
  }
  return TextStyle(color: Colors.black);
});

// after
final style = WidgetStateTextStyle.fromMap({
  WidgetState.error:   TextStyle(color: Colors.red),
  WidgetState.focused: TextStyle(color: Colors.blue),
  WidgetState.any:     TextStyle(color: Colors.black), // "any" is a static const member, not an enum value
});
```

```dart
// before
final color = MaterialStateProperty.resolveWith((states) {
  if (states.contains(MaterialState.focused)) {
    return Colors.blue;
  } else if (!states.contains(MaterialState.disabled)) {
    return Colors.black;
  }
  return null;
});

// after
final color = WidgetStateProperty<Color?>.fromMap({
  WidgetState.focused:   Colors.blue,
  ~WidgetState.disabled: Colors.black,
});
```

```dart
// before
const activeStates = [MaterialState.selected, MaterialState.focused, MaterialState.scrolledUnder];

final color = MaterialStateColor.resolveWith((states) {
  if (activeStates.any(states.contains)) {
    if (states.contains(MaterialState.hovered) {
      return Colors.blueAccent;
    }
    return Colors.blue;
  }
  return Colors.black;
});

// after
final active = WidgetState.selected | WidgetState.focused | WidgetState.scrolledUnder;

final color = WidgetStateColor.fromMap({
  active & WidgetState.hovered: Colors.blueAccent,
  active:  Colors.blue,
  ~active: Colors.black,
});
```

<br>

(fixes flutter#146042, and also fixes flutter#143488)
@nate-thegrate nate-thegrate mentioned this pull request Oct 14, 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 framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

5 participants