import 'dart:io'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:test_sa/extensions/context_extension.dart'; import 'package:test_sa/models/subtitle.dart'; import 'package:test_sa/views/app_style/sizing.dart'; class AMiniOneFilePicker extends StatefulWidget { final Function(File) onPick; final File file; final String label; final bool error; const AMiniOneFilePicker({Key key, this.label, this.error, this.file, this.onPick}) : super(key: key); @override _AMiniOneFilePickerState createState() => _AMiniOneFilePickerState(); } class _AMiniOneFilePickerState extends State { File _file; @override void initState() { super.initState(); _file = widget.file; } @override Widget build(BuildContext context) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Visibility( visible: widget.label != null, child: Column( children: [ SizedBox(height: 8 * AppStyle.getScaleFactor(context)), Text( widget.label ?? '', style: Theme.of(context).textTheme.titleLarge, textScaleFactor: AppStyle.getScaleFactor(context), ), ], ), ), Visibility( visible: _file == null && widget.error == true, child: Column( children: [ const SizedBox(height: 4), Text( context.translation.requiredFile, style: Theme.of(context).textTheme.titleLarge.copyWith(color: Colors.red), textScaleFactor: AppStyle.getScaleFactor(context), ), ], ), ), const SizedBox(height: 8), SizedBox( width: MediaQuery.of(context).size.width, child: ElevatedButton( style: ElevatedButton.styleFrom( shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(AppStyle.borderRadius * AppStyle.getScaleFactor(context))), //primary: Colors.grey[200], textStyle: Theme.of(context).textTheme.labelSmall, padding: _file == null ? null : EdgeInsets.zero, ), child: Padding( padding: const EdgeInsets.all(8.0), child: Text( _file == null ? context.translation.pickFile : _file.path.split("/").last, textScaleFactor: AppStyle.getScaleFactor(context), ), ), onPressed: () async { onFilePicker(context.translation); }, ), ), ], ); } fromFilePicker(AppLocalizations subtitle) async { FilePickerResult result = await FilePicker.platform.pickFiles( type: FileType.custom, allowedExtensions: ['jpg', 'jpeg', 'png', 'pdf', 'doc', 'docx', 'xlsx', 'pptx'], ); if (result != null) { for (var path in result.paths) { _file = File(path); widget.onPick(_file); } setState(() {}); } } onFilePicker(AppLocalizations subtitle) async { ImageSource source = await showDialog( context: context, builder: (dialogContext) => CupertinoAlertDialog( actions: [ TextButton( child: Text(subtitle.pickFromCamera), onPressed: () { Navigator.of(dialogContext).pop(ImageSource.camera); }, ), TextButton( child: Text(subtitle.pickFromGallery), onPressed: () { Navigator.of(dialogContext).pop(ImageSource.gallery); }, ), TextButton( child: Text(subtitle.pickFromFiles), onPressed: () async { await fromFilePicker(subtitle); Navigator.pop(context); }, ), ], ), ); if (source == null) return; final pickedFile = await ImagePicker().pickImage(source: source, imageQuality: 70, maxWidth: 800, maxHeight: 800); if (pickedFile != null) { File fileImage = File(pickedFile.path); if (fileImage != null) { _file = File(pickedFile.path); widget.onPick(_file); } } setState(() {}); } }