import 'package:badges/badges.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:hyzp_ybqx/pages/Login/FaceReg.dart';
import 'package:hyzp_ybqx/pages/Login/ModifyPassword.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../components/commonFun.dart';
import '../../components/dioFun.dart';
import '../../widget/JdButton.dart';

class page6_download extends StatefulWidget {
  page6_download({Key key}) : super(key: key);

  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<page6_download> {
  dispose() {
    super.dispose();
  }

  String download_url;
  String last_ver;

  @override
  void initState() {
    super.initState();
    getNewverUrl().then((value) {
      download_url = value['downurl'];
      last_ver = value['ver'];
      print('download_url = ${download_url}');
      print('last_ver = ${last_ver}');
      if (mounted) {
        setState(() {});
      }
    });
  }

  ////////
  static onNullFun() {}

  Widget _getListTile(title,
      {String leadPath = '',
      Color leadColor,
      onTapFun = onNullFun,
      onLongPressFun = onNullFun,
      size = 16.0,
      bool bBadge = false}) {
    return Column(
      children: <Widget>[
        ListTile(
          leading: bBadge
              ? Badge(
                  position: BadgePosition.topEnd(top: -7, end: -12),
                  badgeContent: null,
                  child: Image.asset(
                    leadPath,
                    height: ScreenUtil().setHeight(78),
                    fit: BoxFit.fitHeight,
                  ),
                )
              : Image.asset(
                  leadPath,
                  height: ScreenUtil().setHeight(78),
                  fit: BoxFit.fitHeight,
                ),
          title: new Text(
            title,
            style: TextStyle(fontSize: size, decoration: TextDecoration.underline),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
          trailing: Image.asset('assets/images/copy.png', height: 30, fit: BoxFit.fitHeight),
          contentPadding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 0),
          enabled: true,
          onTap: onTapFun,
          onLongPress: onLongPressFun,
        ),
        // Divider(
        //   height: 1.0,
        // ),
      ],
    );
  }

  // 复制内容到剪切板
  // Clipboard.setData(ClipboardData(text:'内容'));

  Widget _getUrlRow(title,
      {String leadPath = '',
      Color leadColor,
      onTapFun = onNullFun,
      onLongPressFun = onNullFun,
      size = 16.0,
      bool bBadge = false}) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        InkWell(
          onTap: onTapFun,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image.asset(
                leadPath,
                height: ScreenUtil().setHeight(88),
                fit: BoxFit.fitHeight,
              ),
              SizedBox(width: ScreenUtil().setWidth(40)),
              Container(
                width: ScreenUtil().screenWidth - 150,
                // height: ScreenUtil().setHeight(200),
                // child: Text(
                //   title,
                //   style: TextStyle(fontSize: size, decoration: TextDecoration.underline),
                //   maxLines: 1,
                //   overflow: TextOverflow.ellipsis,
                // ),
                child: TextField(
                  // textAlignVertical: TextAlignVertical(y: 1.0),
                  onTap: onTapFun,
                  maxLines: 1,
                  cursorHeight: 60,
                  style: TextStyle(fontSize: size, decoration: TextDecoration.underline),
                  controller: TextEditingController(text: title),
                  readOnly: true,
                  decoration: InputDecoration(
                    //contentPadding: EdgeInsets.only(bottom: 16),
                    // hintText: title,
                    isCollapsed: true, //重点，相当于高度包裹的意思，必须设置为true，不然有默认奇妙的最小高度
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(30), borderSide: BorderSide.none),
                  ),
                ),
              )
            ],
          ),
        ),
        InkWell(
          onTap: () {
            // Flutter 复制文本到剪贴板
            Clipboard.setData(ClipboardData(text: download_url));
            //showToast('帮助信息已复制到剪贴板', textAlign: TextAlign.left);
            Fluttertoast.showToast(msg: 'apk下载地址已复制到剪贴板', gravity: ToastGravity.BOTTOM);
          },
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              SizedBox(width: ScreenUtil().setWidth(40)),
              Image.asset('assets/images/copy.png',
                  height: ScreenUtil().setHeight(88), fit: BoxFit.fitHeight),
            ],
          ),
        ),
      ],
    );
  }

  List<Widget> _listViewUser = [];

  Color _greenColor = Color.fromRGBO(36, 206, 192, 1); //绿色
  Color _deepBlueColor = Color.fromRGBO(79, 118, 230, 1); //深蓝
  Color _deepGreyColor = Color.fromRGBO(116, 139, 161, 1); //深灰
  Color _ligthBlueColor = Color.fromRGBO(80, 159, 245, 1); //亮蓝

  OnTap_FaceReg() async {
    Navigator.of(context).push(MaterialPageRoute(builder: (context) => FaceReg()));
  }

  OnTap_modify_password() {
    Navigator.of(context).push(MaterialPageRoute(builder: (context) => ModifyPassword()));
  }

  OnTap_download_apk() {
    _launchUrl(download_url);
  }

  _launchUrl(String url) async {
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      setState(() {
        // _status = 'Unable to launch url $url';
        Fluttertoast.showToast(
          msg: 'Unable to launch url $url!',
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
        );
      });
    }
  }

  ////////

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(ScreenUtil().setHeight(173)), // 设置appBar高度
        // 设置appBar高度
        child: AppBar(
          automaticallyImplyLeading: false,
          centerTitle: true,
          titleSpacing: 0.0,
          //设置title的左边距
          flexibleSpace: Container(
            //SizedBox(height: ScreenUtil().statusBarHeight), //显示顶部状态栏
            // SizedBox(height: ScreenUtil().setHeight(10)), //显示顶部状态栏
            padding: EdgeInsets.only(top: ScreenUtil().statusBarHeight), //留出顶部状态栏高度
            child: Container(
              //height: ScreenUtil().setHeight(173),
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                  colors: [
                    Color.fromRGBO(12, 186, 156, 1),
                    Color.fromRGBO(39, 127, 235, 1),
                  ],
                ),
              ),
              // decoration: BoxDecoration(
              //   gradient: LinearGradient(colors: [
              //     Color(0xFF0018EB),
              //     Color(0xFF01C1D9),
              //   ], begin: Alignment.bottomCenter, end: Alignment.topCenter),
              // ),
            ),
          ),
          title: Padding(
            padding: EdgeInsets.only(left: 0, right: 0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                getIconAndTextButton(
                  iconColor: Colors.white,
                  iconData: Icons.chevron_left_outlined,
                  onPress: () {
                    Navigator.pop(context);
                  },
                ),
                Expanded(
                  child: Text("软件下载（apk）",
                      style: TextStyle(color: Colors.white, fontSize: 20),
                      textAlign: TextAlign.center,
                      overflow: TextOverflow.ellipsis),
                ),
                SizedBox(width: 50),
              ],
            ),
          ),
        ),
      ),
      body: null == download_url || download_url.isEmpty
          ? getMoreWidget(color: Colors.black38)
          : Container(
              padding: EdgeInsets.only(top: 20, bottom: 20, left: 20, right: 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/user.png', fit: BoxFit.cover),
                      child: Image.asset('assets/images/ybsthbj.png', fit: BoxFit.fitHeight),
                    ),
                  ),
                  SizedBox(height: 40),
                  Text('扫码下载 ver: $last_ver', textAlign: TextAlign.center, style: TextStyle(fontSize: 20)),

                  //原文链接：https://blog.csdn.net/houruoyu3/article/details/112314090
                  // Text("普通二维码"),
                  // SizedBox(height: 20,),
                  // QrImage(data: "这是普通二维码",size: 100,),
                  // Text("中间有图片的二维码"),
                  // SizedBox(height: 10),
                  // QrImage(
                  //   data: "这是中间有图的二维码",
                  //   size: 100,
                  //   embeddedImage: NetworkImage(
                  //       "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2491682377,1019940373&fm=26&gp=0.jpg"),
                  // ),
                  Center(
                    child: QrImage(
                      data: download_url, //二维码文本内容
                      // data: "http://125.64.218.67:9909/downloads/hyzp_ybqx_1.4.13_20220121.apk",
                      size: 200,
                      // embeddedImage: AssetImage('assets/images/ybsthbj.png'),
                    ),
                  ),
                  SizedBox(height: 40),
                  Text('网址下载', textAlign: TextAlign.center, style: TextStyle(fontSize: 20)),
                  //账户管理项目
                  _getUrlRow(download_url,
                      size: 20.0,
                      leadPath: 'assets/images/download.png',
                      leadColor: _deepBlueColor,
                      onTapFun: OnTap_download_apk),
                  // _getListTile('人脸注册',
                  //     leadPath: 'assets/images/人脸注册.png',
                  //     leadColor: _ligthBlueColor,
                  //     onTapFun: OnTap_FaceReg),
                  SizedBox(height: 60),
                  JdButton(
                    height: ScreenUtil().setHeight(382),
                    //height: 126,
                    text: "返回",
                    color: Colors.blueAccent,
                    onTop: () {
                      Navigator.pop(context);
                    },
                  )
                ],
              ),
            ),
    );
  }
}
