import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallpaper/pages/imgcategorypage/imgcategorypage_controller.dart';
// import 'package:wallpaper/models/imgnetodel.dart';
import 'package:transparent_image/transparent_image.dart';
import 'package:wallpaper/pages/imgscanpage/imgscanpage_view.dart';

class ImgCategoryPageView extends GetView<ImgCategoryPageController> {
  const ImgCategoryPageView({super.key});

  // const ImgCategoryPageView({super.key});
  // final String stitle;
  // final NetImgCategory dataModel;
  // const ImgCategoryPageView({super.key, required this.stitle,required this.dataModel});

  @override 
  Widget build(BuildContext context)  {
    final Widget bodyView =  getBodyWidget();

    return Scaffold(
      appBar: AppBar(
        title:  Obx(() => Text(controller.stitle.value)) ,
        centerTitle: true,
        // backgroundColor: Colors.transparent,
        // foregroundColor: Colors.white,
      ),
      body:  bodyView ,
    );
  }






  //返回内容
  Widget getBodyWidget() {
    return GridView.count(
            crossAxisCount: 2,
            mainAxisSpacing: 2,
            crossAxisSpacing: 2,
            childAspectRatio: 0.7,
            padding: const EdgeInsets.symmetric(horizontal: 5),
            children: List.generate(controller.dataModel.data!.length, (index)  {
              return  
              Scaffold(
                  backgroundColor: Colors.grey,
                  body: getBodyItem(controller.dataModel.data![index].previewThumb!, index),
                );
            }));
      // return  Obx(() => GridView.count(
      //       crossAxisCount: 2,
      //       mainAxisSpacing: 2,
      //       crossAxisSpacing: 2,
      //       childAspectRatio: 0.7,
      //       padding: const EdgeInsets.symmetric(horizontal: 5),
      //       children: List.generate(controller.dataModel.data!.length, (index)  {
      //         return  getBodyItem(controller.dataModel.data![index].previewThumb!, index);
      //       }),
      //   ));
  }

  Stack getBodyItem(String imgUrl,int index) {
    return Stack(//堆叠效果
                  children: [
                    CachedNetworkImage(
                      imageUrl: imgUrl,
                      width: double.infinity,
                      height: double.infinity,
                      fit: BoxFit.cover,
                      placeholder: (context, url) => const SizedBox(height: 30,width: 30,child: Center(
                        child: CircularProgressIndicator(color: Colors.white,),
                      ),),
                      errorWidget: (context, url, error) => const Icon(Icons.error_outline_outlined,color: Colors.white,),
                  ),

                  Material(
                    color: Colors.transparent,
                    child: InkWell(
                          onTap: () {
                            // Get.to(ImgScanPageView(dataModel: controller.dataModel,currentIndex: index,));
                            controller.goScanView(index, controller.dataModel);
                          },
                          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),
                                            ),
                                        ),
                                ),
                  ),
                  ),
                  
                ],
            );
  }


  List<Widget> getButtomWidge(String imgUrl){
    List<Widget> wg = [
                        IconButton(onPressed:()  {
                            controller.downloadImg(imgUrl);

                        }, icon: const Icon(Icons.download),color: Colors.white,),
                        
                        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,));
      }
      return wg;
  }


}