You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kivy中实现或模拟Marquee(跑马灯)功能?

实现长文件名路径的适配显示方案

方案一:自定义跑马灯(Marquee)组件

如果找不到现成的Marquee Widget,可以自己实现一个简单的滚动文本效果:

class MarqueeText extends StatefulWidget {
  final String text;
  final TextStyle? style;
  final Axis scrollDirection;
  final double scrollSpeed;

  const MarqueeText({
    super.key,
    required this.text,
    this.style,
    this.scrollDirection = Axis.horizontal,
    this.scrollSpeed = 20.0,
  });

  @override
  State<MarqueeText> createState() => _MarqueeTextState();
}

class _MarqueeTextState extends State<MarqueeText> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;
  double _textWidth = 0.0;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: (widget.text.length / widget.scrollSpeed).round()),
    )..repeat();
    _animation = Tween<double>(begin: 0, end: -1).animate(_controller);
  }

  @override
  void didUpdateWidget(covariant MarqueeText oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.text != widget.text) {
      _controller.duration = Duration(seconds: (widget.text.length / widget.scrollSpeed).round());
      _controller.reset();
      _controller.repeat();
    }
  }

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

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        return SizedBox(
          width: constraints.maxWidth,
          child: SingleChildScrollView(
            scrollDirection: widget.scrollDirection,
            physics: const NeverScrollableScrollPhysics(),
            child: AnimatedBuilder(
              animation: _animation,
              builder: (context, child) {
                return Transform.translate(
                  offset: Offset(_animation.value * _textWidth, 0),
                  child: child,
                );
              },
              child: Text(
                widget.text,
                style: widget.style,
                overflow: TextOverflow.visible,
                onLayout: (TextLayoutMetrics metrics) {
                  setState(() {
                    _textWidth = metrics.width;
                  });
                },
              ),
            ),
          ),
        );
      },
    );
  }
}

使用时直接替换原Label:

MarqueeText(text: "/very/long/path/to/your/file/name.txt")

方案二:截断显示+悬浮提示

如果不需要滚动效果,更轻量化的方式是截断文本,搭配悬浮提示展示完整路径:

Tooltip(
  message: "/very/long/path/to/your/file/name.txt",
  child: Text(
    "/very/long/path/to/your/file/name.txt",
    overflow: TextOverflow.ellipsis,
    maxLines: 1,
  ),
)

文本过长时自动显示省略号,用户移动端长按/桌面端hover即可查看完整路径。

方案三:多行自动换行

如果布局允许占用垂直空间,可设置文本自动换行:

Text(
  "/very/long/path/to/your/file/name.txt",
  maxLines: null, // 不限行数
  overflow: TextOverflow.visible,
)

适合有足够垂直空间的场景,避免横向溢出。

内容的提问来源于stack exchange,提问作者AMM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 00:45:24