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

138 lines
4.4 KiB
Dart

3 years ago
import 'dart:io';
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/views/app_style/sizing.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.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;
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(
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(
_subtitle.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);
}
});
},
),
),
],
);
}
}