I’m building a Chat Page and want it to display the latest message when opened, similar to most chat apps. Currently, I have tried two approach.
Approach 1. Using addPostFrameCallback
with jumpTo
like this, but this will causes the screen flicker when opened.
WidgetsBinding.instance.addPostFrameCallback((_) {
provider.chatScrollController.jumpTo(
provider.chatScrollController.position.maxScrollExtent);
});
Approach 2. Using `reverse: true` but this will display the message at the bottom when there only one or two.
is there a better way to achieve smooth scrolling to the latest message without the flicker? Would love any advice or suggestions!
2
Answers
You can use
animateTo
method fromScrollController
. In your code changejumpTo
toanimateTo
then addduration
andcurves
properties inside the method like this:This will animating your scroll to the bottom of the page.
Reference animateTo Flutter Documentation
You can achieve this by using a SingleChildScrollView with the reverse property set to true. Then, wrap it with an Align widget and set alignment: Alignment.bottomCenter, like so: