import 'package:flutter/material.dart';
import '../../services/EventBus.dart';
import 'package:camera/camera.dart';
import '../../components/commonFun.dart';
import 'dart:io';
import 'package:flutter_screenutil/flutter_screenutil.dart';


class FaceLogin extends StatefulWidget {
  FaceLogin({this.arguments, Key key}) : super(key: key);
  var arguments;

  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<FaceLogin> {
  Future<void> camerasInit() async {
    try {
      WidgetsFlutterBinding.ensureInitialized();
      cameras = await availableCameras();
      print(cameras.toString());
    } on CameraException catch (e) {
      //logError(e.code, e.description);
    }
  }

  @override
  void initState() {
    super.initState();
    camerasInit();
  }

  //监听登录页面销毁的事件
  dispose() {
    super.dispose();
    eventBus.fire(new UserEvent('登录成功...'));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(
      //   centerTitle: true,
      //   title: Text('人脸验证登录'),
      // ),
      body: Container(
        padding: EdgeInsets.all(ScreenUtil().setWidth(20)),
        child: ListView(
          children: <Widget>[
            Center(
              child: Container(
                margin: EdgeInsets.only(top: 30),
                height: ScreenUtil().setWidth(160),
                width: ScreenUtil().setWidth(160),
                child: Image.asset('assets/images/ybsthbj.png', fit: BoxFit.fitHeight),
              ),
            ),
            //SizedBox(height: 30),
            Center(
              child: Container(
                //margin: EdgeInsets.only(top: 30),
                height: ScreenUtil().setWidth(800),
                width: ScreenUtil().setWidth(800),
                child: FlatButton(
                  onPressed: () {
                    Navigator.pushNamed(context, '/faceLogin_take_pictuer', arguments: 'FaceLogin');
                  },
                  //已经为图片header1.png添加圆形边框
                  child: Stack(
                    children: <Widget>[
                      Align(
                        alignment: Alignment.center,
                        child: Container(
                          child: Image.asset('assets/images/header1.png', fit: BoxFit.cover),
                        ),
                      ),
                      Align(
                        alignment: Alignment.center,
                        child: Container(
                          alignment: Alignment(0, 0),
                          //192像素的图片显示出来为202像素，200 = 192 + 8
                          height: 200,
                          width: 200,
                          decoration: BoxDecoration(
                            //color: Colors.black,
                            shape: BoxShape.circle,
                            border: Border.all(color: Color.fromRGBO(88, 126, 211, 1), width: 5.0),
                            // borderRadius: BorderRadius.all(
                            //   Radius.circular(400),
                            // ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            SizedBox(
              height: 20,
            ),
            // Container(
            //   alignment: Alignment(0, 0),
            //   width: 200,
            //   height: 230,
            //   decoration: BoxDecoration(
            //     border: Border.all(color: Colors.orange, width: 1.0),
            //   ),
            //   child: _getImage(widget.arguments),
            // ),
          ],
        ),
      ),
    );
  }

  //定义一个组件显示图片
  Widget _getImage(String filePath) {
    if (null == filePath) {
      return Text("请选择图片...");
    }
    File _image = File(filePath);
    //return Image.file(_image);

    int imageWidth;
    int imageHeight;
    // 预先获取图片信息
    Image image = Image.file(File.fromUri(Uri.parse(filePath)));
    image.image
        .resolve(new ImageConfiguration())
        .addListener(new ImageStreamListener((ImageInfo info, bool _) {
      imageWidth = info.image.width;
      imageHeight = info.image.height;
    }));

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        //480*720
        Text('宽高：${imageWidth}x${imageHeight}', style: TextStyle(fontSize: 17.0)),
        Container(
          width: imageWidth / 4,
          height: imageHeight / 4,
          decoration: BoxDecoration(
            image: DecorationImage(image: AssetImage(filePath), fit: BoxFit.cover),
          ),
        )
      ],
    );
  }
}
