Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color code for numbers 0-9 and generate color for other characters using hex code #13

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions lib/src/colorized_text_avatar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,33 @@ class TextAvatar extends StatelessWidget {
Widget build(BuildContext context) {
shape = (shape == null) ? Shape.Rectangle : shape;
size = (size == null || size! < 32.0) ? 48.0 : size;
backgroundColor = backgroundColor == null ? _colorBackgroundConfig() : backgroundColor;
backgroundColor =
backgroundColor == null ? _colorBackgroundConfig() : backgroundColor;
textColor = _colorTextConfig();
return _textDisplay();
}

Color _colorBackgroundConfig() {
if (RegExp(r'[A-Z]|').hasMatch(
_textConfiguration(),
)) {
backgroundColor =
colorData[_textConfiguration()[0].toLowerCase().toString()];
Color? color = colorData[_textConfiguration()[0].toLowerCase().toString()];
if (color == null) {
color = buildBackgroundColor();
}
return backgroundColor!;

return color;
}

Color buildBackgroundColor() {
String newText = text == null ? '?' : _toString(value: text);
String hexCode = newText
.toString()
.codeUnits
.map((e) => e.toRadixString(16))
.join()
.toUpperCase();

String color = 'FF${hexCode.substring(0, 6)}';

return Color(int.parse(color, radix: 16));
}

Color _colorTextConfig() {
Expand Down
11 changes: 10 additions & 1 deletion lib/src/constants/colors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,14 @@ var colorData = {
"w": Color.fromRGBO(194, 194, 194, 1),
"x": Color.fromRGBO(124, 222, 235, 1),
"y": Color.fromRGBO(188, 170, 164, 1),
"z": Color.fromRGBO(173, 214, 125, 1),
"1": Color.fromRGBO(229, 115, 115, 1),
"2": Color.fromRGBO(67, 160, 71, 1),
"3": Color.fromRGBO(255, 179, 0, 1),
"4": Color.fromRGBO(244, 81, 30, 1),
"5": Color.fromRGBO(228, 72, 215, 1),
"6": Color.fromRGBO(255, 110, 64, 1),
"7": Color.fromRGBO(0, 121, 107, 1),
"8": Color.fromRGBO(158, 158, 158, 1),
"9": Color.fromRGBO(106, 27, 154, 1),
"0": Color.fromRGBO(226, 82, 97, 1),
};