Files
CityCards_Customer_Flutter/lib/postcard/widgets/front_card_widget.dart

98 lines
2.7 KiB
Dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class FrontCardWidget extends StatelessWidget {
final String imageUrl;
final double aspectRatio;
const FrontCardWidget({
super.key,
this.imageUrl = '',
this.aspectRatio = 1.5,
});
/// Automatically detect if the path is a network URL or local file path
bool get _isNetworkImage {
return imageUrl.startsWith('http://') ||
imageUrl.startsWith('https://');
}
@override
Widget build(BuildContext context) {
return Container(
key: const ValueKey('front'),
margin: EdgeInsets.symmetric(horizontal: 12.w),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6),
// boxShadow: [
// BoxShadow(
// color: Colors.black.withOpacity(0.1),
// blurRadius: 10,
// offset: const Offset(0, 4),
// ),
// ],
),
child: ClipRRect(
borderRadius: BorderRadius.circular(6),
child: AspectRatio(
aspectRatio: aspectRatio,
child: _buildImage(),
),
),
);
}
Widget _buildImage() {
if (imageUrl.isEmpty) {
return Container(
color: Colors.grey.shade200,
child: const Center(
child: Icon(Icons.image, size: 40, color: Colors.grey),
),
);
}
if (_isNetworkImage) {
return Image.network(
imageUrl,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return Container(
color: Colors.grey.shade200,
child: const Center(
child: Icon(Icons.broken_image, size: 40, color: Colors.grey),
),
);
},
loadingBuilder: (context, child, loadingProgress) {
if (loadingProgress == null) return child;
return Container(
color: Colors.grey.shade200,
child: Center(
child: CircularProgressIndicator(color: Color(0xffF95F62),
value: loadingProgress.expectedTotalBytes != null
? loadingProgress.cumulativeBytesLoaded /
loadingProgress.expectedTotalBytes!
: null,
),
),
);
},
);
} else {
return Image.file(
File(imageUrl),
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return Container(
color: Colors.grey.shade200,
child: const Center(
child: Icon(Icons.broken_image, size: 40, color: Colors.grey),
),
);
},
);
}
}
}