Skip to content

Instantly share code, notes, and snippets.

@aasumitro
Created April 12, 2021 18:06
Show Gist options
  • Save aasumitro/7d60714a41071d7f944b53d54edbb1b0 to your computer and use it in GitHub Desktop.
Save aasumitro/7d60714a41071d7f944b53d54edbb1b0 to your computer and use it in GitHub Desktop.
Circular and Rectangler Avatar on Flutter
import '/circular_widget.dart';
import '/rectangular_widget.dart';
import 'package:flutter/material.dart';
class AvatarWidget extends StatefulWidget {
final AvatarType avatarType;
final bool isOnlyText;
final double radius;
final double borderWidth;
final Color? borderColor;
final Color? backgroundColor;
final Color? foregroundColor;
final String imagePath;
final Text? text;
final GestureTapCallback? onTap;
final Widget? placeHolder;
final Widget? errorWidget;
const AvatarWidget(
{Key? key,
this.avatarType = AvatarType.CIRCLE,
this.isOnlyText = false,
this.radius = 50,
this.borderWidth = 0,
this.borderColor,
this.backgroundColor,
this.foregroundColor,
this.imagePath = "",
this.text,
this.onTap,
this.placeHolder,
this.errorWidget})
: super(key: key);
@override
_AvatarWidgetState createState() => _AvatarWidgetState();
}
class _AvatarWidgetState extends State<AvatarWidget> {
@override
Widget build(BuildContext context) {
return InkWell(
onTap: widget.onTap,
child: widget.avatarType == AvatarType.CIRCLE
? CircularWidget(
backgroundColor: widget.backgroundColor,
borderColor: widget.borderColor,
borderWidth: widget.borderWidth,
errorWidget: widget.errorWidget,
placeHolder: widget.placeHolder,
imagePath: widget.imagePath,
radius: widget.radius,
foregroundColor: widget.foregroundColor,
text: widget.text,
isOnlyText: widget.isOnlyText,
)
: RectangularWidget(
backgroundColor: widget.backgroundColor,
borderColor: widget.borderColor,
borderWidth: widget.borderWidth,
errorWidget: widget.errorWidget,
placeHolder: widget.placeHolder,
imagePath: widget.imagePath,
radius: widget.radius,
foregroundColor: widget.foregroundColor,
text: widget.text,
isOnlyText: widget.isOnlyText,
),
);
}
}
enum AvatarType {
CIRCLE,
RECTANGLE,
}
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
class CircularWidget extends StatelessWidget {
final Text? text;
final bool? isOnlyText;
final double? radius;
final double? borderWidth;
final String? imagePath;
final Color? borderColor;
final Color? backgroundColor;
final Color? foregroundColor;
final Widget? placeHolder;
final Widget? errorWidget;
const CircularWidget(
{Key? key,
this.radius,
this.borderWidth,
this.imagePath,
this.backgroundColor,
this.foregroundColor,
this.borderColor,
this.placeHolder,
this.errorWidget,
this.text,
this.isOnlyText})
: super(key: key);
Widget getTextWidget() {
return ClipRRect(
borderRadius: BorderRadius.circular(radius!),
child: Container(
padding: EdgeInsets.all(borderWidth!),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(radius!),
color: backgroundColor),
child: Center(child: text)),
);
}
@override
Widget build(BuildContext context) {
return Container(
height: radius! * 2,
width: radius! * 2,
padding: EdgeInsets.all(borderWidth!),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(radius!), color: borderColor),
child: isOnlyText!
? getTextWidget()
: imagePath!.isEmpty
? ClipRRect(
borderRadius: BorderRadius.circular(radius!),
child: Container(
color: backgroundColor,
),
)
: ClipRRect(
borderRadius: BorderRadius.circular(radius!),
child: imagePath!.contains("http")
? CachedNetworkImage(
imageUrl: imagePath!,
fit: BoxFit.cover,
placeholder: (context, url) {
return placeHolder!;
},
errorWidget: (context, url, error) {
return errorWidget!;
},
)
: Image.asset(imagePath!, fit: BoxFit.cover),
),
);
}
}
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
class RectangularWidget extends StatelessWidget {
final Text? text;
final bool? isOnlyText;
final double? radius;
final double? borderWidth;
final String? imagePath;
final Color? borderColor;
final Color? backgroundColor;
final Color? foregroundColor;
final Widget? placeHolder;
final Widget? errorWidget;
const RectangularWidget(
{Key? key,
this.radius,
this.borderWidth,
this.imagePath,
this.backgroundColor,
this.foregroundColor,
this.borderColor,
this.placeHolder,
this.errorWidget,
this.text,
this.isOnlyText})
: super(key: key);
Widget getTextWidget() {
return ClipRRect(
borderRadius: BorderRadius.circular(radius! / 2),
child: Container(
padding: EdgeInsets.all(borderWidth!),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(radius! / 2),
color: backgroundColor),
child: Center(child: text)),
);
}
@override
Widget build(BuildContext context) {
return Container(
height: radius! * 2,
width: radius! * 2,
padding: EdgeInsets.all(borderWidth!),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(radius! / 2), color: borderColor),
child: isOnlyText!
? getTextWidget()
: imagePath!.isEmpty
? ClipRRect(
borderRadius: BorderRadius.circular(radius! / 2),
child: Container(
color: backgroundColor,
),
)
: ClipRRect(
borderRadius: BorderRadius.circular(radius! / 2),
child: imagePath!.contains("http") ? CachedNetworkImage(
imageUrl: imagePath!,
fit: BoxFit.cover,
placeholder: (context, url) {
return placeHolder!;
},
errorWidget: (context, url, error) {
return errorWidget!;
},
) : Image.asset(imagePath!, fit: BoxFit.cover,),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment