import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';

class RMarquee extends StatefulWidget {
  const RMarquee({super.key, required this.text, this.duration = const Duration(seconds: 5)});

  final String text;
  final Duration duration;

  @override
  State<RMarquee> createState() => _RMarqueeState();
}

class _RMarqueeState extends State<RMarquee> with SingleTickerProviderStateMixin {
  late ScrollController _scrollController;
  late double _textWidth;
  late double _screenWidth;

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      _startScrolling();
    });
  }

  void _startScrolling() async {
    while (true) {
      await _scrollController.animateTo(
        _scrollController.position.maxScrollExtent,
        duration: widget.duration,
        curve: Curves.linear,
      );

      await _scrollController.animateTo(0, duration: Duration(seconds: 0), curve: Curves.linear);
    }
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 40,
      child: ListView(
        controller: _scrollController,
        scrollDirection: Axis.horizontal,
        children: [
          Text(widget.text, style: AppFonts.yekan16Bold),
          SizedBox(width: 50),
        ],
      ),
    );
  }
}
