I am trying to make a list that is basically a log screen. That said, I need the list to keep scrolling to bottom all the time.
How this can be done?
Using a ScrollController maxScrollExtent can be used together to smoothly scroll to the bottom of the view: _scrollController. animateTo( _scrollController. position.
Just change Column widget to ListView widget — and that's all. Your container becomes scrollable.
All you have to do is set Global Keys for your widgets and call Scrollable. ensureVisible on the key of your widget you want to scroll to. For this to work your ListView should be a finite List of objects.
Set reverse: true
on the ListView
widget and reverse the children list.
ListView(
// Start scrolled to the bottom by default and stay there.
reverse: true,
children: widget.children.reversed.toList(growable: false),
),
If you have a very long list and reversing is expensive, you can reverse the index rather than the whole list using ListView.builder
ListView.builder(
reverse: true,
itemCount: items.length,
itemBuilder: (context, index) {
final reversedIndex = items.length - 1 - index;
final item = items[reversedIndex];
return MyWidget(item);
}
)
I got this from Günter's comment above. Thanks!
I could make this work by using a Timer
but probably should have a better way to do this.
My workaround was:
ScrollController()
and attach it to the listView:ListView.builder(
controller: _scrollController,
itemCount: _logLines.values.length,
itemBuilder: (context, index) => _getLogLine(index),
)
initState
method and set a Timer inside it like: @override
void initState() {
super.initState();
Timer.periodic(Duration(milliseconds: 100), (timer) {
if (mounted) {
_scrollToBottom();
} else {
timer.cancel();
}
});
}
Define a method _scrollToBottom()
that calls:
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With