Change Pick Image to Pick File
parent
6fc8e9b02c
commit
0b46974c47
@ -0,0 +1,145 @@
|
||||
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:test_sa/controllers/localization/localization.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<AMiniOneFilePicker> {
|
||||
File _file;
|
||||
Subtitle _subtitle;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_file = widget.file;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
_subtitle = AppLocalization.of(context).subtitle;
|
||||
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(
|
||||
_subtitle.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 ? _subtitle.pickFile : _file.path.split("/").last,
|
||||
textScaleFactor: AppStyle.getScaleFactor(context),
|
||||
),
|
||||
),
|
||||
onPressed: () async {
|
||||
onFilePicker(_subtitle);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
fromFilePicker(Subtitle 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(Subtitle subtitle) async {
|
||||
ImageSource source = await showDialog(
|
||||
context: context,
|
||||
builder: (dialogContext) => CupertinoAlertDialog(
|
||||
actions: <Widget>[
|
||||
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(() {});
|
||||
}
|
||||
}
|
||||
@ -1,122 +0,0 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:test_sa/controllers/localization/localization.dart';
|
||||
import 'package:test_sa/models/subtitle.dart';
|
||||
import 'package:test_sa/views/app_style/sizing.dart';
|
||||
|
||||
class AMiniOneImagePicker extends StatefulWidget {
|
||||
final Function(File) onPick;
|
||||
final File image;
|
||||
final String label;
|
||||
final bool error;
|
||||
|
||||
const AMiniOneImagePicker({Key key, this.label, this.error, this.image, this.onPick}) : super(key: key);
|
||||
|
||||
@override
|
||||
_AMiniOneImagePickerState createState() => _AMiniOneImagePickerState();
|
||||
}
|
||||
|
||||
class _AMiniOneImagePickerState extends State<AMiniOneImagePicker> {
|
||||
File _image;
|
||||
Subtitle _subtitle;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_image = widget.image;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
_subtitle = AppLocalization.of(context).subtitle;
|
||||
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.headline6,
|
||||
textScaleFactor: AppStyle.getScaleFactor(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Visibility(
|
||||
visible: _image == null && widget.error == true,
|
||||
child: Column(
|
||||
children: [
|
||||
SizedBox(
|
||||
height: 4,
|
||||
),
|
||||
Text(
|
||||
_subtitle.requiredImage,
|
||||
style: Theme.of(context).textTheme.headline6.copyWith(color: Colors.red),
|
||||
textScaleFactor: AppStyle.getScaleFactor(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 8,
|
||||
),
|
||||
Container(
|
||||
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.overline,
|
||||
padding: _image == null ? null : EdgeInsets.zero,
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Text(
|
||||
_image == null ? _subtitle.pickImage : _image.path.split("/").last,
|
||||
textScaleFactor: AppStyle.getScaleFactor(context),
|
||||
),
|
||||
),
|
||||
onPressed: () async {
|
||||
ImageSource source = await showDialog(
|
||||
context: context,
|
||||
builder: (_) => CupertinoAlertDialog(
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
child: Text("pick from camera"),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop(ImageSource.camera);
|
||||
},
|
||||
),
|
||||
TextButton(
|
||||
child: Text("pick from gallery"),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop(ImageSource.gallery);
|
||||
},
|
||||
),
|
||||
],
|
||||
));
|
||||
if (source == null) return;
|
||||
|
||||
final pickedFile = await ImagePicker().pickImage(source: source, imageQuality: 70, maxWidth: 1000, maxHeight: 1000);
|
||||
|
||||
setState(() {
|
||||
if (pickedFile != null) {
|
||||
_image = File(pickedFile.path);
|
||||
widget.onPick(_image);
|
||||
} else {}
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue