/*
 * @Author: XianKaiQun
 * @LastEditors  : WuWei
 * @Date: 2020-11-04 14:32:20
 * @LastEditTime : 2022-01-19 10:47:18
 */
import 'dart:io';

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

///[WImagePreview]嵌入式图片预览widget，
///提供`onDelete`事件，则右上角关闭小圈圈就会显示，不提供则不显示
class WImagePreviewFile extends StatelessWidget {
  const WImagePreviewFile({
    Key? key,
    required this.images,
    required this.index,
    required this.onDelete,
    this.size,
  }) : super(key: key);
  final List<File> images;
  final double? size;
  final int index;
  final void Function()? onDelete;

  ///一般来说，使用[WImagePreview]嵌入式图片预览widget时，没有意外的话都会直接构造一个列表，
  ///所以这里提供一个快速构建一个`List<Widget>`的方法，
  ///这样你可以使[WImagePreview]和
  static List<WImagePreviewFile> buildList({
    List<File>? images,
    void Function(int index)? onDelete,
    double? size,
  }) {
    return [
      if (images != null && images.length > 0)
        for (int i = 0; i < images.length; i++)
          WImagePreviewFile(
            size: size,
            images: images,
            index: i,
            onDelete: onDelete == null ? null : () => onDelete(i),
          )
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Wisdom.stack(
      width: size ?? 65.pt,
      height: size ?? 65.pt,
      clipBehavior: Clip.none,
      children: [
        Wisdom(
          onTap: () => WisPhotoViewFileUtil.show(images, index),
          padding: EdgeInsets.only(right: 5.pt, top: 5.pt),
          child: DecoratedBox(
            position: DecorationPosition.foreground,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(4.pt),
              border: Border.all(color: Colors.black12, width: 0.5.pt),
            ),
            child: WImage.file(
              images[index],
              borderRadius: BorderRadius.circular(4.pt),
              width: size ?? 65.pt,
              height: size ?? 65.pt,
              fit: BoxFit.cover,
            ),
          ),
        ),
        if (onDelete != null)
          Positioned(
            right: 0,
            top: 0,
            child: WClose(
              onTap: onDelete,
              backgroundColor: Colors.black.withOpacity(0.6),
              color: Colors.white,
            ),
          ),
      ],
    );
  }
}
