mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
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 #146042, and also fixes #143488)