Skip to content

Instantly share code, notes, and snippets.

@alwerr
Created November 19, 2024 05:21
Show Gist options
  • Save alwerr/c27f13e04602ee4c690c81b7a7c3a489 to your computer and use it in GitHub Desktop.
Save alwerr/c27f13e04602ee4c690c81b7a7c3a489 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: AutoScrollListView(),
);
}
}
class AutoScrollListView extends StatefulWidget {
@override
_AutoScrollListViewState createState() => _AutoScrollListViewState();
}
class _AutoScrollListViewState extends State<AutoScrollListView> {
final ScrollController _scrollController = ScrollController();
final List<String> _items = List.generate(100, (index) => 'Item $index');
@override
void initState() {
super.initState();
_startAutoScroll();
}
void _startAutoScroll() {
Future.delayed(Duration(seconds: 1), () {
if (_scrollController.hasClients) {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: Duration(seconds: 2),
curve: Curves.easeInOut,
).then((_) {
_scrollController.jumpTo(0); // Reset to top
_startAutoScroll(); // Repeat
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Auto Scroll ListView')),
body: ListView.builder(
controller: _scrollController,
itemCount: _items.length,
itemBuilder: (context, index) {
return ListTile(title: Text(_items[index]));
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment