2024-10-10 05:49:33 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
import 'package:bootstrap_icons/bootstrap_icons.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:english_learning/core/utils/styles/theme.dart';
|
|
|
|
|
|
|
|
|
|
class UserAvatar extends StatelessWidget {
|
|
|
|
|
final String? pictureUrl;
|
|
|
|
|
final double radius;
|
|
|
|
|
final String baseUrl;
|
|
|
|
|
final Function()? onImageSelected;
|
|
|
|
|
final File? selectedImage;
|
2024-11-05 06:34:38 +00:00
|
|
|
final bool showCameraIcon;
|
2024-10-10 05:49:33 +00:00
|
|
|
|
|
|
|
|
const UserAvatar({
|
|
|
|
|
super.key,
|
|
|
|
|
this.pictureUrl,
|
|
|
|
|
this.radius = 55,
|
|
|
|
|
required this.baseUrl,
|
|
|
|
|
this.onImageSelected,
|
|
|
|
|
this.selectedImage,
|
2024-11-05 06:34:38 +00:00
|
|
|
this.showCameraIcon = false,
|
2024-10-10 05:49:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2024-11-05 06:34:38 +00:00
|
|
|
return Stack(
|
|
|
|
|
children: [
|
|
|
|
|
// Avatar with detail view
|
|
|
|
|
GestureDetector(
|
|
|
|
|
onTap: () {
|
|
|
|
|
showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
|
return Dialog(
|
|
|
|
|
child: Container(
|
|
|
|
|
width: double.infinity,
|
|
|
|
|
height: 400,
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
borderRadius: BorderRadius.circular(12),
|
|
|
|
|
),
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
Expanded(
|
|
|
|
|
child: ClipRRect(
|
|
|
|
|
borderRadius: const BorderRadius.vertical(
|
|
|
|
|
top: Radius.circular(12),
|
|
|
|
|
),
|
|
|
|
|
child: _getDetailAvatarContent(),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
|
child: TextButton(
|
|
|
|
|
style: TextButton.styleFrom(
|
|
|
|
|
foregroundColor: AppColors.primaryColor,
|
|
|
|
|
),
|
|
|
|
|
onPressed: () => Navigator.pop(context),
|
|
|
|
|
child: const Text('Close'),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
child: CircleAvatar(
|
|
|
|
|
radius: radius,
|
|
|
|
|
backgroundColor: AppColors.primaryColor,
|
|
|
|
|
child: _getAvatarContent(),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
// Camera icon
|
|
|
|
|
if (showCameraIcon)
|
|
|
|
|
Positioned(
|
|
|
|
|
right: 0,
|
|
|
|
|
bottom: 0,
|
|
|
|
|
child: GestureDetector(
|
|
|
|
|
onTap: onImageSelected,
|
|
|
|
|
child: Container(
|
|
|
|
|
padding: const EdgeInsets.all(6),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: AppColors.primaryColor,
|
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
|
border: Border.all(
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
width: 2,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: Icon(
|
|
|
|
|
BootstrapIcons.camera,
|
|
|
|
|
color: AppColors.whiteColor,
|
|
|
|
|
size: radius * 0.3,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
2024-10-10 05:49:33 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _getAvatarContent() {
|
|
|
|
|
if (selectedImage != null) {
|
|
|
|
|
return ClipOval(
|
|
|
|
|
child: Image.file(
|
|
|
|
|
selectedImage!,
|
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
|
width: radius * 2,
|
|
|
|
|
height: radius * 2,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else if (pictureUrl != null && pictureUrl!.isNotEmpty) {
|
|
|
|
|
return ClipOval(
|
|
|
|
|
child: Image.network(
|
|
|
|
|
'$baseUrl$pictureUrl',
|
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
|
width: radius * 2,
|
|
|
|
|
height: radius * 2,
|
|
|
|
|
errorBuilder: (context, error, stackTrace) {
|
|
|
|
|
print('Error loading avatar image: $error');
|
|
|
|
|
return _buildDefaultIcon();
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return _buildDefaultIcon();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-05 06:34:38 +00:00
|
|
|
Widget _getDetailAvatarContent() {
|
|
|
|
|
if (selectedImage != null) {
|
|
|
|
|
return Image.file(
|
|
|
|
|
selectedImage!,
|
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
|
);
|
|
|
|
|
} else if (pictureUrl != null && pictureUrl!.isNotEmpty) {
|
|
|
|
|
return Image.network(
|
|
|
|
|
'$baseUrl$pictureUrl',
|
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
|
errorBuilder: (context, error, stackTrace) {
|
|
|
|
|
return Center(
|
|
|
|
|
child: Icon(
|
|
|
|
|
BootstrapIcons.person,
|
|
|
|
|
color: AppColors.primaryColor,
|
|
|
|
|
size: 120,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return Center(
|
|
|
|
|
child: Icon(
|
|
|
|
|
BootstrapIcons.person,
|
|
|
|
|
color: AppColors.primaryColor,
|
|
|
|
|
size: 120,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-10 05:49:33 +00:00
|
|
|
Widget _buildDefaultIcon() {
|
|
|
|
|
return Icon(
|
|
|
|
|
BootstrapIcons.person,
|
|
|
|
|
color: AppColors.whiteColor,
|
|
|
|
|
size: radius * 1.2,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|