Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.

Commit 4e37b17

Browse files
author
eightgran
committed
Add LitUserIcon
1 parent 31762e5 commit 4e37b17

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Add `addListener` member on `LitSnackbarController` to call custom callbacks on each action.
1111
- Replace `Column` with `Stack` to display grouped notifications on `LitNotificationContainer`.
1212
- Add customization options on `LitBottomNavigation`.
13+
- Add `LitUserIcon` to display the username as an acronym on a colored background.
1314

1415
## 1.0.5
1516

example/lib/main.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,26 @@ class __ExampleScreenState extends State<_ExampleScreen> {
319319
),
320320
)
321321
: SizedBox(),
322+
Padding(
323+
padding: const EdgeInsets.symmetric(
324+
vertical: 32.0,
325+
),
326+
child: LitUserIcon(
327+
primaryColor: colorPickerBtnColor,
328+
onPressed: () => {
329+
showDialog(
330+
context: context,
331+
builder: (context) {
332+
return _LitColorPickerDialogImpl(
333+
color: colorPickerBtnColor,
334+
onApply: (c) {
335+
_setColorPickerColor(c);
336+
},
337+
);
338+
})
339+
},
340+
),
341+
),
322342
_ButtonList(
323343
darkMode: darkMode,
324344
addNotification: _addNotification,

lib/containers.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ export 'src/widgets/containers/lit_footer.dart';
2020
export 'src/widgets/containers/clean_ink_well.dart';
2121
export 'src/widgets/containers/indexed_page_view.dart';
2222
export 'src/widgets/containers/lit_divider.dart';
23+
export 'src/widgets/containers/lit_user_icon.dart';
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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

Comments
 (0)