You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudsolutions-atoms/lib/views/widgets/images/one_image_picker.dart

131 lines
4.3 KiB
Dart

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/extensions/context_extension.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/views/app_style/sizing.dart';
class AOneImagePicker extends StatefulWidget {
final Function(File) onPick;
final File image;
final String label;
final bool error;
const AOneImagePicker({Key key, this.label, this.error, this.image, this.onPick}) : super(key: key);
@override
_AOneImagePickerState createState() => _AOneImagePickerState();
}
class _AOneImagePickerState extends State<AOneImagePicker> {
File _image;
@override
void initState() {
super.initState();
_image = widget.image;
}
@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.headline6,
textScaleFactor: AppStyle.getScaleFactor(context),
),
],
),
),
Visibility(
visible: _image == null && widget.error == true,
child: Column(
children: [
SizedBox(
height: 4,
),
Text(
context.translation.requiredImage,
style: Theme.of(context).textTheme.headline6.copyWith(color: Colors.red),
textScaleFactor: AppStyle.getScaleFactor(context),
),
],
),
),
SizedBox(
height: 8,
),
Container(
height: MediaQuery.of(context).size.height / 8,
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],
padding: _image == null ? null : EdgeInsets.zero,
),
child: _image == null
? Text(
context.translation.pickImage,
style: Theme.of(context).textTheme.headline6,
textScaleFactor: AppStyle.getScaleFactor(context),
)
: ClipRRect(
borderRadius: BorderRadius.circular(AppStyle.borderRadius * AppStyle.getScaleFactor(context)),
child: Image(
height: MediaQuery.of(context).size.height / 6,
width: MediaQuery.of(context).size.width,
image: FileImage(_image),
fit: BoxFit.cover,
),
),
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);
}
});
},
),
),
],
);
}
}