From af21d9f5b360dee1343386bcc02f6ff5bd6d48fa Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:20:32 -0700 Subject: [PATCH] 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 --- packages/flutter/lib/src/painting/colors.dart | 9 ++++++--- packages/flutter/test/painting/colors_test.dart | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/painting/colors.dart b/packages/flutter/lib/src/painting/colors.dart index 5a306590a3c..a897c0b33d6 100644 --- a/packages/flutter/lib/src/painting/colors.dart +++ b/packages/flutter/lib/src/painting/colors.dart @@ -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 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 _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 get keys => _swatch.keys; @override bool operator ==(Object other) { diff --git a/packages/flutter/test/painting/colors_test.dart b/packages/flutter/test/painting/colors_test.dart index 470601d0caf..97917619341 100644 --- a/packages/flutter/test/painting/colors_test.dart +++ b/packages/flutter/test/painting/colors_test.dart @@ -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', () {