I'm trying to auto scroll to the end of a SliverList that is inside a CustomScrollView. SliverList itself doesn't have a controller property so I have to pass a ScrollController to the CustomScrollView.
The problem is when I pass a controller to the CustomScrollView its behavior changes and it's no longer scrolls the outer list and would not cause a collapsed SliverAppBar widget. How can I auto scroll SliverList and also keep the behavior of CustomScrollView as before?
Here is my code:
class _MyHomePageState extends State<MyHomePage> {
ScrollController _scrollController = ScrollController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
curve: Curves.easeOut,
duration: const Duration(seconds: 1),
);
},
),
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
expandedHeight: 230.0,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text('SliverAppBar Expand'),
),
)
];
},
body: CustomScrollView(
//When controller is passed to CustomScrollView, its behavior changes
// controller: _scrollController,
slivers: [
//Some widgets are here
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return Container(
height: 80,
color: Colors.primaries[index % Colors.primaries.length],
alignment: Alignment.center,
child: Text(
'Item : $index',
),
);
},
childCount: 20,
),
),
],
),
) ,
);
}
}