|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:lit_ui_kit/containers.dart'; |
| 3 | +import 'package:lit_ui_kit/styles.dart'; |
| 4 | +import 'package:lit_ui_kit/text.dart'; |
| 5 | + |
| 6 | +/// A widget displaying an username on a colored background. |
| 7 | +/// |
| 8 | +/// The username will be converted into an acronym to keep the displayed text |
| 9 | +/// as short as possible. The username is split on each space character. |
| 10 | +class LitUserIcon extends StatefulWidget { |
| 11 | + /// Creates a [LitUserIcon]. |
| 12 | + const LitUserIcon({ |
| 13 | + Key? key, |
| 14 | + this.size = 96.0, |
| 15 | + this.primaryColor = Colors.white, |
| 16 | + this.username = "Some One", |
| 17 | + this.boxShadow = const [ |
| 18 | + const BoxShadow( |
| 19 | + blurRadius: 4.0, |
| 20 | + color: Colors.black12, |
| 21 | + offset: Offset( |
| 22 | + -3.0, |
| 23 | + 2.0, |
| 24 | + ), |
| 25 | + spreadRadius: -1.0, |
| 26 | + ) |
| 27 | + ], |
| 28 | + this.borderRadius = const BorderRadius.all( |
| 29 | + const Radius.circular(38.0), |
| 30 | + ), |
| 31 | + this.fontSize = 34.0, |
| 32 | + this.onPressed, |
| 33 | + this.margin = const EdgeInsets.symmetric( |
| 34 | + horizontal: 8.0, |
| 35 | + ), |
| 36 | + }) : super(key: key); |
| 37 | + |
| 38 | + /// The size which accounts for both, width and height. |
| 39 | + final double size; |
| 40 | + |
| 41 | + /// The primary color used to generate the gradient background. |
| 42 | + final Color primaryColor; |
| 43 | + |
| 44 | + /// The displayed username, whose text is converted into an acronym. |
| 45 | + final String username; |
| 46 | + |
| 47 | + /// The container's box shadow. |
| 48 | + final List<BoxShadow> boxShadow; |
| 49 | + |
| 50 | + /// The container's border radius. |
| 51 | + final BorderRadius borderRadius; |
| 52 | + |
| 53 | + /// Called once the container has been pressed. |
| 54 | + final void Function()? onPressed; |
| 55 | + |
| 56 | + /// The text font size. |
| 57 | + final double fontSize; |
| 58 | + |
| 59 | + /// The margin surrounding the text. |
| 60 | + final EdgeInsets margin; |
| 61 | + |
| 62 | + @override |
| 63 | + __UserIconState createState() => __UserIconState(); |
| 64 | +} |
| 65 | + |
| 66 | +class __UserIconState extends State<LitUserIcon> { |
| 67 | + /// Returns a stylized user color. |
| 68 | + Color get _userColor { |
| 69 | + Color uColor = widget.primaryColor; |
| 70 | + Color contrastColor = Color(0xFFCDCDCD); |
| 71 | + int alpha = uColor.alpha; |
| 72 | + int red = (uColor.red * 0.8).floor(); |
| 73 | + int green = (uColor.green * 0.8).floor(); |
| 74 | + int blue = (uColor.blue * 0.8).floor(); |
| 75 | + Color desatColor = Color.fromARGB(alpha, red, green, blue); |
| 76 | + return Color.lerp(contrastColor, desatColor, 0.3)!; |
| 77 | + } |
| 78 | + |
| 79 | + /// Returns the initials of the user derived by the username. |
| 80 | + String get _usernameInitials { |
| 81 | + String initals = ""; |
| 82 | + List<String> names = "${widget.username}".split(" "); |
| 83 | + |
| 84 | + // Add the first character of each substring (name elements). |
| 85 | + for (String nameElement in names) { |
| 86 | + if (initals.length < 3) { |
| 87 | + initals = initals + |
| 88 | + // If there is only one element and the element's length |
| 89 | + // does not exceed three characters, adopt the whole element's |
| 90 | + // content. |
| 91 | + ((nameElement.length < 3 && names.length == 1) |
| 92 | + ? nameElement |
| 93 | + // Else, shorten it. |
| 94 | + : nameElement.substring(0, 1)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return initals; |
| 99 | + } |
| 100 | + |
| 101 | + Color get _textColor { |
| 102 | + return _userColor.computeLuminance() >= 0.5 |
| 103 | + ? Colors.white |
| 104 | + : Color(0xFF888888); |
| 105 | + } |
| 106 | + |
| 107 | + void _onTap() { |
| 108 | + if (widget.onPressed != null) { |
| 109 | + widget.onPressed!(); |
| 110 | + } else { |
| 111 | + return; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @override |
| 116 | + Widget build(BuildContext context) { |
| 117 | + return CleanInkWell( |
| 118 | + onTap: _onTap, |
| 119 | + child: Container( |
| 120 | + height: widget.size, |
| 121 | + width: widget.size, |
| 122 | + decoration: BoxDecoration( |
| 123 | + boxShadow: widget.boxShadow, |
| 124 | + gradient: LinearGradient( |
| 125 | + begin: Alignment.topRight, |
| 126 | + end: Alignment.bottomLeft, |
| 127 | + colors: [ |
| 128 | + _userColor, |
| 129 | + Colors.white, |
| 130 | + ]), |
| 131 | + borderRadius: widget.borderRadius, |
| 132 | + ), |
| 133 | + child: Center( |
| 134 | + child: Padding( |
| 135 | + padding: widget.margin, |
| 136 | + child: ClippedText( |
| 137 | + _usernameInitials, |
| 138 | + style: LitSansSerifStyles.header5.copyWith( |
| 139 | + fontSize: widget.fontSize, |
| 140 | + color: _textColor, |
| 141 | + fontWeight: FontWeight.w700, |
| 142 | + letterSpacing: 1.0, |
| 143 | + ), |
| 144 | + ), |
| 145 | + ), |
| 146 | + ), |
| 147 | + ), |
| 148 | + ); |
| 149 | + } |
| 150 | +} |
0 commit comments