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();
  }

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

相关推荐
SoaringHeart1 天前
Flutter进阶:基于 EasyRefresh 的下拉刷新封装 n_easy_refresh_mixin.dart
前端·flutter
月光下的丝瓜2 天前
Flutter 国内安装指南
前端·flutter
恋猫de小郭4 天前
Amper 正式转正 Kotlin Toolchain ,Gradle 未来何去何从
android·前端·flutter
张风捷特烈4 天前
Flutter 类库大揭秘#02 | path_provider 各平台实现
前端·flutter
TT_Close5 天前
别劝退了!5秒搞定 Flutter 鸿蒙 FVM 起跑线
flutter·harmonyos·visual studio code
你听得到115 天前
用户说 App 卡,但说不清在哪?我把 Flutter 监控 SDK 升级成了链路观测工作台
前端·flutter·性能优化
stringwu7 天前
Flutter 开发必备:MVI 架构的高效实现指南
前端·flutter
程序员老刘8 天前
Flutter版本选择指南:3.44系列继续观望 | 2026年6月
flutter·ai编程·客户端
用户965597361909 天前
Provider vs Bloc vs GetX vs Riverpod:Flutter 状态管理方案怎么选?
flutter
恋猫de小郭9 天前
Flutter Patchwork,不用 Fork 改依赖包源码的第三方工具
android·前端·flutter