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 天前
Dart 之 JSON
android·flutter·dart
恋猫de小郭2 天前
站起来,Flutter 要支持真正的 3D Texture 了
android·前端·flutter
黑科技iOS上架3 天前
iOS4.3合规自测手段:二进制分析
flutter·ios·cocoa·审核
好的佩奇4 天前
蓝牙之 GATT 数据模型
android·flutter·蓝牙
王二蛋与他的张大花5 天前
后台内存 827MB,我一行业务逻辑都没改就砍到 100MB
android·flutter·性能优化
恋猫de小郭5 天前
Flutter 的FFI/JNI 插件终于能区分不同的 FlutterEngine 了
android·前端·flutter
晚霞的不甘6 天前
Flutter/OpenHarmony 三方库适配实战:google_maps_flutter 地图
flutter·macos·华为·cocoa·harmonyos·1024程序员节
恋猫de小郭6 天前
Flutter HC 旋转 ANR,而且还和之前的 HCPP 问题类似
android·前端·flutter
恋猫de小郭7 天前
Flutter Perfetto:Dart Heap Sampling 终于要接入 Android 原生内存分析了
android·前端·flutter
程序员老刘8 天前
Flutter版本选择指南:尽早升级3.47.9 | 2026年11月
flutter·ai编程·客户端