// If you know the height for each widget or the height is fixed.
void _scrollToLatestItem(int latestIndex) {
_controller.animateTo(
latestIndex * _widgetHeight,
duration: Duration(milliseconds: 300),
curve: Curves.easeInCubic,
);
}
// There is no other way to scroll to the end of a lazy listView and
// unknown height for each widget (the height is not fixed) in the list.
// If you know the height for each widget or the height is fixed
// DO NOT use this code!
Future<void> _scrollToLatestMessage() async {
await Future.delayed(const Duration(milliseconds: 50));
if (chatScrollController.position.pixels <
chatScrollController.position.maxScrollExtent) {
await chatScrollController.animateTo(
chatScrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 250),
curve: Curves.easeInCubic,
);
await _scrollToLatestMessage();
}
}