mirror of
https://github.com/flutter/samples.git
synced 2026-03-22 04:17:50 +00:00
Fixed hex color picker in material 3 demo (#2578)
This pull request fixes the problem where the color picker wasn't handling hex values correctly. Fixes #2577
This commit is contained in:
@@ -66,14 +66,13 @@ class _ColorBoxState extends State<ColorBox> {
|
||||
onPressed: () async {
|
||||
final messenger = ScaffoldMessenger.of(context);
|
||||
// Copy color as hex to clipboard
|
||||
String hex = '#';
|
||||
final c = widget.color;
|
||||
// Will change from int 0-255 to double 0.0-1.0 in 3.26+
|
||||
// The properties also change from red/green/blue to r/g/b
|
||||
// hex += (c.[r g b] * 255.0).round().toRadixString(16).padLeft(2, '0');
|
||||
hex += c.r.round().toRadixString(16).padLeft(2, '0');
|
||||
hex += c.g.round().toRadixString(16).padLeft(2, '0');
|
||||
hex += c.b.round().toRadixString(16).padLeft(2, '0');
|
||||
|
||||
final hex =
|
||||
'#${_colorChannelToHex(c.r)}'
|
||||
'${_colorChannelToHex(c.g)}'
|
||||
'${_colorChannelToHex(c.b)}';
|
||||
|
||||
final data = ClipboardData(text: hex);
|
||||
await Clipboard.setData(data);
|
||||
messenger.hideCurrentSnackBar();
|
||||
@@ -90,3 +89,8 @@ class _ColorBoxState extends State<ColorBox> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _colorChannelToHex(double value) {
|
||||
final intVal = (value * 255).round();
|
||||
return intVal.toRadixString(16).padLeft(2, '0');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user