81 lines
1.9 KiB
Dart
81 lines
1.9 KiB
Dart
/// Thumbnail or letter avatar for a product.
|
|
library;
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
class ProductImage extends StatelessWidget {
|
|
const ProductImage({
|
|
super.key,
|
|
required this.name,
|
|
this.path,
|
|
this.size = 48,
|
|
});
|
|
|
|
final String name;
|
|
final String? path;
|
|
final double size;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
final file = path == null ? null : File(path!);
|
|
final hasFile = file != null && file.existsSync();
|
|
|
|
return ClipRRect(
|
|
borderRadius: BorderRadius.circular(size / 4),
|
|
child: SizedBox(
|
|
width: size,
|
|
height: size,
|
|
child: hasFile
|
|
? Image.file(
|
|
file,
|
|
fit: BoxFit.cover,
|
|
cacheWidth: (size * 3).round(),
|
|
errorBuilder: (_, _, _) => _LetterAvatar(
|
|
name: name,
|
|
size: size,
|
|
colorScheme: theme.colorScheme,
|
|
),
|
|
)
|
|
: _LetterAvatar(
|
|
name: name,
|
|
size: size,
|
|
colorScheme: theme.colorScheme,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _LetterAvatar extends StatelessWidget {
|
|
const _LetterAvatar({
|
|
required this.name,
|
|
required this.size,
|
|
required this.colorScheme,
|
|
});
|
|
|
|
final String name;
|
|
final double size;
|
|
final ColorScheme colorScheme;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final trimmed = name.trim();
|
|
final letter = trimmed.isEmpty ? '?' : trimmed.substring(0, 1).toUpperCase();
|
|
return ColoredBox(
|
|
color: colorScheme.secondaryContainer,
|
|
child: Center(
|
|
child: Text(
|
|
letter,
|
|
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
|
color: colorScheme.onSecondaryContainer,
|
|
fontSize: size * 0.4,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|