Skip to content

Commit 3082364

Browse files
committed
get color from name
1 parent f08be59 commit 3082364

File tree

7 files changed

+53
-24
lines changed

7 files changed

+53
-24
lines changed

lib/flutter_colorpicker.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
library flutter_colorpicker;
22

3-
export 'package:flutter_colorpicker/src/colorpicker.dart';
4-
export 'package:flutter_colorpicker/src/material_picker.dart';
5-
export 'package:flutter_colorpicker/src/block_picker.dart';
6-
export 'package:flutter_colorpicker/src/palette.dart';
7-
export 'package:flutter_colorpicker/src/utils.dart';
3+
export 'src/colorpicker.dart';
4+
export 'src/material_picker.dart';
5+
export 'src/block_picker.dart';
6+
export 'src/palette.dart';
7+
export 'src/utils.dart';

lib/src/block_picker.dart

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
library block_colorpicker;
44

55
import 'package:flutter/material.dart';
6-
import 'package:flutter_colorpicker/src/utils.dart';
6+
import 'utils.dart';
77

88
/// Child widget for layout builder.
99
typedef PickerItem = Widget Function(Color color);
@@ -90,7 +90,7 @@ class BlockPicker extends StatefulWidget {
9090
this.itemBuilder = _defaultItemBuilder,
9191
}) : super(key: key);
9292

93-
final Color pickerColor;
93+
final Color? pickerColor;
9494
final ValueChanged<Color> onColorChanged;
9595
final List<Color> availableColors;
9696
final bool useInShowDialog;
@@ -102,7 +102,7 @@ class BlockPicker extends StatefulWidget {
102102
}
103103

104104
class _BlockPickerState extends State<BlockPicker> {
105-
late Color _currentColor;
105+
Color? _currentColor;
106106

107107
@override
108108
void initState() {
@@ -122,8 +122,10 @@ class _BlockPickerState extends State<BlockPicker> {
122122
widget.availableColors,
123123
(Color color) => widget.itemBuilder(
124124
color,
125-
(_currentColor.value == color.value) &&
126-
(widget.useInShowDialog ? true : widget.pickerColor.value == color.value),
125+
(_currentColor != null && (widget.useInShowDialog ? true : widget.pickerColor != null))
126+
? (_currentColor?.value == color.value) &&
127+
(widget.useInShowDialog ? true : widget.pickerColor?.value == color.value)
128+
: false,
127129
() => changeColor(color),
128130
),
129131
);
@@ -142,7 +144,7 @@ class MultipleChoiceBlockPicker extends StatefulWidget {
142144
this.itemBuilder = _defaultItemBuilder,
143145
}) : super(key: key);
144146

145-
final List<Color> pickerColors;
147+
final List<Color>? pickerColors;
146148
final ValueChanged<List<Color>> onColorsChanged;
147149
final List<Color> availableColors;
148150
final bool useInShowDialog;
@@ -154,7 +156,7 @@ class MultipleChoiceBlockPicker extends StatefulWidget {
154156
}
155157

156158
class _MultipleChoiceBlockPickerState extends State<MultipleChoiceBlockPicker> {
157-
late List<Color> _currentColors;
159+
List<Color>? _currentColors;
158160

159161
@override
160162
void initState() {
@@ -163,8 +165,12 @@ class _MultipleChoiceBlockPickerState extends State<MultipleChoiceBlockPicker> {
163165
}
164166

165167
void toggleColor(Color color) {
166-
setState(() => _currentColors.contains(color) ? _currentColors.remove(color) : _currentColors.add(color));
167-
widget.onColorsChanged(_currentColors);
168+
setState(() {
169+
if (_currentColors != null) {
170+
_currentColors!.contains(color) ? _currentColors!.remove(color) : _currentColors!.add(color);
171+
}
172+
});
173+
widget.onColorsChanged(_currentColors ?? []);
168174
}
169175

170176
@override
@@ -174,7 +180,9 @@ class _MultipleChoiceBlockPickerState extends State<MultipleChoiceBlockPicker> {
174180
widget.availableColors,
175181
(Color color) => widget.itemBuilder(
176182
color,
177-
_currentColors.contains(color) && (widget.useInShowDialog ? true : widget.pickerColors.contains(color)),
183+
(_currentColors != null && (widget.useInShowDialog ? true : widget.pickerColors != null))
184+
? _currentColors!.contains(color) && (widget.useInShowDialog ? true : widget.pickerColors!.contains(color))
185+
: false,
178186
() => toggleColor(color),
179187
),
180188
);

lib/src/colorpicker.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
library hsv_picker;
66

77
import 'package:flutter/material.dart';
8-
import 'package:flutter_colorpicker/src/palette.dart';
9-
import 'package:flutter_colorpicker/src/utils.dart';
8+
import 'palette.dart';
9+
import 'utils.dart';
1010

1111
/// The default layout of Color Picker.
1212
class ColorPicker extends StatefulWidget {
@@ -196,9 +196,8 @@ class _ColorPickerState extends State<ColorPicker> {
196196
// is provided, but it may help to calm the Dart analyzer in the future.
197197
if (widget.hexInputController == null) return;
198198
// If a user is inserting/typing any text — try to get the color value from it,
199-
final Color? color = colorFromHex(widget.hexInputController!.text,
200-
// and interpret its transparency, dependent on the widget's settings.
201-
enableAlpha: widget.enableAlpha);
199+
// and interpret its transparency, dependent on the widget's settings.
200+
final Color? color = colorFromHex(widget.hexInputController!.text, enableAlpha: widget.enableAlpha);
202201
// If it's the valid color:
203202
if (color != null) {
204203
// set it as the current color and

lib/src/colors.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,9 @@ const Map<String, Color> x11Colors = {
161161
'yellow': Color(0xffffff00),
162162
'yellowgreen': Color(0xff9acd32),
163163
};
164+
165+
Color? colorFromName(String val) => x11Colors[val.trim().replaceAll(' ', '').toLowerCase()];
166+
167+
extension ColorExtension on String {
168+
Color? toColor() => colorFromName(this);
169+
}

lib/src/material_picker.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ library material_colorpicker;
44

55
import 'package:flutter/gestures.dart';
66
import 'package:flutter/material.dart';
7-
import 'package:flutter_colorpicker/src/utils.dart';
7+
import 'utils.dart';
88

99
// The Color Picker which contains Material Design Color Palette.
1010
class MaterialPicker extends StatefulWidget {

lib/src/palette.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'dart:math';
66
import 'package:flutter/gestures.dart';
77
import 'package:flutter/material.dart';
88
import 'package:flutter/services.dart';
9-
import 'package:flutter_colorpicker/src/utils.dart';
9+
import 'utils.dart';
1010

1111
/// Palette types for color picker area widget.
1212
enum PaletteType {

lib/src/utils.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
33
import 'dart:math';
44
import 'package:flutter/painting.dart';
5+
import 'colors.dart';
56

67
/// Check if is good condition to use white foreground color by passing
78
/// the background color, and optional bias.
89
///
910
/// Reference:
10-
///
11+
///
1112
/// Old: https://www.w3.org/TR/WCAG20-TECHS/G18.html
12-
///
13+
///
1314
/// New: https://github.com/mchome/flutter_statusbarcolor/issues/40
1415
bool useWhiteForeground(Color backgroundColor, {double bias = 0.0}) {
1516
// Old:
@@ -197,3 +198,18 @@ String colorToHex(
197198

198199
// Shorthand for padLeft of RadixString, DRY.
199200
String _padRadix(int value) => value.toRadixString(16).padLeft(2, '0');
201+
202+
// Extension for String
203+
extension ColorExtension1 on String {
204+
Color? toColor() {
205+
Color? color = colorFromName(this);
206+
if (color != null) return color;
207+
return colorFromHex(this);
208+
}
209+
}
210+
211+
// Extension from Color
212+
extension ColorExtension2 on Color {
213+
String toHexString({bool includeHashSign = false, bool enableAlpha = true, bool toUpperCase = true}) =>
214+
colorToHex(this, includeHashSign: false, enableAlpha: true, toUpperCase: true);
215+
}

0 commit comments

Comments
 (0)