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:yumi/shared/tools/sc_banner_utils.dart';
import 'package:provider/provider.dart';
import 'package:yumi/ui_kit/components/sc_compontent.dart';
import 'package:yumi/services/general/sc_app_general_manager.dart';

class RoomBannerView extends StatefulWidget {
  @override
  _RoomBannerViewState createState() => _RoomBannerViewState();
}

class _RoomBannerViewState extends State<RoomBannerView> {
  int _currentIndex = 0;

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

  _banner(SCAppGeneralManager ref) {
    return ref.roomBanners.isEmpty
        ? Container()
        : SizedBox(
          width: 46.w,
          child: Column(
            children: [
              CarouselSlider(
                options: CarouselOptions(
                  height: 46.w,
                  autoPlay: true,
                  // 启用自动播放
                  enlargeCenterPage: false,
                  // 居中放大当前页面
                  aspectRatio: 1 / 1,
                  // 宽高比
                  enableInfiniteScroll: true,
                  // 启用无限循环
                  autoPlayAnimationDuration: Duration(milliseconds: 800),
                  // 自动播放动画时长
                  viewportFraction: 1,
                  // 视口分数
                  onPageChanged: (index, reason) {
                    _currentIndex = index;
                    setState(() {});
                  },
                ),
                items:
                    ref.roomBanners.map((item) {
                      return GestureDetector(
                        child: netImage(url: item.smallCover ?? ""),
                        onTap: () {
                          print('ads:${item.toJson()}');
                          SCBannerUtils.openBanner(item, context);
                        },
                      );
                    }).toList(),
              ),
              SingleChildScrollView(
              scrollDirection:Axis.horizontal,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children:
                      ref.roomBanners.asMap().entries.map((entry) {
                        return Container(
                          width: 4.0,
                          height: 4.0,
                          margin: EdgeInsetsDirectional.fromSTEB(2, 3, 2, 0),
                          decoration: BoxDecoration(
                            shape: BoxShape.circle,
                            color:
                                _currentIndex == entry.key
                                    ? Colors.blue
                                    : Colors.white,
                          ),
                        );
                      }).toList(),
                ),
              ),
            ],
          ),
        );
  }
}
