import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:transparent_image/transparent_image.dart';
import 'package:wallpaper/pages/downloadpage/downloadpage_controller.dart';

class DownloadPageView extends GetView<DownloadPageController> {
  // const DownloadPageView({super.key});

  @override 
  Widget build(BuildContext context) {
    final Widget bodyView = getBodyWidget();
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: const Text("下载"),
      ),
      body:  bodyView,
    );
  }

  
  //返回内容
  Widget getBodyWidget(){
    return Obx(() => GridView.count(
              crossAxisCount: 2,
              mainAxisSpacing: 2,
              crossAxisSpacing: 2,
              childAspectRatio: 0.7,
              padding: const EdgeInsets.symmetric(horizontal: 5),
              // padding: const EdgeInsets.fromLTRB(5, 50, 5, 0),
              children: List.generate(controller.dataItems.length, (index) {
                return getBodyItem(controller.dataItems[index].localUrl!,index);
            }),
          ));
  }

  Stack getBodyItem(String fileName,int index) {
    var imgUrl = '${controller.localDir.value}/$fileName';
    return Stack(//堆叠效果
                  children: [
                    Image.file(
                        File(imgUrl),
                        width: double.infinity,
                        height: double.infinity,
                        fit: BoxFit.cover,
                      ),
                  Material(
                    color: Colors.transparent,
                    child: InkWell(
                          onTap: () {
                            //前往看大图
                            controller.goLocalScanPage(index);
                          },
                          child: Align(
                                  alignment: Alignment.bottomCenter,
                                  child: Container(
                                          color: Colors.black.withAlpha(160),
                                          height: 40,
                                          width: double.infinity,
                                          child: Row(
                                              mainAxisAlignment: MainAxisAlignment.center,
                                              mainAxisSize: MainAxisSize.max,
                              //                交叉轴的布局方式，对于column来说就是水平方向的布局方式
                                              crossAxisAlignment: CrossAxisAlignment.center,
                                              children: getButtomWidge(imgUrl,index),
                                            ),
                                        ),
                                ),
                  ),
                  ),
                  
                ],
            );
  }


  List<Widget> getButtomWidge(String imgUrl,int index){
    List<Widget> wg = [
                        IconButton(onPressed:() {
                          controller.saveImgToAlbum(imgUrl);

                        }, icon: const Icon(Icons.album),color: Colors.white,),
                      ];
      if (controller.isAndroid) {
          wg.add(IconButton(onPressed:() {
                          controller.setWallpaper(imgUrl);

                        }, icon: const Icon(Icons.wallpaper),color: Colors.white,));
      }
      wg.add(IconButton(onPressed:() {
                          controller.deleteWallpaper(imgUrl,index);

                        }, icon: const Icon(Icons.delete),color: Colors.red,));
      return wg;
  }
}