A customizable Flutter package for displaying profile avatars with support for multiple image sources including network images, local files, name initials, and placeholder icons.
- π Network Images: Display avatars from URLs with built-in caching support
- π Local Files: Load avatar images from device storage
- π€ Name Initials: Generate text-based avatars from user names
- π Placeholder Icons: Show default avatar icons when no image is available
- π¨ Customizable Styling: Control size, colors, and border radius
- β³ Loading States: Built-in loading indicators for network images
- π‘οΈ Error Handling: Graceful fallbacks when images fail to load
- π± Platform Support: Works on iOS, Android, Web, and Desktop
Demo App Overview | Interactive Controls |
---|---|
![]() |
![]() |
Add this to your package's pubspec.yaml
file:
dependencies:
my_profile_avatar: ^1.0.0
Then run:
flutter pub get
import 'package:my_profile_avatar/my_profile_avatar.dart';
MyProfileAvatar(
style: MyProfileAvatarStyle(
size: 60,
color: Colors.blue,
radius: 30,
),
urlImage: 'https://example.com/avatar.jpg',
)
MyProfileAvatar(
style: MyProfileAvatarStyle(
size: 80,
color: Colors.blue,
radius: 40,
),
urlImage: 'https://images.pexels.com/photos/220453/pexels-photo-220453.jpeg',
)
MyProfileAvatar(
style: MyProfileAvatarStyle(
size: 60,
color: Colors.green,
radius: 30,
),
name: 'John Doe',
)
MyProfileAvatar(
style: MyProfileAvatarStyle(
size: 100,
color: Colors.purple,
radius: 50,
),
pathImage: '/path/to/local/image.jpg',
)
MyProfileAvatar(
style: MyProfileAvatarStyle(
size: 50,
color: Colors.grey,
radius: 25,
),
// No image sources provided - shows placeholder
)
For more explicit control over avatar types:
// Network image avatar
MyProfileAvatar.type(
type: MyProfileAvatarType.network,
value: 'https://example.com/avatar.jpg',
style: MyProfileAvatarStyle(
size: 60,
color: Colors.blue,
),
)
// Name initials avatar
MyProfileAvatar.type(
type: MyProfileAvatarType.name,
value: 'Alice Johnson',
style: MyProfileAvatarStyle(
size: 80,
color: Colors.green,
),
)
The MyProfileAvatarStyle
class provides extensive customization options:
MyProfileAvatarStyle(
size: 100, // Width and height in logical pixels
color: Colors.blue, // Primary color for text, icons, and loading indicators
radius: 50, // Border radius (50 = perfect circle for size 100)
)
// Perfect circle
MyProfileAvatarStyle(
size: 80,
color: Colors.blue,
radius: 40, // Half of size
)
// Rounded square
MyProfileAvatarStyle(
size: 60,
color: Colors.green,
radius: 12,
)
// Sharp square
MyProfileAvatarStyle(
size: 50,
color: Colors.red,
radius: 0,
)
The widget automatically determines which content to display based on this priority order:
- Local File (
pathImage
) - Highest priority - Network Image (
urlImage
) - Name Initials (
name
) - Placeholder Icon - Lowest priority (fallback)
MyProfileAvatar(
style: MyProfileAvatarStyle(color: Colors.blue),
pathImage: '/local/image.jpg', // This will be shown
urlImage: 'https://example.com/avatar.jpg', // Ignored
name: 'John Doe', // Ignored
)
The widget includes built-in error handling, but you can also check the avatar type programmatically:
final avatar = MyProfileAvatar(
style: MyProfileAvatarStyle(color: Colors.blue),
urlImage: 'https://example.com/avatar.jpg',
);
// Check what type will be displayed
final avatarType = avatar.getType(); // MyProfileAvatarType.network
final avatarValue = avatar.getValue(); // 'https://example.com/avatar.jpg'
class UserProfile extends StatelessWidget {
final User user;
const UserProfile({required this.user, super.key});
@override
Widget build(BuildContext context) {
return Column(
children: [
MyProfileAvatar(
style: MyProfileAvatarStyle(
size: 120,
color: Theme.of(context).primaryColor,
radius: 60,
),
urlImage: user.profileImageUrl,
name: user.fullName,
),
const SizedBox(height: 16),
Text(user.fullName, style: Theme.of(context).textTheme.headlineSmall),
],
);
}
}
Parameter | Type | Description |
---|---|---|
style |
MyProfileAvatarStyle |
Required. Styling configuration for the avatar |
name |
String? |
Optional name to display as initials |
urlImage |
String? |
Optional URL for network image |
pathImage |
String? |
Optional path to local image file |
Parameter | Type | Default | Description |
---|---|---|---|
color |
Color |
Required | Primary color for text, icons, and indicators |
size |
double |
50.0 |
Width and height in logical pixels |
radius |
double |
100.0 |
Border radius for rounded corners |
Value | Description |
---|---|
placeholder |
Default placeholder icon |
network |
Network image from URL |
file |
Local file image |
name |
Name initials as text |
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Clone your fork:
git clone https://github.com/mohamedmaher-dev/my_profile_avatar.git
- Create a feature branch:
git checkout -b feature/amazing-feature
- Make your changes and add tests
- Run tests:
flutter test
- Commit your changes:
git commit -m 'Add amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Open a Pull Request
If you encounter any issues or have feature requests, please file them in the issues section.
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with β€οΈ using Flutter
- Network image caching powered by cached_network_image
- Icons from Cupertino Icons
Made with β€οΈ for the Flutter community