import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class SCPartiallyBoldMarquee extends StatefulWidget {
  String boldText1 = "";
  String boldText2 = "";
  String normalText1 = "";
  String normalText2 = "";

  @override
  _PartiallyBoldMarqueeState createState() => _PartiallyBoldMarqueeState();

  SCPartiallyBoldMarquee(
    this.boldText1,
    this.boldText2,
    this.normalText1,
    this.normalText2,
  );
}

class _PartiallyBoldMarqueeState extends State<SCPartiallyBoldMarquee>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;
  bool goMarquee = false;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: Duration(seconds: 20), // 总滚动时间，需调试
      vsync: this,
    )..repeat(); // 无限循环
    _animation = Tween<double>(begin: 0.2, end: -1.0).animate(_controller);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      physics: const NeverScrollableScrollPhysics(), // 禁用用户手势
      child: AnimatedBuilder(
        animation: _animation,
        builder: (context, child) {
          return Transform.translate(
            offset: Offset(_animation.value * 650, 0),
            // 500为估算的滚动距离，需根据文本宽度精确计算
            child: RichText(
              text: TextSpan(
                children: [
                  TextSpan(
                    text: widget.boldText1,
                    style: TextStyle(
                      fontSize: 13.sp,
                      color: Colors.black,
                      fontFamily: 'MyCustomFont',
                      fontWeight: FontWeight.w900,
                    ),
                  ),
                  TextSpan(
                    text: widget.normalText1,
                    style: TextStyle(
                      fontSize: 12.sp,
                      color: Colors.black,
                      fontFamily: 'MyCustomFont',
                      fontWeight: FontWeight.w600, // 加粗
                    ),
                  ),
                  TextSpan(
                    text: widget.boldText2,
                    style: TextStyle(
                      fontSize: 13.sp,
                      color: Colors.black,
                      fontFamily: 'MyCustomFont',
                      fontWeight: FontWeight.w900,
                    ),
                  ),
                  TextSpan(
                    text: widget.normalText2,
                    style: TextStyle(
                      fontSize: 12.sp,
                      color: Colors.black,
                      fontFamily: 'MyCustomFont',
                      fontWeight: FontWeight.w600, // 加粗
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}
