Flutter 文字上下滚动切换 用于公告消息提示

布满荆棘的人生 2023-10-07 19:16 92阅读 0赞

效果预览

在这里插入图片描述

1 基本使用代码
  1. void main() {
  2. ///启动根目录
  3. runApp(MaterialApp(
  4. home: TestTipsPage(),
  5. ));
  6. }
  7. class TestTipsPage extends StatefulWidget {
  8. @override
  9. _TestTipsPageState createState() => _TestTipsPageState();
  10. }
  11. class _TestTipsPageState extends State<TestTipsPage> {
  12. @override
  13. void initState() {
  14. super.initState();
  15. }
  16. @override
  17. Widget build(BuildContext context) {
  18. return Scaffold(
  19. backgroundColor: Colors.white,
  20. body: Center(
  21. child: Container(
  22. alignment: Alignment.center,
  23. width: double.infinity,
  24. height: 24,
  25. child: buildMarqueeWidget(),
  26. ),
  27. ),
  28. );
  29. }
  30. }
2 核心使用代码
  1. MarqueeWidget buildMarqueeWidget(List<String> loopList) {
  2. ///上下轮播 安全提示
  3. return MarqueeWidget(
  4. //子Item构建器
  5. itemBuilder: (BuildContext context, int index) {
  6. String itemStr = loopList[index];
  7. //通常可以是一个 Text文本
  8. return Text(itemStr);
  9. },
  10. //循环的提示消息数量
  11. count: loopList.length,
  12. );
  13. }
3 核心实现代码
  1. import 'dart:async';
  2. import 'package:flutter/cupertino.dart';
  3. // 上下滚动的消息轮播
  4. class MarqueeWidget extends StatefulWidget {
  5. /// 子视图数量
  6. final int count;
  7. ///子视图构建器
  8. final IndexedWidgetBuilder itemBuilder;
  9. ///轮播的时间间隔
  10. final int loopSeconds;
  11. const MarqueeWidget({
  12. Key? key,
  13. required this.count,
  14. required this.itemBuilder,
  15. this.loopSeconds = 5,
  16. }) : super(key: key);
  17. @override
  18. _MarqueeWidgetState createState() => _MarqueeWidgetState();
  19. }
  20. class _MarqueeWidgetState extends State<MarqueeWidget> {
  21. late PageController _controller;
  22. late Timer _timer;
  23. @override
  24. void initState() {
  25. super.initState();
  26. _controller = PageController();
  27. _timer = Timer.periodic(Duration(seconds: widget.loopSeconds), (timer) {
  28. if (_controller.page != null) {
  29. // 如果当前位于最后一页,则直接跳转到第一页,两者内容相同,跳转时视觉上无感知
  30. if (_controller.page!.round() >= widget.count) {
  31. _controller.jumpToPage(0);
  32. }
  33. _controller.nextPage(
  34. duration: const Duration(seconds: 1), curve: Curves.linear);
  35. }
  36. });
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. return PageView.builder(
  41. scrollDirection: Axis.vertical,
  42. controller: _controller,
  43. itemBuilder: (buildContext, index) {
  44. if (index < widget.count) {
  45. return widget.itemBuilder(buildContext, index);
  46. } else {
  47. return widget.itemBuilder(buildContext, 0);
  48. }
  49. },
  50. itemCount: widget.count + 1,
  51. );
  52. }
  53. @override
  54. void dispose() {
  55. super.dispose();
  56. _controller.dispose();
  57. _timer.cancel();
  58. }
  59. }

发表评论

表情:
评论列表 (有 0 条评论,92人围观)

还没有评论,来说两句吧...

相关阅读

    相关 marquee标签_html滚动

    marquee标签可以让我们在网页中编写的文字动起来,不论是上下滚动还是左右滚动都可以通过marquee标签中的属性来设置。语法:﹤marquee﹥需要被滚动的文字﹤/marq