scroll-to-index
scroll-to-index copied to clipboard
It doesn't work with two sliverList in CustomScrollView
can not scroll to the target index in this example, this example use CustomScrollView with two sliverList
import 'package:flutter/material.dart';
import 'package:scroll_to_index/scroll_to_index.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List<int> first = [];
List<int> second = [];
int count = 20;
@override
void initState() {
List.generate(count, (index) {
first.add(count - 1 - index);
second.add(index + count);
});
super.initState();
}
ValueKey key = const ValueKey("key");
final controller = AutoScrollController(
viewportBoundaryGetter: () => const Rect.fromLTRB(0, 0, 0, 0),
axis: Axis.vertical,
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: CustomScrollView(
cacheExtent: 1,
controller: controller,
center: key,
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
physics: const AlwaysScrollableScrollPhysics(),
slivers: [
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return AutoScrollTag(
key: ValueKey(first[index]),
controller: controller,
index: first[index],
child: ListTile(
onTap: () {
controller.scrollToIndex(first[index] + count, preferPosition: AutoScrollPosition.begin);
},
title: Text("${first[index]}"),
),
);
},
childCount: first.length,
),
),
SliverList(
key: key,
delegate: SliverChildBuilderDelegate(
(context, index) {
return AutoScrollTag(
key: ValueKey(second[index]),
controller: controller,
index: second[index],
child: ListTile(
onTap: () {
controller.scrollToIndex(second[index] - count, preferPosition: AutoScrollPosition.begin);
},
title: Text("${second[index]}"),
),
);
},
childCount: second.length,
),
),
],
),
);
}
}
Give false value "primary" of custom scrollview