Added .keys to ColorSwatch (#155262)

This addresses the issue that ColorSwatch has operator[], but no way to know what are valid inputs.

issue: https://github.com/flutter/flutter/issues/155113
This commit is contained in:
gaaclarke 2024-09-16 14:20:32 -07:00 committed by GitHub
parent 411b2ae1bd
commit af21d9f5b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 3 deletions

View File

@ -398,7 +398,7 @@ class HSLColor {
/// A color that has a small table of related colors called a "swatch".
///
/// The table is indexed by values of type `T`.
/// The table is accessed by key values of type `T`.
///
/// See also:
///
@ -413,14 +413,17 @@ class ColorSwatch<T> extends Color {
/// The `primary` argument should be the 32 bit ARGB value of one of the
/// values in the swatch, as would be passed to the [Color.new] constructor
/// for that same color, and as is exposed by [value]. (This is distinct from
/// the specific index of the color in the swatch.)
/// the key of any color in the swatch.)
const ColorSwatch(super.primary, this._swatch);
@protected
final Map<T, Color> _swatch;
/// Returns an element of the swatch table.
Color? operator [](T index) => _swatch[index];
Color? operator [](T key) => _swatch[key];
/// Returns the valid keys for accessing operator[].
Iterable<T> get keys => _swatch.keys;
@override
bool operator ==(Object other) {

View File

@ -436,6 +436,7 @@ void main() {
expect(greens1.hashCode, greens2.hashCode);
expect(greens1['2259 C'], const Color(0xFF027223));
expect(greens1.value, 0xFF027223);
expect(listEquals(greens1.keys.toList(), greens2.keys.toList()), isTrue);
});
test('ColorSwatch.lerp', () {