78 lines
2.1 KiB
Dart
78 lines
2.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
|
|
class SeamlessAutoScroll extends StatefulWidget {
|
|
final List<Widget> items;
|
|
final double itemHeight;
|
|
final Duration scrollInterval;
|
|
|
|
const SeamlessAutoScroll({
|
|
Key? key,
|
|
required this.items,
|
|
this.itemHeight = 120.0,
|
|
this.scrollInterval = const Duration(seconds: 2),
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
_AutoScrollListState createState() => _AutoScrollListState();
|
|
}
|
|
|
|
class _AutoScrollListState extends State<SeamlessAutoScroll> {
|
|
final ScrollController _scrollController = ScrollController();
|
|
Timer? _timer;
|
|
int _currentIndex = 0;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_startAutoScroll();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_timer?.cancel();
|
|
_scrollController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _startAutoScroll() {
|
|
_timer = Timer.periodic(widget.scrollInterval, (timer) {
|
|
if (_currentIndex < widget.items.length) {
|
|
// 滚动到下一个项目
|
|
_scrollController.animateTo(
|
|
_currentIndex * widget.itemHeight,
|
|
duration: const Duration(milliseconds: 550),
|
|
curve: Curves.easeIn,
|
|
);
|
|
_currentIndex++;
|
|
} else {
|
|
// 滚动到底部后回到顶部重新开始
|
|
_currentIndex = 0;
|
|
_scrollController.jumpTo(0);
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Container(
|
|
height: 110.w, // 固定容器高度
|
|
decoration: BoxDecoration(borderRadius: BorderRadius.circular(8)),
|
|
child: ListView.builder(
|
|
controller: _scrollController,
|
|
physics: const NeverScrollableScrollPhysics(), // 禁用手动滚动
|
|
itemCount: widget.items.length,
|
|
itemBuilder: (context, index) {
|
|
return Container(
|
|
height: widget.itemHeight,
|
|
alignment: Alignment.centerLeft,
|
|
child: widget.items[index],
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|