Now each Colors.foo constant is-a Color and a color swatch (#8833)

This commit is contained in:
Hans Muller
2017-03-17 15:24:54 -07:00
committed by GitHub
parent 5cf04b6141
commit 031e042eee
12 changed files with 832 additions and 515 deletions

View File

@@ -6,37 +6,37 @@ import 'package:flutter/material.dart';
const double kColorItemHeight = 48.0;
class ColorSwatch {
ColorSwatch({ this.name, this.colors, this.accentColors, this.threshold: 900});
class Palette {
Palette({ this.name, this.primary, this.accent, this.threshold: 900});
final String name;
final Map<int, Color> colors;
final Map<int, Color> accentColors;
final MaterialColor primary;
final MaterialAccentColor accent;
final int threshold; // titles for indices > threshold are white, otherwise black
bool get isValid => name != null && colors != null && threshold != null;
bool get isValid => name != null && primary != null && threshold != null;
}
final List<ColorSwatch> colorSwatches = <ColorSwatch>[
new ColorSwatch(name: 'RED', colors: Colors.red, accentColors: Colors.redAccent, threshold: 300),
new ColorSwatch(name: 'PINK', colors: Colors.pink, accentColors: Colors.pinkAccent, threshold: 200),
new ColorSwatch(name: 'PURPLE', colors: Colors.purple, accentColors: Colors.purpleAccent, threshold: 200),
new ColorSwatch(name: 'DEEP PURPLE', colors: Colors.deepPurple, accentColors: Colors.deepPurpleAccent, threshold: 200),
new ColorSwatch(name: 'INDIGO', colors: Colors.indigo, accentColors: Colors.indigoAccent, threshold: 200),
new ColorSwatch(name: 'BLUE', colors: Colors.blue, accentColors: Colors.blueAccent, threshold: 400),
new ColorSwatch(name: 'LIGHT BLUE', colors: Colors.lightBlue, accentColors: Colors.lightBlueAccent, threshold: 500),
new ColorSwatch(name: 'CYAN', colors: Colors.cyan, accentColors: Colors.cyanAccent, threshold: 600),
new ColorSwatch(name: 'TEAL', colors: Colors.teal, accentColors: Colors.tealAccent, threshold: 400),
new ColorSwatch(name: 'GREEN', colors: Colors.green, accentColors: Colors.greenAccent, threshold: 500),
new ColorSwatch(name: 'LIGHT GREEN', colors: Colors.lightGreen, accentColors: Colors.lightGreenAccent, threshold: 600),
new ColorSwatch(name: 'LIME', colors: Colors.lime, accentColors: Colors.limeAccent, threshold: 800),
new ColorSwatch(name: 'YELLOW', colors: Colors.yellow, accentColors: Colors.yellowAccent),
new ColorSwatch(name: 'AMBER', colors: Colors.amber, accentColors: Colors.amberAccent),
new ColorSwatch(name: 'ORANGE', colors: Colors.orange, accentColors: Colors.orangeAccent, threshold: 700),
new ColorSwatch(name: 'DEEP ORANGE', colors: Colors.deepOrange, accentColors: Colors.deepOrangeAccent, threshold: 400),
new ColorSwatch(name: 'BROWN', colors: Colors.brown, threshold: 200),
new ColorSwatch(name: 'GREY', colors: Colors.grey, threshold: 500),
new ColorSwatch(name: 'BLUE GREY', colors: Colors.blueGrey, threshold: 500),
final List<Palette> allPalettes = <Palette>[
new Palette(name: 'RED', primary: Colors.red, accent: Colors.redAccent, threshold: 300),
new Palette(name: 'PINK', primary: Colors.pink, accent: Colors.pinkAccent, threshold: 200),
new Palette(name: 'PURPLE', primary: Colors.purple, accent: Colors.purpleAccent, threshold: 200),
new Palette(name: 'DEEP PURPLE', primary: Colors.deepPurple, accent: Colors.deepPurpleAccent, threshold: 200),
new Palette(name: 'INDIGO', primary: Colors.indigo, accent: Colors.indigoAccent, threshold: 200),
new Palette(name: 'BLUE', primary: Colors.blue, accent: Colors.blueAccent, threshold: 400),
new Palette(name: 'LIGHT BLUE', primary: Colors.lightBlue, accent: Colors.lightBlueAccent, threshold: 500),
new Palette(name: 'CYAN', primary: Colors.cyan, accent: Colors.cyanAccent, threshold: 600),
new Palette(name: 'TEAL', primary: Colors.teal, accent: Colors.tealAccent, threshold: 400),
new Palette(name: 'GREEN', primary: Colors.green, accent: Colors.greenAccent, threshold: 500),
new Palette(name: 'LIGHT GREEN', primary: Colors.lightGreen, accent: Colors.lightGreenAccent, threshold: 600),
new Palette(name: 'LIME', primary: Colors.lime, accent: Colors.limeAccent, threshold: 800),
new Palette(name: 'YELLOW', primary: Colors.yellow, accent: Colors.yellowAccent),
new Palette(name: 'AMBER', primary: Colors.amber, accent: Colors.amberAccent),
new Palette(name: 'ORANGE', primary: Colors.orange, accent: Colors.orangeAccent, threshold: 700),
new Palette(name: 'DEEP ORANGE', primary: Colors.deepOrange, accent: Colors.deepOrangeAccent, threshold: 400),
new Palette(name: 'BROWN', primary: Colors.brown, threshold: 200),
new Palette(name: 'GREY', primary: Colors.grey, threshold: 500),
new Palette(name: 'BLUE GREY', primary: Colors.blueGrey, threshold: 500),
];
@@ -71,29 +71,33 @@ class ColorItem extends StatelessWidget {
}
}
class ColorSwatchTabView extends StatelessWidget {
ColorSwatchTabView({ Key key, this.swatch }) : super(key: key) {
assert(swatch != null && swatch.isValid);
class PaletteTabView extends StatelessWidget {
static const List<int> primaryKeys = const <int>[50, 100, 200, 300, 400, 500, 600, 700, 800, 900];
static const List<int> accentKeys = const <int>[100, 200, 400, 700];
PaletteTabView({ Key key, this.colors }) : super(key: key) {
assert(colors != null && colors.isValid);
}
final ColorSwatch swatch;
final Palette colors;
@override
Widget build(BuildContext context) {
final TextTheme textTheme = Theme.of(context).textTheme;
final TextStyle whiteTextStyle = textTheme.body1.copyWith(color: Colors.white);
final TextStyle blackTextStyle = textTheme.body1.copyWith(color: Colors.black);
final List<Widget> colorItems = swatch.colors.keys.map((int index) {
final List<Widget> colorItems = primaryKeys.map((int index) {
return new DefaultTextStyle(
style: index > swatch.threshold ? whiteTextStyle : blackTextStyle,
child: new ColorItem(index: index, color: swatch.colors[index]),
style: index > colors.threshold ? whiteTextStyle : blackTextStyle,
child: new ColorItem(index: index, color: colors.primary[index]),
);
}).toList();
if (swatch.accentColors != null) {
colorItems.addAll(swatch.accentColors.keys.map((int index) {
if (colors.accent != null) {
colorItems.addAll(accentKeys.map((int index) {
return new DefaultTextStyle(
style: index > swatch.threshold ? whiteTextStyle : blackTextStyle,
child: new ColorItem(index: index, color: swatch.accentColors[index], prefix: 'A'),
style: index > colors.threshold ? whiteTextStyle : blackTextStyle,
child: new ColorItem(index: index, color: colors.accent[index], prefix: 'A'),
);
}).toList());
}
@@ -111,19 +115,19 @@ class ColorsDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new DefaultTabController(
length: colorSwatches.length,
length: allPalettes.length,
child: new Scaffold(
appBar: new AppBar(
elevation: 0,
title: new Text('Colors'),
bottom: new TabBar(
isScrollable: true,
tabs: colorSwatches.map((ColorSwatch swatch) => new Tab(text: swatch.name)).toList(),
tabs: allPalettes.map((Palette swatch) => new Tab(text: swatch.name)).toList(),
),
),
body: new TabBarView(
children: colorSwatches.map((ColorSwatch swatch) {
return new ColorSwatchTabView(swatch: swatch);
children: allPalettes.map((Palette colors) {
return new PaletteTabView(colors: colors);
}).toList(),
),
),

View File

@@ -12,7 +12,7 @@ class IconsDemo extends StatefulWidget {
}
class IconsDemoState extends State<IconsDemo> {
static final List<Map<int, Color>> iconColorSwatches = <Map<int, Color>>[
static final List<MaterialColor> iconColors = <MaterialColor>[
Colors.red,
Colors.pink,
Colors.purple,
@@ -31,17 +31,17 @@ class IconsDemoState extends State<IconsDemo> {
Colors.deepOrange,
Colors.brown,
Colors.grey,
Colors.blueGrey
Colors.blueGrey,
];
int iconColorIndex = 8; // teal
double iconOpacity = 1.0;
Color get iconColor => iconColorSwatches[iconColorIndex][400];
Color get iconColor => iconColors[iconColorIndex];
void handleIconButtonPress() {
setState(() {
iconColorIndex = (iconColorIndex + 1) % iconColorSwatches.length;
iconColorIndex = (iconColorIndex + 1) % iconColors.length;
});
}

View File

@@ -14,7 +14,7 @@ class _Page {
_Page({ this.label, this.colors, this.icon });
final String label;
final Map<int, Color> colors;
final MaterialColor colors;
final IconData icon;
Color get labelColor => colors != null ? colors[300] : Colors.grey[300];

View File

@@ -31,7 +31,7 @@ class GalleryDrawerHeader extends StatefulWidget {
class _GalleryDrawerHeaderState extends State<GalleryDrawerHeader> {
bool _logoHasName = true;
bool _logoHorizontal = true;
Map<int, Color> _swatch = Colors.blue;
MaterialColor _logoColor = Colors.blue;
@override
Widget build(BuildContext context) {
@@ -43,7 +43,8 @@ class _GalleryDrawerHeaderState extends State<GalleryDrawerHeader> {
style: _logoHasName ? _logoHorizontal ? FlutterLogoStyle.horizontal
: FlutterLogoStyle.stacked
: FlutterLogoStyle.markOnly,
swatch: _swatch,
lightColor: _logoColor.shade400,
darkColor: _logoColor.shade900,
textColor: config.light ? const Color(0xFF616161) : const Color(0xFF9E9E9E),
),
duration: const Duration(milliseconds: 750),
@@ -62,22 +63,22 @@ class _GalleryDrawerHeaderState extends State<GalleryDrawerHeader> {
},
onDoubleTap: () {
setState(() {
final List<Map<int, Color>> options = <Map<int, Color>>[];
if (_swatch != Colors.blue)
options.addAll(<Map<int, Color>>[Colors.blue, Colors.blue, Colors.blue, Colors.blue, Colors.blue, Colors.blue, Colors.blue]);
if (_swatch != Colors.amber)
options.addAll(<Map<int, Color>>[Colors.amber, Colors.amber, Colors.amber]);
if (_swatch != Colors.red)
options.addAll(<Map<int, Color>>[Colors.red, Colors.red, Colors.red]);
if (_swatch != Colors.indigo)
options.addAll(<Map<int, Color>>[Colors.indigo, Colors.indigo, Colors.indigo]);
if (_swatch != Colors.pink)
options.addAll(<Map<int, Color>>[Colors.pink]);
if (_swatch != Colors.purple)
options.addAll(<Map<int, Color>>[Colors.purple]);
if (_swatch != Colors.cyan)
options.addAll(<Map<int, Color>>[Colors.cyan]);
_swatch = options[new math.Random().nextInt(options.length)];
final List<MaterialColor> options = <MaterialColor>[];
if (_logoColor != Colors.blue)
options.addAll(<MaterialColor>[Colors.blue, Colors.blue, Colors.blue, Colors.blue, Colors.blue, Colors.blue, Colors.blue]);
if (_logoColor != Colors.amber)
options.addAll(<MaterialColor>[Colors.amber, Colors.amber, Colors.amber]);
if (_logoColor != Colors.red)
options.addAll(<MaterialColor>[Colors.red, Colors.red, Colors.red]);
if (_logoColor != Colors.indigo)
options.addAll(<MaterialColor>[Colors.indigo, Colors.indigo, Colors.indigo]);
if (_logoColor != Colors.pink)
options.addAll(<MaterialColor>[Colors.pink]);
if (_logoColor != Colors.purple)
options.addAll(<MaterialColor>[Colors.purple]);
if (_logoColor != Colors.cyan)
options.addAll(<MaterialColor>[Colors.cyan]);
_logoColor = options[new math.Random().nextInt(options.length)];
});
}
)

View File

@@ -18,7 +18,7 @@ class _GesturePainter extends CustomPainter {
final double zoom;
final Offset offset;
final Map<int, Color> swatch;
final MaterialColor swatch;
final bool forward;
final bool scaleEnabled;
final bool tapEnabled;
@@ -71,7 +71,7 @@ class _GestureDemoState extends State<GestureDemo> {
double _previousZoom;
double _zoom = 1.0;
Map<int, Color> _swatch = Colors.blue;
MaterialColor _swatch = Colors.blue;
bool _forward = true;
bool _scaleEnabled = true;
@@ -106,28 +106,42 @@ class _GestureDemoState extends State<GestureDemo> {
void _handleColorChange() {
setState(() {
switch (_swatch) {
case Colors.blueGrey: _swatch = Colors.red; break;
case Colors.red: _swatch = Colors.pink; break;
case Colors.pink: _swatch = Colors.purple; break;
case Colors.purple: _swatch = Colors.deepPurple; break;
case Colors.deepPurple: _swatch = Colors.indigo; break;
case Colors.indigo: _swatch = Colors.blue; break;
case Colors.blue: _swatch = Colors.lightBlue; break;
case Colors.lightBlue: _swatch = Colors.cyan; break;
case Colors.cyan: _swatch = Colors.teal; break;
case Colors.teal: _swatch = Colors.green; break;
case Colors.green: _swatch = Colors.lightGreen; break;
case Colors.lightGreen: _swatch = Colors.lime; break;
case Colors.lime: _swatch = Colors.yellow; break;
case Colors.yellow: _swatch = Colors.amber; break;
case Colors.amber: _swatch = Colors.orange; break;
case Colors.orange: _swatch = Colors.deepOrange; break;
case Colors.deepOrange: _swatch = Colors.brown; break;
case Colors.brown: _swatch = Colors.grey; break;
case Colors.grey: _swatch = Colors.blueGrey; break;
default: assert(false);
}
if (_swatch == Colors.blueGrey)
_swatch = Colors.red;
else if (_swatch == Colors.red)
_swatch = Colors.pink;
else if (_swatch == Colors.pink)
_swatch = Colors.purple;
else if (_swatch == Colors.purple)
_swatch = Colors.deepPurple;
else if (_swatch == Colors.deepPurple)
_swatch = Colors.indigo;
else if (_swatch == Colors.indigo)
_swatch = Colors.blue;
else if (_swatch == Colors.blue)
_swatch = Colors.lightBlue;
else if (_swatch == Colors.lightBlue)
_swatch = Colors.cyan;
else if (_swatch == Colors.teal)
_swatch = Colors.green;
else if (_swatch == Colors.green)
_swatch = Colors.lightGreen;
else if (_swatch == Colors.lightGreen)
_swatch = Colors.lime;
else if (_swatch == Colors.lime)
_swatch = Colors.yellow;
else if (_swatch == Colors.yellow)
_swatch = Colors.amber;
else if (_swatch == Colors.amber)
_swatch = Colors.orange;
else if (_swatch == Colors.orange)
_swatch = Colors.deepOrange;
else if (_swatch == Colors.deepOrange)
_swatch = Colors.brown;
else if (_swatch == Colors.brown)
_swatch = Colors.grey;
else if (_swatch == Colors.grey)
_swatch = Colors.blueGrey;
});
}