Untitled

 avatar
unknown
plain_text
a year ago
4.8 kB
6
Indexable
class UploadPhoto extends StatefulWidget {
  final Function(File) onFileChanged;
  const UploadPhoto({
    super.key,
    required this.onFileChanged,
  });

  @override
  State<UploadPhoto> createState() => _UploadPhotoState();
}

class _UploadPhotoState extends State<UploadPhoto> {
  final ImagePicker _picker = ImagePicker();
  XFile? _imageFile;

  Future<void> _pickImageFromCamera() async {
    final pickedFile = await _picker.pickImage(source: ImageSource.camera);
    if (mounted) {
      setState(() {
        _imageFile = pickedFile;
      });
    }
    if (pickedFile != null) {
      widget.onFileChanged(File(pickedFile.path));
    }
  }

  Future<void> _pickImageFromGallery() async {
    final pickedFile = await _picker.pickImage(source: ImageSource.gallery);
    if (mounted) {
      setState(() {
        _imageFile = pickedFile;
      });
    }
    if (pickedFile != null) {
      widget.onFileChanged(File(pickedFile.path));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(
        top: 15,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            'Masukan Foto',
            style: mediumTextStyle.copyWith(
              color: blackColor,
              fontSize: 18,
            ),
          ),
          Text(
            'Wajib memasukan foto!',
            style: regularTextStyle.copyWith(
              color: darkGreyColor,
              fontSize: 13,
            ),
          ),
          SizedBox(height: 8),
          Container(
            width: double.infinity,
            height: 227,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(12),
              border: Border.all(
                color: lightGreyColor,
              ),
            ),
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  if (_imageFile != null)
                    Image.file(
                      File(_imageFile!.path),
                      width: double.infinity,
                      height: 200,
                      fit: BoxFit.cover,
                    )
                  else
                    Column(
                      children: [
                        GestureDetector(
                          onTap: _pickImageFromCamera,
                          child: IntrinsicWidth(
                            child: Container(
                              padding: EdgeInsets.symmetric(
                                vertical: 7,
                                horizontal: 28,
                              ),
                              decoration: BoxDecoration(
                                color: darkGreenColor,
                                borderRadius: BorderRadius.circular(12),
                              ),
                              child: Center(
                                child: Text(
                                  'Buka Kamera',
                                  style: mediumTextStyle.copyWith(
                                    color: whiteColor,
                                    fontSize: 16,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),
                        SizedBox(height: 6),
                        GestureDetector(
                          onTap: _pickImageFromGallery,
                          child: IntrinsicWidth(
                            child: Container(
                              padding: EdgeInsets.symmetric(
                                vertical: 7,
                                horizontal: 28,
                              ),
                              decoration: BoxDecoration(
                                border: Border.all(
                                  color: darkGreenColor,
                                ),
                                borderRadius: BorderRadius.circular(12),
                              ),
                              child: Center(
                                child: Text(
                                  'Pilih di Galeri',
                                  style: mediumTextStyle.copyWith(
                                    color: darkGreenColor,
                                    fontSize: 16,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
Editor is loading...
Leave a Comment