/*
 * @Author: XianKaiQun
 * @Date: 2020-07-08 11:51:57
 * @LastEditors  : WuWei
 * @LastEditTime : 2021-04-06 17:04:44
 * @Description:
 */
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

///本地图片预览
class WisPhotoViewFileUtil extends StatefulWidget {
  final List<File>? photoList;
  final int? index;
  WisPhotoViewFileUtil({this.photoList, this.index});

  static show(List<File> photoList, int? index) async {
    return await WNavUtil.instance!.push(
      PageRouteBuilder(
        opaque: false,
        transitionsBuilder: (context, animation, secondaryAnimation, child) {
          return ScaleTransition(
            scale: Tween(begin: 0.5, end: 1.0).animate(
              CurvedAnimation(
                parent: animation,
                curve: Curves.fastOutSlowIn,
              ),
            ),
            child: FadeTransition(
              opacity: Tween(begin: 0.0, end: 1.0).animate(
                CurvedAnimation(
                  parent: animation,
                  curve: Curves.fastOutSlowIn,
                ),
              ),
              child: child,
            ),
          );
        },
        pageBuilder: (context, animation, secondaryAnimation) {
          return WisPhotoViewFileUtil(
            photoList: photoList,
            index: index,
          );
        },
      ),
    );
  }

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

class _WisPhotoViewFileUtilState extends State<WisPhotoViewFileUtil> {
  int? currentIndex = 0;
  int? initialIndex = 0;
  late List<File> photoList;
  int get length => photoList.length;
  @override
  void initState() {
    currentIndex = widget.index;
    initialIndex = widget.index;
    photoList = widget.photoList ?? [];
    super.initState();
  }

  void onPageChanged(int index) {
    setState(() {
      currentIndex = index;
    });
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: CloseButton(),
        backgroundColor: Colors.transparent,
      ),
      extendBodyBehindAppBar: true,
      body: Wisdom(
        color: Colors.black,
        onTap: () => Navigator.pop(context),
        child: Stack(
          alignment: Alignment.bottomRight,
          children: <Widget>[
            PhotoViewGallery.builder(
              scrollDirection: Axis.horizontal,
              scrollPhysics: const BouncingScrollPhysics(),
              builder: (BuildContext context, int index) {
                return PhotoViewGalleryPageOptions(
                  imageProvider: FileImage(widget.photoList![index]),
                  initialScale: PhotoViewComputedScale.contained,
                  maxScale: PhotoViewComputedScale.contained * 3,
                  minScale: PhotoViewComputedScale.contained,
                  // heroAttributes: HeroAttributes(tag: galleryItems[index].id),
                );
              },
              itemCount: widget.photoList!.length,
              backgroundDecoration: BoxDecoration(
                color: Colors.black,
              ),
              pageController:
                  PageController(initialPage: initialIndex!), //点进去哪页默认就显示哪一页
              onPageChanged: onPageChanged,
            ),
            Container(
              padding: EdgeInsets.all(10.0),
              child: Text(
                "${currentIndex! + 1}/${photoList.length}",
                style: TextStyle(color: Colors.white),
              ),
            )
          ],
        ),
      ),
    );
  }
}
