diff --git a/packages/flutter/lib/src/material/colors.dart b/packages/flutter/lib/src/material/colors.dart index 7186c0b567..45d4b89395 100644 --- a/packages/flutter/lib/src/material/colors.dart +++ b/packages/flutter/lib/src/material/colors.dart @@ -2,41 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:ui' show Color, hashValues; +import 'dart:ui' show Color; -/// A color that has a small table of related colors called a "swatch". -/// -/// See also: -/// -/// * [MaterialColor] and [MaterialAccentColor], which define material design -/// primary and accent color swatches. -/// * [Colors], which defines all of the standard material design colors. -class ColorSwatch extends Color { - /// Creates a color that has a small table of related colors called a "swatch". - const ColorSwatch(int primary, this._swatch) : super(primary); - - final Map _swatch; - - /// Returns an element of the swatch table. - Color operator [](int index) => _swatch[index]; - - @override - bool operator ==(dynamic other) { - if (identical(this, other)) - return true; - if (other.runtimeType != runtimeType) - return false; - final ColorSwatch typedOther = other; - return super==(other) && _swatch == typedOther._swatch; - } - - @override - int get hashCode => hashValues(runtimeType, value, _swatch); - - @override - String toString() => '$runtimeType(primary value: ${super.toString()})'; - -} +import 'package:flutter/painting.dart'; /// Defines a single color as well a color swatch with ten shades of the color. /// @@ -47,39 +15,39 @@ class ColorSwatch extends Color { /// See also: /// /// * [Colors], which defines all of the standard material colors. -class MaterialColor extends ColorSwatch { +class MaterialColor extends ColorSwatch { /// Creates a color swatch with a variety of shades. const MaterialColor(int primary, Map swatch) : super(primary, swatch); /// The lightest shade. - Color get shade50 => _swatch[50]; + Color get shade50 => this[50]; /// The second lightest shade. - Color get shade100 => _swatch[100]; + Color get shade100 => this[100]; /// The third lightest shade. - Color get shade200 => _swatch[200]; + Color get shade200 => this[200]; /// The fourth lightest shade. - Color get shade300 => _swatch[300]; + Color get shade300 => this[300]; /// The fifth lightest shade. - Color get shade400 => _swatch[400]; + Color get shade400 => this[400]; /// The default shade. - Color get shade500 => _swatch[500]; + Color get shade500 => this[500]; /// The fourth darkest shade. - Color get shade600 => _swatch[600]; + Color get shade600 => this[600]; /// The third darkest shade. - Color get shade700 => _swatch[700]; + Color get shade700 => this[700]; /// The second darkest shade. - Color get shade800 => _swatch[800]; + Color get shade800 => this[800]; /// The darkest shade. - Color get shade900 => _swatch[900]; + Color get shade900 => this[900]; } /// Defines a single accent color as well a swatch of four shades of the @@ -94,25 +62,25 @@ class MaterialColor extends ColorSwatch { /// /// * [Colors], which defines all of the standard material colors. /// * -class MaterialAccentColor extends ColorSwatch { +class MaterialAccentColor extends ColorSwatch { /// Creates a color swatch with a variety of shades appropriate for accent /// colors. const MaterialAccentColor(int primary, Map swatch) : super(primary, swatch); /// The lightest shade. - Color get shade50 => _swatch[50]; + Color get shade50 => this[50]; /// The second lightest shade. - Color get shade100 => _swatch[100]; + Color get shade100 => this[100]; /// The default shade. - Color get shade200 => _swatch[200]; + Color get shade200 => this[200]; /// The second darkest shade. - Color get shade400 => _swatch[400]; + Color get shade400 => this[400]; /// The darkest shade. - Color get shade700 => _swatch[700]; + Color get shade700 => this[700]; } /// [Color] and [ColorSwatch] constants which represent Material design's @@ -130,7 +98,7 @@ class MaterialAccentColor extends ColorSwatch { /// Colors.green[400] // Selects a mid-range green. /// ``` /// -/// Each ColorSwatch constant is a color and can used directly. For example +/// Each [ColorSwatch] constant is a color and can used directly. For example /// /// ```dart /// new Container( diff --git a/packages/flutter/lib/src/painting/colors.dart b/packages/flutter/lib/src/painting/colors.dart index b76a4ae213..be9748dc41 100644 --- a/packages/flutter/lib/src/painting/colors.dart +++ b/packages/flutter/lib/src/painting/colors.dart @@ -172,3 +172,39 @@ class HSVColor { @override String toString() => "HSVColor($alpha, $hue, $saturation, $value)"; } + +/// A color that has a small table of related colors called a "swatch". +/// +/// The table is indexed by values of type `T`. +/// +/// See also: +/// +/// * [MaterialColor] and [MaterialAccentColor], which define material design +/// primary and accent color swatches. +/// * [Colors], which defines all of the standard material design colors. +class ColorSwatch extends Color { + /// Creates a color that has a small table of related colors called a "swatch". + const ColorSwatch(int primary, this._swatch) : super(primary); + + @protected + final Map _swatch; + + /// Returns an element of the swatch table. + Color operator [](T index) => _swatch[index]; + + @override + bool operator ==(dynamic other) { + if (identical(this, other)) + return true; + if (other.runtimeType != runtimeType) + return false; + final ColorSwatch typedOther = other; + return super==(other) && _swatch == typedOther._swatch; + } + + @override + int get hashCode => hashValues(runtimeType, value, _swatch); + + @override + String toString() => '$runtimeType(primary value: ${super.toString()})'; +} diff --git a/packages/flutter/test/painting/colors_test.dart b/packages/flutter/test/painting/colors_test.dart index a1a4f0ac0d..213b8ed7da 100644 --- a/packages/flutter/test/painting/colors_test.dart +++ b/packages/flutter/test/painting/colors_test.dart @@ -38,4 +38,28 @@ void main() { expect(green.toColor(), equals(const Color.fromARGB(0xFF, 0x00, 0xFF, 0x00))); expect(blue.toColor(), equals(const Color.fromARGB(0xFF, 0x00, 0x00, 0xFF))); }); + + test('ColorSwatch test', () { + final int color = 0xFF027223; + final ColorSwatch greens1 = new ColorSwatch( + color, const { + '2259 C': const Color(0xFF027223), + '2273 C': const Color(0xFF257226), + '2426 XGC': const Color(0xFF00932F), + '7732 XGC': const Color(0xFF007940), + }, + ); + final ColorSwatch greens2 = new ColorSwatch( + color, const { + '2259 C': const Color(0xFF027223), + '2273 C': const Color(0xFF257226), + '2426 XGC': const Color(0xFF00932F), + '7732 XGC': const Color(0xFF007940), + }, + ); + expect(greens1, greens2); + expect(greens1.hashCode, greens2.hashCode); + expect(greens1['2259 C'], const Color(0xFF027223)); + expect(greens1.value, 0xFF027223); + }); }