Skip to content

Commit

Permalink
Merge pull request #2 from tarkalabs/feature/avatar
Browse files Browse the repository at this point in the history
Feature/avatar
  • Loading branch information
Ibrahimhass authored May 22, 2023
2 parents 767fc68 + 71974b3 commit a37fec3
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 18 deletions.
33 changes: 33 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ class _HomePageState extends State<HomePage> {
body: ListView(
padding: const EdgeInsets.symmetric(horizontal: 25, vertical: 30),
children: [
TKAvatar(
avatarSize: TKAvatarSize.xxl,
avatarContent: TKAvatarContent(
text: "IH",
type: TKAvatarContentType.text,
),
),
TKAvatar(
avatarSize: TKAvatarSize.xl,
avatarContent: TKAvatarContent(
text: "IH",
type: TKAvatarContentType.text,
),
),
TKAvatar(
avatarSize: TKAvatarSize.m,
avatarContent: TKAvatarContent(
type: TKAvatarContentType.icon,
icon: Icon(Icons.construction),
),
isBadged: true,
),
TKAvatar(
avatarSize: TKAvatarSize.xxl,
avatarContent: TKAvatarContent(
type: TKAvatarContentType.image,
image: const TKImage(
imageUrl:
"https://avatars.githubusercontent.com/u/16992520?v=4",
),
),
isBadged: true,
),
const Text("Heading1", style: TKTextStyle.heading1),
const Text("Heading2", style: TKTextStyle.heading2),
const Text("Heading3", style: TKTextStyle.heading3),
Expand Down
34 changes: 17 additions & 17 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ packages:
dependency: transitive
description:
name: async
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
sha256: bfe67ef28df125b7dddcea62755991f807aa39a2492a23e1550161692950bbe0
url: "https://pub.dev"
source: hosted
version: "2.11.0"
version: "2.10.0"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,10 +21,10 @@ packages:
dependency: transitive
description:
name: characters
sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
sha256: e6a326c8af69605aec75ed6c187d06b349707a27fbff8222ca9cc2cff167975c
url: "https://pub.dev"
source: hosted
version: "1.3.0"
version: "1.2.1"
clock:
dependency: transitive
description:
Expand All @@ -37,10 +37,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
sha256: cfc915e6923fe5ce6e153b0723c753045de46de1b4d63771530504004a45fae0
url: "https://pub.dev"
source: hosted
version: "1.17.1"
version: "1.17.0"
cupertino_icons:
dependency: "direct main"
description:
Expand Down Expand Up @@ -87,10 +87,10 @@ packages:
dependency: transitive
description:
name: js
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
sha256: "5528c2f391ededb7775ec1daa69e65a2d61276f7552de2b5f7b8d34ee9fd4ab7"
url: "https://pub.dev"
source: hosted
version: "0.6.7"
version: "0.6.5"
lints:
dependency: transitive
description:
Expand All @@ -103,10 +103,10 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
sha256: "16db949ceee371e9b99d22f88fa3a73c4e59fd0afed0bd25fc336eb76c198b72"
url: "https://pub.dev"
source: hosted
version: "0.12.15"
version: "0.12.13"
material_color_utilities:
dependency: transitive
description:
Expand All @@ -119,18 +119,18 @@ packages:
dependency: transitive
description:
name: meta
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
sha256: "6c268b42ed578a53088d834796959e4a1814b5e9e164f147f580a386e5decf42"
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.8.0"
path:
dependency: transitive
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
sha256: db9d4f58c908a4ba5953fcee2ae317c94889433e5024c27ce74a37f94267945b
url: "https://pub.dev"
source: hosted
version: "1.8.3"
version: "1.8.2"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -187,10 +187,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb
sha256: ad540f65f92caa91bf21dfc8ffb8c589d6e4dc0c2267818b4cc2792857706206
url: "https://pub.dev"
source: hosted
version: "0.5.1"
version: "0.4.16"
vector_math:
dependency: transitive
description:
Expand All @@ -200,5 +200,5 @@ packages:
source: hosted
version: "2.1.4"
sdks:
dart: ">=3.0.0-0 <4.0.0"
dart: ">=2.19.6 <3.0.0"
flutter: ">=1.17.0"
210 changes: 210 additions & 0 deletions lib/components/avatar/tk_avatar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
import 'package:flutter/material.dart';
import 'package:tarka_ui/styles/tk_colors.dart';
import 'package:tarka_ui/styles/tk_text_style.dart';

/// TKAvatar is used to create a Avatar with content, size and badge flag.
class TKAvatar extends StatelessWidget {
final TKAvatarSize avatarSize;
final TKAvatarContent avatarContent;
final bool isBadged;

const TKAvatar({
super.key,
required this.avatarSize,
required this.avatarContent,
this.isBadged = false,
});

@override
Widget build(BuildContext context) {
Widget circleChild;

if (avatarContent.icon != null) {
circleChild = Icon(
avatarContent.icon!.icon,
size: avatarSize._getAvatarSize() / 2,
color: TKColors.constantLight,
);
} else if (avatarContent.text != null) {
TextStyle textStyle = avatarSize._getTextStyle().copyWith(
color: TKColors.onTertiary,
);

circleChild = Text(
avatarContent.text!,
style: textStyle,
);
} else {
circleChild = ClipOval(child: avatarContent.image!);
}

CircleAvatar circleAvatar = CircleAvatar(
radius: avatarSize._getAvatarSize() / 2,
backgroundColor: TKColors.tertiary,
child: circleChild,
);

if (isBadged) {
return LayoutBuilder(
builder: (context, constraints) {
Positioned badge;

if (constraints.maxHeight != double.infinity) {
badge = Positioned(
left: constraints.maxWidth / 2 + avatarSize._getAvatarSize() / 4,
top: constraints.maxHeight / 2 + avatarSize._getAvatarSize() / 4,
child: Container(
height: avatarSize._getBadgeSize(),
width: avatarSize._getBadgeSize(),
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: TKColors.success,
),
),
);
} else {
badge = Positioned(
left: constraints.maxWidth / 2 + avatarSize._getAvatarSize() / 4,
bottom: 0,
child: Container(
height: avatarSize._getBadgeSize(),
width: avatarSize._getBadgeSize(),
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: TKColors.success,
),
),
);
}

return Stack(
alignment: Alignment.center,
children: [
Center(
child: SizedBox(
width: avatarSize._getAvatarSize(),
height: avatarSize._getAvatarSize(),
child: circleAvatar,
),
),
badge
],
);
},
);
} else {
return circleAvatar;
}
}
}

/// TKAvatarContent stores the content type and the content for the Avatar.
class TKAvatarContent {
final TKAvatarContentType type;
final Icon? icon;
final TKImage? image;
final String? text;

TKAvatarContent({required this.type, this.icon, this.image, this.text});
}

/// TKAvatarContentType is used to define the content type of Avatar.
enum TKAvatarContentType { image, icon, text }

/// TKAvatarSize is used to define the size of Avatar.
enum TKAvatarSize {
xxs,
xs,
s,
m,
l,
xl,
xxl;

double _getAvatarSize() {
switch (this) {
case TKAvatarSize.xxs:
return 24;
case TKAvatarSize.xs:
return 32;
case TKAvatarSize.s:
return 40;
case TKAvatarSize.m:
return 48;
case TKAvatarSize.l:
return 64;
case TKAvatarSize.xl:
return 80;
case TKAvatarSize.xxl:
return 96;
}
}

double _getBadgeSize() {
switch (this) {
case TKAvatarSize.xxs:
case TKAvatarSize.xs:
case TKAvatarSize.s:
case TKAvatarSize.m:
return 12;
case TKAvatarSize.l:
return 16;
case TKAvatarSize.xl:
case TKAvatarSize.xxl:
return 24;
}
}

TextStyle _getTextStyle() {
switch (this) {
case TKAvatarSize.xxs:
return TKTextStyle.body8;
case TKAvatarSize.xs:
return TKTextStyle.heading7;
case TKAvatarSize.s:
return TKTextStyle.heading6;
case TKAvatarSize.m:
return TKTextStyle.heading5;
case TKAvatarSize.l:
return TKTextStyle.heading4;
case TKAvatarSize.xl:
return TKTextStyle.heading3;
case TKAvatarSize.xxl:
return TKTextStyle.heading2;
}
}
}

/// TKImage is used as an abstraction to handle asset and network image.
class TKImage extends StatelessWidget {
final String? _imageUrl;
final String? _assetPath;

const TKImage({
super.key,
String? imageUrl,
String? assetPath,
}) : _assetPath = assetPath,
_imageUrl = imageUrl,
assert(
imageUrl != null || assetPath != null,
'imageUrl or assetPath must be provided.',
);

@override
Image build(BuildContext context) {
if (_assetPath != null) {
return Image.asset(
_assetPath!,
);
} else {
return Image.network(
_imageUrl!,
errorBuilder:
(BuildContext context, Object exception, StackTrace? stackTrace) {
return const Text('Failed to load image');
},
);
}
}
}
4 changes: 3 additions & 1 deletion lib/tarka_ui.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
library tarka_ui;

export 'package:tarka_ui/components/avatar/tk_avatar.dart';
export 'package:tarka_ui/components/button/tk_button.dart';
// exports component
export 'package:tarka_ui/styles/tk_colors.dart';
export 'package:tarka_ui/styles/tk_symbols.dart';
export 'package:tarka_ui/styles/tk_text_style.dart';
export 'package:tarka_ui/components/button/tk_button.dart';

0 comments on commit a37fec3

Please sign in to comment.