import 'dart:io';

import 'package:flutter/material.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';

class ViewImagePage extends StatefulWidget {
  ViewImagePage({Key key, this.img, this.edit = false, this.width})
      : super(key: key);
  final String img;
  final bool edit;
  final width;

  @override
  ViewImagePageState createState() => ViewImagePageState();
}

class ViewImagePageState extends State<ViewImagePage> {
  String image;
  var w;
  var index = 1;
  var _scrollController;
  var down = 0.0; //触开始的时候的left
  var half = false; //是否超过一半

  delete() {
    showAlert(
        context,
        "提示",
        "要删除这张照片吗？",
        "确定",
        () {
          Navigator.pop(context);
          Navigator.pop(context, true);
        },
        txt2: "取消",
        onPre2: () {
          Navigator.pop(context);
        });
  }

  @override
  void initState() {
    //页面初始化
    super.initState();
    _scrollController = ScrollController(
      initialScrollOffset: 0,
    );
    setState(() {
      image = widget.img;
    });
  }

  nextPage(w) {
    setState(() {
      index = index + 1;
      _scrollController.animateTo(
        (index - 1) * w,
        duration: Duration(milliseconds: 200),
        curve: Curves.easeIn,
      );
    });
  }

  lastPage(w) {
    setState(() {
      index = index - 1;
      _scrollController.animateTo(
        (index - 1) * w,
        duration: Duration(milliseconds: 200),
        curve: Curves.easeIn,
      );
    });
  }

  moveEnd(e, w, l) {
    var end = e.primaryVelocity;
    if (end > 10 && index > 1) {
      lastPage(w);
    } else if (end < -10 && index < l) {
      nextPage(w);
    } else if (half == true) {
      if (down > w / 2 && index < l) {
        //右边开始滑动超过一半,则下一张
        nextPage(w);
      } else if (down < w / 2 && index > 1) {
        lastPage(w);
      } else {
        _scrollController.animateTo(
          (index - 1) * w,
          duration: Duration(milliseconds: 200),
          curve: Curves.easeIn,
        );
      }
    } else {
      _scrollController.animateTo(
        (index - 1) * w,
        duration: Duration(milliseconds: 200),
        curve: Curves.easeIn,
      );
    }
  }

  imgMove(e, w, l) {
    //down 为起点
    var left = e.position.dx;
    var now = (index - 1) * w;
    var move = left - down; //移动距离
    if (left - down > w / 2 || down - left > w / 2) {
      half = true;
    } else {
      half = false;
    }
    _scrollController.jumpTo(now - move);
  }

  Widget list(w, l) {
    List<Widget> array = [];
    array.add(
      Listener(
//        onPointerMove: (e) {
//          imgMove(e,w,l);
//        },
//        onPointerDown: (e) {
//          down = e.position.dx;
//        },
        child: GestureDetector(
          onTap: () {
            Navigator.pop(context);
          },
          onHorizontalDragEnd: (e) {
            moveEnd(e, w, l);
          },
          child: Container(
            width: w,
            child: image != null
                ? image.contains("http")
                    ? Image.network(image)
                    : Image.file(File(image))
                : Container(),
          ),
        ),
      ),
    );
    return ListView(
      controller: _scrollController,
      scrollDirection: Axis.horizontal,
      children: array,
    );
  }

  @override
  Widget build(BuildContext context) {
    w = MediaQuery.of(context).size.width;
    return Scaffold(
        appBar: MyAppBar(
          centerTitle: "查看图片",
          actions: <Widget>[
            widget.edit
                ? FlatButton(
                    child: Text("删除"),
                    textColor: Colours.text,
                    highlightColor: Colors.transparent,
                    onPressed: () {
                      delete();
                    },
                  )
                : Container()
          ],
        ),
        body: Container(
          color: Colors.black,
          child: Stack(
            children: <Widget>[
              list(w, 1),
//          Positioned(
//            top: 20,
//            child: Container(
//                alignment: Alignment.center,
//                width: w,
//                child: Text('$index/$l',style: TextStyle(
//                  decoration: TextDecoration.none,
//                  color: Colors.white,fontSize: 16,
//                  fontWeight: FontWeight.w400,
//                  shadows: [
//                    Shadow(color: Colors.black, offset: Offset(1, 1)),
//                  ],
//                ))
//            ),
//          ),
//            Positioned(
//              top: 60,
//              right: 20,
//              child: Container(
//                alignment: Alignment.centerLeft,
//                width: 20,
//                child: GestureDetector(
//                  onTap: () {delete();},
//                  child: Icon(Icons.delete,color: Colors.white),
//                ),
//              ),
//            ),
            ],
          ),
        ));
  }
}
