import 'dart:async';
import 'package:flutter/material.dart';
/// 首页隐患播报
class DangerAutoScrolllist extends StatefulWidget {
  final List<dynamic> items;
  final double height;

  const DangerAutoScrolllist({
    super.key,
    required this.items,
    this.height = 150,
  });

  @override
  State<DangerAutoScrolllist> createState() => _AutoScrollListState();
}

class _AutoScrollListState extends State<DangerAutoScrolllist> {
  final ScrollController _scrollController = ScrollController();
  Timer? _timer;
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _startAutoScroll();
  }

  void _startAutoScroll() {
    _timer = Timer.periodic(const Duration(seconds: 3), (timer) {
      if (!mounted || widget.items.isEmpty) return;

      final itemHeight = widget.height / 2; // 你可以根据实际内容高度调整
      _currentIndex++;

      if (_currentIndex * itemHeight >= _scrollController.position.maxScrollExtent) {
        _currentIndex = 0;
        _scrollController.jumpTo(0);
      } else {
        _scrollController.animateTo(
          _currentIndex * itemHeight,
          duration: const Duration(milliseconds: 500),
          curve: Curves.easeInOut,
        );
      }
    });
  }

  @override
  void dispose() {
    _timer?.cancel();
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final List<dynamic> dataList = widget.items;

    return SizedBox(
      height: widget.height,
      child: ListView.builder(
        controller: _scrollController,
        itemExtent: widget.height / 2, // 每行高度固定
        itemCount: dataList.length,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (context, index) {
          final item = dataList[index];
          return GestureDetector(
            onTap: () {
              print("点击了 ${item['HIDDENDESCR']}");
              // 可以在这里跳转或弹窗
            },
            child: Row(
              children: [
                Expanded(child: Text(item['HIDDENDESCR'] ?? '-', maxLines: 1, overflow: TextOverflow.ellipsis)),
                Expanded(child: Text(item['CREATORNAME'] ?? '-', maxLines: 1, overflow: TextOverflow.ellipsis)),
                Expanded(child: Text(item['CREATTIME'] ?? '-', maxLines: 1, overflow: TextOverflow.ellipsis)),
              ],
            ),
          );
        },
      ),
    );
  }
}