Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrolling down to bottom of screen when Redux store changes #47

Closed
hash404 opened this issue May 12, 2018 · 15 comments
Closed

Scrolling down to bottom of screen when Redux store changes #47

hash404 opened this issue May 12, 2018 · 15 comments

Comments

@hash404
Copy link

hash404 commented May 12, 2018

Hi Brian,

I'm trying to build a small chat app with Redux, and when new list items (chat messages) are added to the store I want to animate a scroll down to the bottom of the list so they are in view.

Without Redux, in Flutter you're able to use ScrollController then call _scrollController.animateTo when you call setState to add the new items (source).

In React, you would have componentDidUpdate, so you would be able to animate the scroll to the bottom in componentDidUpdate when your Redux store changes (source).

Wondering what is the equivalent way to do this in Redux with Flutter since StatelessWidget doesn't provide hooks like componentDidUpdate? Unfortunately, just calling _scrollController.animateTo after dispatching a Redux action doesn't scroll to the bottom position after the Redux store updates.

@brianegan
Copy link
Owner

Ah, this is an interesting case. We have the onWillChange property on the StoreConnector, but I haven't quite implemented this functionality just yet and I do know it's important. I'll look at good ways to handle this.

@AlexandreRoba
Copy link

Any update on this?

@AlexandreRoba
Copy link

I'm also kind of stuck on this. I need to set the position on a ListView on the "selected" item when the presenter is built. Unfortunattely the only way to scroll the ListView is to use a ScrollController... The ScrolleContoller can only be called once it has been attached to the ListView which happens in the build method. I'm setting a Timer in the build method that will animate the listview 10ms after is has been built... But this is ugly :(

@brianegan
Copy link
Owner

Hey @AlexandreRoba -- sorry, not sure how I missed your message. I'm actually playing around with this right now, and hope to publish something this weekend!

@brianegan
Copy link
Owner

brianegan commented Jun 10, 2018

Hey all -- what would you like this callback to be called? What I can think of:

  • onPostFrame - this is the system callback used on the hood, but maybe not very clear
  • onDidChange - good alignment with the onWillChange callback

Other suggestions welcome!

Also, one more question: Would you want this callback to run after the First build and after every state change, or only after the state changes?

@AlexandreRoba
Copy link

Hey Brian, IMHO onDidChange speaks for itself :)

@brianegan
Copy link
Owner

brianegan commented Jun 11, 2018

Thanks @AlexandreRoba -- Would you want this callback to run after the First build and after every state change, or only after the state changes?

Or should I make 2 callbacks: onFirstBuild and onDidChange? Or is the onFirstBuild even necessary? Just trying to support the important use-cases!

@AlexandreRoba
Copy link

AlexandreRoba commented Jun 11, 2018 via email

@brianegan
Copy link
Owner

Hey hey, thanks again for all your feedback -- yah, it's a bit tricky since there are some intricacies in how builds are handled in Flutter. There are 3 types of builds that could happen:

  1. The first time the ViewModel is created and the builder function is run.
  2. Every time the ViewModel changes (not the App State necessarily, as you could use distinct to limit the number of rebuilds for performance reasons)
  3. Whenever Flutter decides it needs to rebuild the Widget. This could happen if the user rotates the screen, or if a Widget above the StoreConnector request a rebuild, or if the user navigates to a new route. This one is largely out of our hands and depends on the individual app / how Flutter rebuilds.

I could add callbacks for the first 2 instances, but not the 3rd. I could either:

  1. Merge these two callbacks into onBuildViewModel or something like that
  2. Keep them separate: onFirstBuild and onDidChange.

@AlexandreRoba
Copy link

AlexandreRoba commented Jun 11, 2018 via email

@brianegan
Copy link
Owner

Cool, thanks again. That was my gut feeling as well, so I'll go that route.

@kitoko552
Copy link

I got the same problem as hash404 got. I agree with Alexandre.
I'm looking forward to updating!

@brianegan
Copy link
Owner

Hey all, just pushed up a new PR that adds two callbacks for this purpose. Please see #55 and let me know your thoughts on the naming!

@dviluk
Copy link

dviluk commented Jun 19, 2018

brianegan commented 8 days ago

Hey hey, thanks again for all your feedback -- yah, it's a bit tricky since there are some intricacies in how builds are handled in Flutter. There are 3 types of builds that could happen:

  1. The first time the ViewModel is created and the builder function is run.
  2. Every time the ViewModel changes (not the App State necessarily, as you could use distinct to limit the number of rebuilds for performance reasons)
  3. Whenever Flutter decides it needs to rebuild the Widget. This could happen if the user rotates the screen, or if a Widget above the StoreConnector request a rebuild, or if the user navigates to a new route. This one is largely out of our hands and depends on the individual app / how Flutter rebuilds.
  1. also when you open the keyboard

@brianegan
Copy link
Owner

Hey all -- this change has now been published as part of flutter_redux 0.5.2. Please play around and let me know whatcha think / if ya run into trouble :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants