Scrollable Listview in Flutter with Dart
437
Add a constructor and pass the controller as parameter
class MainView extends StatelessWidget {
...
// is this the correct place?
final ScrollController scrollController = ScrollController();
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new ChatListView(scrollController: scrollController)
);
}
}
class ChatListView extends StatefulWidget {
ChatListView({@required this.scrollController});
final ScrollController scrollController;
@override
_ChatListView createState() => _ChatListView();
}
class _ChatListView extends State< ChatListView > {
Widget build(BuildContext context) {
return ListView.builder(
controller: widget.scrollController,
);
}
}
Author by
Daniel Stephens
Updated on December 05, 2022Comments
-
Daniel Stephens over 1 year
Can someone explain me where I should define a scroll controller? I have chat list view which is the body of a scrollable view. I want to be able to control the scrolling behaviour from
MainView
but don't know how to pass the controller down to_ChatListView
. Any ideas?mainview.dart
class MainView extends StatelessWidget { ... // is this the correct place? final ScrollController scrollController = ScrollController(); @override Widget build(BuildContext context) { return new Scaffold( body: new ChatListView() ); } }
chatlistview.dart
class ChatListView extends StatefulWidget { @override _ChatListView createState() => _ChatListView(); } class _ChatListView extends State< ChatListView > { Widget build(BuildContext context) { return ListView.builder( controller: scrollController, ); } }
-
Daniel Stephens almost 6 yearsPerfect, the
widget
object was finally what I was looking for! Thanks