131 lines
4.0 KiB
Dart
131 lines
4.0 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
|
|
import '../assets/assets.gen.dart';
|
|
import '../constants/colors.dart';
|
|
import '../constants/variables.dart';
|
|
import 'buttons.dart';
|
|
import 'spaces.dart';
|
|
|
|
class ImagePickerWidget extends StatefulWidget {
|
|
final String label;
|
|
final void Function(XFile? file) onChanged;
|
|
final bool showLabel;
|
|
final String? initialImageUrl;
|
|
|
|
const ImagePickerWidget({
|
|
super.key,
|
|
required this.label,
|
|
required this.onChanged,
|
|
this.showLabel = true,
|
|
this.initialImageUrl,
|
|
});
|
|
|
|
@override
|
|
State<ImagePickerWidget> createState() => _ImagePickerWidgetState();
|
|
}
|
|
|
|
class _ImagePickerWidgetState extends State<ImagePickerWidget> {
|
|
String? imagePath;
|
|
bool hasInitialImage = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
hasInitialImage = widget.initialImageUrl != null;
|
|
}
|
|
|
|
Future<void> _pickImage() async {
|
|
final pickedFile = await ImagePicker().pickImage(
|
|
source: ImageSource.gallery,
|
|
);
|
|
|
|
setState(() {
|
|
if (pickedFile != null) {
|
|
imagePath = pickedFile.path;
|
|
hasInitialImage = false; // Clear initial image when new image is picked
|
|
widget.onChanged(pickedFile);
|
|
} else {
|
|
debugPrint('No image selected.');
|
|
widget.onChanged(null);
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
if (widget.showLabel) ...[
|
|
Text(
|
|
widget.label,
|
|
style: const TextStyle(
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
const SpaceHeight(12.0),
|
|
],
|
|
Container(
|
|
padding: const EdgeInsets.all(6.0),
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(16.0),
|
|
border: Border.all(color: AppColors.primary),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
SizedBox(
|
|
width: 80.0,
|
|
height: 80.0,
|
|
child: ClipRRect(
|
|
borderRadius: BorderRadius.circular(10.0),
|
|
child: imagePath != null
|
|
? Image.file(
|
|
File(imagePath!),
|
|
fit: BoxFit.cover,
|
|
)
|
|
: hasInitialImage && widget.initialImageUrl != null
|
|
? CachedNetworkImage(
|
|
imageUrl: widget.initialImageUrl!.contains('http')
|
|
? widget.initialImageUrl!
|
|
: '${Variables.baseUrl}/${widget.initialImageUrl}',
|
|
placeholder: (context, url) =>
|
|
const Center(child: CircularProgressIndicator()),
|
|
errorWidget: (context, url, error) => Container(
|
|
padding: const EdgeInsets.all(16.0),
|
|
color: AppColors.black.withOpacity(0.05),
|
|
child: Assets.icons.image.svg(),
|
|
),
|
|
fit: BoxFit.cover,
|
|
)
|
|
: Container(
|
|
padding: const EdgeInsets.all(16.0),
|
|
color: AppColors.black.withOpacity(0.05),
|
|
child: Assets.icons.image.svg(),
|
|
),
|
|
),
|
|
),
|
|
const Spacer(),
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 10.0),
|
|
child: Button.filled(
|
|
height: 30.0,
|
|
width: 140.0,
|
|
onPressed: _pickImage,
|
|
label: 'Choose Photo',
|
|
fontSize: 12.0,
|
|
borderRadius: 5.0,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|