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

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

相关推荐
GitLqr14 小时前
玩转 Flutter 中的 Stack 与 Positioned:解决 UI 重叠问题的实战指南
flutter·面试·全栈
唔661 天前
flutter web iOS 在浏览器加载中文慢的问题
前端·flutter·ios
恋猫de小郭1 天前
Jetpack Compose 8 月版正式发布,核心模块 1.12
android·前端·flutter
梦想的颜色2 天前
AI 时代小白 VibeCoding 做 APP:UniApp(含 Uni‑X)、Flutter 与 React Native+Expo全维度技术选型对比指南
flutter·react native·app·uniapp·vibecoding·unippx·app产品
坚果的博客2 天前
Flutter-OH 3.44.9-dev 悄然上线|首个 OpenHarmony Canary 预览版上线
flutter
大龄秃头程序员2 天前
Flutter 动画随笔:业务里真正高频的几类控件
flutter
天空之城--2 天前
Android Flutter行业最新动态与实用参考(2026年8月第2周)
android·flutter
恋猫de小郭2 天前
Flutter 3.47 发布,快来看看有什么更新吧
android·前端·flutter
大龄秃头程序员3 天前
一次 Flutter 动画 Demo 的整理与复盘
flutter