marquee_text.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import 'package:flutter/material.dart';
  2. /// One-line text that scrolls only while [animate] is true.
  3. ///
  4. /// Long titles loop with a small gap; short titles (including "Nothing
  5. /// playing") stay static. Pausing stops the animation at its current offset,
  6. /// and resuming continues from there.
  7. class MarqueeText extends StatefulWidget {
  8. const MarqueeText({
  9. super.key,
  10. required this.text,
  11. this.style,
  12. this.animate = false,
  13. this.gap = 48,
  14. });
  15. final String text;
  16. final TextStyle? style;
  17. final bool animate;
  18. final double gap;
  19. @override
  20. State<MarqueeText> createState() => _MarqueeTextState();
  21. }
  22. class _MarqueeTextState extends State<MarqueeText>
  23. with SingleTickerProviderStateMixin {
  24. late final AnimationController _controller;
  25. bool _needsScroll = false;
  26. double _textWidth = 0;
  27. bool _syncScheduled = false;
  28. bool get _shouldAnimate => widget.animate && _needsScroll;
  29. @override
  30. void initState() {
  31. super.initState();
  32. _controller = AnimationController(
  33. vsync: this,
  34. duration: const Duration(seconds: 10),
  35. )..addStatusListener(_onStatus);
  36. }
  37. void _onStatus(AnimationStatus status) {
  38. if (status == AnimationStatus.completed && _shouldAnimate) {
  39. _controller.value = 0;
  40. _controller.forward();
  41. }
  42. }
  43. @override
  44. void didUpdateWidget(covariant MarqueeText oldWidget) {
  45. super.didUpdateWidget(oldWidget);
  46. if (oldWidget.text != widget.text || oldWidget.style != widget.style) {
  47. _controller.value = 0;
  48. _needsScroll = false;
  49. }
  50. _scheduleSync();
  51. }
  52. @override
  53. void dispose() {
  54. _controller.dispose();
  55. super.dispose();
  56. }
  57. void _scheduleSync() {
  58. if (_syncScheduled) return;
  59. _syncScheduled = true;
  60. WidgetsBinding.instance.addPostFrameCallback((_) {
  61. _syncScheduled = false;
  62. if (mounted) _sync();
  63. });
  64. }
  65. void _sync() {
  66. if (_shouldAnimate) {
  67. if (!_controller.isAnimating) {
  68. if (_controller.value >= 1) _controller.value = 0;
  69. _controller.forward();
  70. }
  71. } else {
  72. _controller.stop();
  73. }
  74. }
  75. @override
  76. Widget build(BuildContext context) {
  77. return LayoutBuilder(
  78. builder: (context, constraints) {
  79. final style = widget.style ?? DefaultTextStyle.of(context).style;
  80. final painter = TextPainter(
  81. text: TextSpan(text: widget.text, style: style),
  82. maxLines: 1,
  83. textDirection: Directionality.of(context),
  84. textScaler: MediaQuery.textScalerOf(context),
  85. )..layout();
  86. final width = painter.width;
  87. final needsScroll =
  88. constraints.hasBoundedWidth && width > constraints.maxWidth;
  89. if (width != _textWidth || needsScroll != _needsScroll) {
  90. _textWidth = width;
  91. _needsScroll = needsScroll;
  92. _scheduleSync();
  93. }
  94. if (!needsScroll) {
  95. return Text(
  96. widget.text,
  97. maxLines: 1,
  98. overflow: TextOverflow.ellipsis,
  99. style: style,
  100. );
  101. }
  102. final duration = Duration(
  103. milliseconds: ((width / 28) * 1000).clamp(6000, 18000).round(),
  104. );
  105. if (_controller.duration != duration) {
  106. _controller.duration = duration;
  107. }
  108. return ClipRect(
  109. child: AnimatedBuilder(
  110. animation: _controller,
  111. builder: (context, _) {
  112. final offset = _controller.value * (width + widget.gap);
  113. return OverflowBox(
  114. alignment: Alignment.centerLeft,
  115. maxWidth: double.infinity,
  116. maxHeight: constraints.maxHeight,
  117. child: Transform.translate(
  118. offset: Offset(-offset, 0),
  119. child: SizedBox(
  120. width: width * 2 + widget.gap,
  121. child: Row(
  122. mainAxisSize: MainAxisSize.min,
  123. children: [
  124. Text(
  125. widget.text,
  126. maxLines: 1,
  127. softWrap: false,
  128. style: style,
  129. ),
  130. SizedBox(width: widget.gap),
  131. Text(
  132. widget.text,
  133. maxLines: 1,
  134. softWrap: false,
  135. style: style,
  136. ),
  137. ],
  138. ),
  139. ),
  140. ),
  141. );
  142. },
  143. ),
  144. );
  145. },
  146. );
  147. }
  148. }