import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

//拍照、选择相册图片对话框
class customDialogH extends Dialog {
  String title;
  String content;
  int index;

  customDialogH({this.title = "", this.content = "", this.index = -1});

  Widget getBtnSizeX({@required text, width = 60.0, height = 30.0, onPressedFun}) {
    return Container(
      color: Colors.white12, //onPressedFun为null时无效
      width: width,
      height: height,
      child: RaisedButton(
        padding: EdgeInsets.all(0),
        textColor: Colors.black,
        child: Text(text),
        onPressed: onPressedFun,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    Size mediaSize = MediaQuery.of(context).size;
    // TODO: implement build
    return WillPopScope(
      child: Material(
        type: MaterialType.transparency,
        child: Container(
          alignment: Alignment(0, -0.7),
          child: Container(
            height: mediaSize.height * 0.25,
            width: mediaSize.width * 0.95,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(
                Radius.circular(2),
              ),
            ),
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
                  child: Stack(
                    children: <Widget>[
                      Align(
                        alignment: Alignment.center,
                        child: Text(
                          "${this.title}",
                          style: TextStyle(
                            fontSize: 15.0,
                          ),
                        ),
                      ),
                      Align(
                        alignment: Alignment.centerRight,
                        child: InkWell(
                          child: Icon(Icons.close),
                          onTap: () {
                            Navigator.pop(context); //关闭弹框，播放输入视频地址
                          },
                        ),
                      )
                    ],
                  ),
                ),
                Divider(),
                SizedBox(height: 35),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    getBtnSizeX(
                      text: "拍照",
                      onPressedFun: () async {
                        await _takePhoto();
                        await Navigator.pop(context, _imagePath); //先关闭“选择操作”对话框
                      },
                    ),
                    getBtnSizeX(
                      text: "从相册选择",
                      width: 110.0,
                      onPressedFun: () async {
                        await _openGallery();
                        await Navigator.pop(context, _imagePath); //先关闭“选择操作”对话框
                      },
                    ),
                    getBtnSizeX(
                      text: "取消",
                      onPressedFun: () {
                        Navigator.pop(context); //关闭弹框，播放输入视频地址
                      },
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
      onWillPop: () {
        // 屏蔽点击返回键的操作
        // player.pause();
        Navigator.pop(context);
      },
    );
  }

  File _image;
  String _imagePath = '';
  final picker = ImagePicker();

  /*拍照*/
  _takePhoto() async {
    final pickedFile = await picker.getImage(source: ImageSource.camera);

    if (pickedFile != null) {
      _image = File(pickedFile.path);
      _imagePath = pickedFile.path;
      this._uploadImage(_image);
    } else {
      print('No image selected.');
    }
  }

  /*相册*/
  _openGallery() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);

    if (pickedFile != null) {
      _image = File(pickedFile.path);
      _imagePath = pickedFile.path;
      this._uploadImage(_image);
    } else {
      print('No image selected.');
    }
  }

  //上传图片
  _uploadImage(File _imageDir) async {
    //注意：dio3.x版本为了兼容web做了一些修改，上传图片的时候需要把File类型转换成String类型，具体代码如下
    var fileDir = _imageDir.path;

    FormData formData = FormData.fromMap({
      "name": "zhangsna 6666666666",
      "age": 20,
      "sex": "男",
      "file": await MultipartFile.fromFile(fileDir, filename: "xxx.jpg")
    });
    //var response = await Dio().post(ServicePath.uploadImageUrl, data: fileDir);
    // var response = await Dio().post(ServiceUrlJd, data: fileDir);
    // print(response);
  }
}
