Try this code I tried to implement it with ListView this is the code:
class SmoothInfiniteSlider extends StatefulWidget {
const SmoothInfiniteSlider({super.key});
@override
State<SmoothInfiniteSlider> createState() => _SmoothInfiniteSliderState();
}
class _SmoothInfiniteSliderState extends State<SmoothInfiniteSlider> {
final ScrollController _scrollController = ScrollController();
Timer? _autoScrollTimer;
bool _isUserInteracting = false;
List<String> items = [
"🎃 History of Halloween?",
"🍊 Tangerine vs Orange",
"⚙️ Tech Trends",
"🐝 Why do we only see one side of the moon?",
"💰 How to save money?",
"🚀 Space Facts",
"🌍 The world",
"🤺 Fencing practice set price",
"🐍 What is Python?",
];
double _scrollSpeed = 1.0; // Adjust speed
@override
void initState() {
super.initState();
_startAutoScroll();
}
void _startAutoScroll() {
_autoScrollTimer?.cancel();
_autoScrollTimer = Timer.periodic(Duration(milliseconds: 30), (timer) {
if (_scrollController.hasClients && !_isUserInteracting) {
_scrollController.jumpTo(_scrollController.offset + _scrollSpeed);
if (_scrollController.offset >=
_scrollController.position.maxScrollExtent - 200) {
_scrollController.jumpTo(0);
}
}
});
}
@override
void dispose() {
_autoScrollTimer?.cancel();
_scrollController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// Duplicate the list to create an infinite scrolling effect
List<String> infiniteItems = [...items, ...items];
return Scaffold(
backgroundColor: Colors.white,
body: GestureDetector(
onPanDown: (_) {
_isUserInteracting = true;
},
onPanCancel: () {
_isUserInteracting = false;
_startAutoScroll();
},
onPanEnd: (_) {
_isUserInteracting = false;
_startAutoScroll();
},
child: ListView.builder(
controller: _scrollController,
scrollDirection: Axis.horizontal,
physics: BouncingScrollPhysics(), // Allow manual scroll
itemCount: infiniteItems.length,
itemBuilder: (context, index) {
final text = infiniteItems[index];
return Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Center(child: Text(text, style: TextStyle(fontSize: 16))),
);
},
),
),
);
}
}
which is not exactly the the way you wanted but you can get the idea. I also implemented it with gridview which was will be somewhat near to what you need if you implemented it StaggeredGridView which allow different size of children. or you can use marquee package for this functionality.
PageViewcode then, how did you use itsPageController?