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.
		
		
		
		
		
			
		
			
				
	
	
		
			123 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Dart
		
	
			
		
		
	
	
			123 lines
		
	
	
		
			4.0 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/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 {}
 | 
						|
              });
 | 
						|
            },
 | 
						|
          ),
 | 
						|
        ),
 | 
						|
      ],
 | 
						|
    );
 | 
						|
  }
 | 
						|
}
 |