import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:yumi/ui_kit/components/sc_compontent.dart';
import 'package:yumi/shared/tools/sc_banner_utils.dart';
import 'package:yumi/services/general/sc_app_general_manager.dart';

class ExploreBannerView extends StatefulWidget {
  @override
  _ExploreBannerViewState createState() => _ExploreBannerViewState();
}

class _ExploreBannerViewState extends State<ExploreBannerView> {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Consumer<SCAppGeneralManager>(
      builder: (context, ref, child) {
        return _banner(ref);
      },
    );
  }

  _banner(SCAppGeneralManager ref) {
    if (ref.exploreBanners.isEmpty) {
      return Container();
    }
    return Container(
      child: Stack(
        alignment: AlignmentDirectional.bottomCenter,
        children: [
          CarouselSlider(
            options: CarouselOptions(
              height: 118.w,
              autoPlay: true,
              // 启用自动播放
              enlargeCenterPage: false,
              // 居中放大当前页面
              enableInfiniteScroll: true,
              // 启用无限循环
              autoPlayAnimationDuration: Duration(milliseconds: 800),
              // 自动播放动画时长
              viewportFraction: 1,
              // 视口分数
              onPageChanged: (index, reason) {
                _currentIndex = index;
                setState(() {});
              },
            ),
            items:
                ref.exploreBanners.map((item) {
                  return GestureDetector(
                    child: netImage(
                      url: item.cover ?? "",
                      height: 118.w,
                      fit: BoxFit.fill,
                      borderRadius: BorderRadius.circular(12.w),
                    ),
                    onTap: () {
                      print('ads:${item.toJson()}');
                      SCBannerUtils.openBanner(item,context);
                    },
                  );
                }).toList(),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children:
                ref.exploreBanners.asMap().entries.map((entry) {
                  return Container(
                    width: 6.0,
                    height: 6.0,
                    margin: EdgeInsets.symmetric(
                      vertical: 8.0,
                      horizontal: 4.0,
                    ),
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color:
                          _currentIndex == entry.key
                              ? Colors.blue
                              : Colors.white,
                    ),
                  );
                }).toList(),
          ),
        ],
      ),
    );
  }
}
