
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallpaper/models/imgnetodel.dart';

import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:wallpaper/pages/localimgscanpage/localimgscanpage_controller.dart';


class LocalImgScanPageView extends GetView<LocalImgScanPageController> {
  // final NetImgCategory dataModel;
  // final int currentIndex;

  // const ImgScanPageView({super.key,required this.dataModel,required this.currentIndex});
  
  @override 
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      backgroundColor: Colors.black,
      appBar:PreferredSize(
              preferredSize: const Size.fromHeight(44),
              child: Obx(() => controller.showAppbar.value == 0 ? Text("") :AppBar(
              centerTitle: true,
              title: const Text("查看大图"),
              
              backgroundColor: Colors.transparent,
              foregroundColor: Colors.white,
            )),
        ),
      body: Stack(
        children: [
          GetBuilder<LocalImgScanPageController>(
            builder: (logic) {
              return PhotoViewGallery.builder(
                    scrollPhysics: const BouncingScrollPhysics(),
                    builder: (BuildContext context, int index) {
                      return PhotoViewGalleryPageOptions(
                        imageProvider:FileImage(File('${controller.localDir}/${controller.dataModel[index].localUrl!}')),
                        initialScale: PhotoViewComputedScale.contained * 0.8,
                        minScale: PhotoViewComputedScale.contained*0.4,
                        maxScale: PhotoViewComputedScale.contained * 4,
                        onTapDown: (context, details, controllerValue) {
                          controller.showAppbarAction();
                        },
                        // heroAttributes: PhotoViewHeroAttributes(tag: galleryItems[index].id),
                      );
                    },
                    pageController: controller.pcontroller,
                    itemCount: controller.dataModel.length,
                    loadingBuilder: (context, event) => Center(
                      child: SizedBox(
                        width: 20.0,
                        height: 20.0,
                        child: CircularProgressIndicator(
                          value: event == null
                              ? 0
                              : event.cumulativeBytesLoaded / event.expectedTotalBytes!,
                        ),
                      ),
                    ),
                    // backgroundDecoration: widget.backgroundDecoration,
                    // pageController: widget.pageController,
                    onPageChanged: onPageChanged,
                    

              );
            },
          ),
          Align(
            alignment: Alignment.bottomCenter,
            child: Container(
                    color: Colors.black.withAlpha(160),
                    height: 100,
                    width: double.infinity,
                    child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        mainAxisSize: MainAxisSize.max,
        //                交叉轴的布局方式，对于column来说就是水平方向的布局方式
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: getButtomWidge(),
                      ),
                  ),
          ),
        ],
      ),
    );
  }

  void onPageChanged(int index){
    print("切换了图片....");
    controller.currentIndex.value = index;
  }


  List<Widget> getButtomWidge(){
    List<Widget> wg = [
                          const Spacer(
                                              ),
                          IconButton(onPressed:() {
                            print("保存壁纸");
                            controller.saveImgToAlbum();

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

                        }, icon: const Icon(Icons.wallpaper),color: Colors.white,));
          wg.add(const Spacer(
                          ));
      }
      return wg;
  }
}