Flutter 无限滚动组件实现ListView

Flutter 无限滚动组件实现ListView 方法:


dart 复制代码
  Widget _buildSroll() {
    return Container(
      height: 30,
      width: double.infinity,
      child: ScrollConfiguration(
        behavior: ScrollConfiguration.of(context).copyWith(
          scrollbars: false, // 隐藏滚动条
        ),
        child: ListView.builder(
          controller: _scrollController,
          scrollDirection: Axis.vertical,
          physics: NeverScrollableScrollPhysics(), // 禁止用户手动滚动
          itemCount: notifications.length * 100, // 实现无限循环效果
          itemBuilder: (context, index) {
            int realIndex = index % notifications.length;
            return Container(
              height: 30,
              padding: EdgeInsets.symmetric(horizontal: 15),
              alignment: Alignment.centerLeft,
              child: Row(
                children: [
                  Icon(Icons.notifications_active, size: 14, color: Colors.red),
                  SizedBox(width: 8),
                  Text(
                    notifications[realIndex],
                    style: TextStyle(fontSize: 12, color: Colors.black87),
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
dart 复制代码
 void _startAutoScroll() {
    _scrollTimer = Timer.periodic(Duration(seconds: 2), (timer) {
      if (_scrollController != null && _scrollController!.hasClients) {
        // 每次滚动一个完整的通知高度 (30px)
        _scrollController!.animateTo(
          _scrollController!.offset + 30,
          duration: Duration(milliseconds: 500),
          curve: Curves.easeInOut,
        );
      }
    });
  }
dart 复制代码
  ScrollController? _scrollController;
  Timer? _scrollTimer;
  final List<String> notifications = [
    "春节特惠:全场酒店8折起",
    "新用户专享:首单立减100元",
    "限时活动:预订即送早餐",
    "会员福利:积分兑换优惠券",
  ];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
   _ scrollController = ScrollController();
    _startAutoScroll();
  }

  @override
  void dispose() {
    _scrollTimer?.cancel();
    _scrollController?.dispose();
    super.dispose();
  }

复制过去即可使用(通过%取模运算实现)

相关推荐
程序员老刘1 天前
跨平台开发地图:金三银四你准备好了吗? | 2026年3月
flutter·客户端
恋猫de小郭1 天前
Kotlin 在 2.0 - 2.3 都更新了什么特性,一口气带你看完这两年 Kotlin 更新
android·前端·flutter
左手厨刀右手茼蒿1 天前
Flutter 三方库 all_lint_rules_community 的鸿蒙化适配指南 - 在鸿蒙系统上构建极致、严谨、基于全量社区 Lint 规则的工业级静态代码质量与安全审计引擎
flutter·harmonyos·鸿蒙·openharmony·all_lint_rules_community
雷帝木木1 天前
Flutter for OpenHarmony:Flutter 三方库 cbor 构建 IoT 设备的极致压缩防窃协议(基于标准二进制 JSON 表达格式)
网络·物联网·flutter·http·json·harmonyos·鸿蒙
Zender Han1 天前
Flutter Bloc / Cubit 最新详解与实战指南(2026版)
android·flutter·ios
王码码20351 天前
Flutter 三方库 servicestack 的鸿蒙化适配指南 - 实现企业级 Message-based 架构集成、支持强类型 JSON 序列化与跨端服务调用同步
flutter·harmonyos·鸿蒙·openharmony·message-based
里欧跑得慢1 天前
Flutter 三方库 jsonata_dart 的鸿蒙化适配指南 - 实现高性能的 JSON 数据查询与转换、支持 JSONata 表达式引擎与端侧复杂数据清洗
flutter·harmonyos·鸿蒙·openharmony·jsonata_dart
Justin在掘金1 天前
Flutter Riverpod 状态管理深入分析
flutter
Justin在掘金1 天前
Flutter BLoC 状态管理框架深入分析
flutter
weixin_443478511 天前
flutter组件学习之Cupertino 组件(iOS风格)
学习·flutter·ios