import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/models/app_config_model.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:zhiying_base_widget/widgets/home/home_quick_entry/cached_network_image_util.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class GuidePage extends StatefulWidget {
  final AppConfigGuideModel model;
  final List<Uint8List> imageDatas;

  const GuidePage(this.model, this.imageDatas, {Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _GuidePageState();
}

class _GuidePageState extends State<GuidePage> {
  @override
  Widget build(BuildContext context) {
    bool isShowIndicator = (widget.model?.isShowIndicator ?? '0') == '1';
    List<int> list = List.generate(widget.imageDatas?.length, (index) => index);
    return WillPopScope(
      onWillPop: () async => false,// 拦截Android返回键
      child: Material(
        child: Swiper(
          itemBuilder: (BuildContext context, int index) {
            // return CachedNetworkImage(imageUrl: widget.model.images[index],fit: BoxFit.cover,);
            return Image.memory(widget.imageDatas[index], fit: BoxFit.cover,);
          },

          loop: false,
          itemCount: widget.imageDatas?.length ?? 0,
          pagination: isShowIndicator ? SwiperCustomPagination(builder: (BuildContext context, SwiperPluginConfig config) {
            return Align(
              alignment: Alignment(0.0, 1),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: list.map((index) {
                  var borderRadius;
                  if (index == 0) {
                    borderRadius = BorderRadius.only(topLeft: Radius.circular(2), bottomLeft: Radius.circular(2));
                  }
                  if (index == list.length - 1) {
                    borderRadius = BorderRadius.only(topRight: Radius.circular(2), bottomRight: Radius.circular(2));
                  }

                  if (index == config.activeIndex) {
                    borderRadius = BorderRadius.all(Radius.circular(2));
                  }

                  return SafeArea(
                    child: Container(
                      margin: EdgeInsets.only(bottom: 40),
                      height: 4,
                      width: 25,
                      decoration: BoxDecoration(borderRadius: borderRadius, color: index == config.activeIndex ? HexColor.fromHex('#FF4242') : HexColor.fromHex('#FFFFFF')),
                    ),
                  );
                }).toList() ,
              ),
            );
          }) : null,
          onIndexChanged: (index) {
          },
          onTap: (index) {
            if (index == widget.model.images.length - 1) {
              Navigator.pop(context, true);
            }
          },
        ),
      ),
    );
  }
}
