Last updated:
0 purchases
scroll to position
Scroll To Index #
A Project for Listview element scroll to current index.
It allows scrolling to a specific index programmatically.
It's configured to scroll to index 10 with a duration of 0.2 seconds. You can modify the index and duration according to your requirements.
PreferPosition set value 0 to 1.0 ( 0 = controller scroll to screen of top, 0.5 = controller scroll to screen of center and 1 = controller scroll to screen of bottom )
⠀
⠀
import 'dart:math';
import 'package:flutter/material.dart';
import 'dart:math' as math;
import 'package:scroll_to_position/scroll_to_position.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
AutoScrollController scrollController = AutoScrollController();
@override
void initState() {
super.initState();
scrollController = AutoScrollController(
viewportBoundaryGetter: () =>
Rect.fromLTRB(0, 0, 0, MediaQuery.of(context).padding.bottom),
axis: Axis.vertical,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
controller: scrollController,
shrinkWrap: true,
itemCount: 20,
itemBuilder: (context, index) {
return AutoScrollTag(
key: ValueKey(index),
controller: scrollController,
index: index,
child: Container(
height: 250,
color:
Colors.primaries[Random().nextInt(Colors.primaries.length)],
child: Center(
child: Text(
'$index',
style: const TextStyle(
fontSize: 25,
),
),
),
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
scrollController.scrollToIndex(
10,
0.2,
);
},
),
);
}
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.