this post was submitted on 04 Jul 2023
8 points (100.0% liked)

Flutter

629 readers
2 users here now

Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC

founded 1 year ago
MODERATORS
 

Hey guys, very new to flutter and have been learning in order to start making pull requests on Github. However its been a while since ive used a non-R programming language so I might be a bit rusty with some concepts.

For reference, the repo i want to help out is Thunder

What im trying to do is cause a page to scroll to the top, when the user presses the bottom navigation bar button for that page.

So it goes PageView, CommunityPage, then PostCardList, which is a class containing a ListView showing the cards and whatever else.

I get that I need to pass one of the scrollControllers up to the main PageView level, and then use the BottomNavigationBar onTap (index) function to call an animateTo on it. But i dont know where, or how i pass the scrollcontroller up a level. When i know how, do i just pass it from the PostCardList class, to the CommunityPage class, to the PageView-level class? Or is there some better way??

Sorry for such a low level question but ive been learning this for like 2 or 3 days and have no experience whatsoever hahaha

Thanks!

top 5 comments
sorted by: hot top controversial new old
[–] [email protected] 2 points 1 year ago* (last edited 1 year ago) (1 children)

You could pass it up a level by having the ScrollController be a class property obtained thru the constructor, which the parent would then provide when calling the widget.

This gets unwieldy fast. That's where the state management solutions come in. From the pubspec.yaml, it seems Thunder uses Bloc for that. Look at the docs.

[–] beppi 3 points 1 year ago

I'm looking into Bloc now, thanks!

[–] [email protected] 2 points 1 year ago (1 children)

I have implemented something like this before, if I remember correctly I've done it with riverpod's ref.listen.

Basically, each tab (of PageView) has its own ScrollController and we don't care about exact implementation on each tab and simply notify that tab to scroll up. You can create a simple StateProvider<int>, then on click of BottomNavigationBar set the provider to its index. Then on each tab, use ref.listen(theprovider.select(value) => value == tabindex) etc.

There are other ways, of course, but this one gets the job done.

[–] beppi 1 points 1 year ago (1 children)

Thanks for the reply! Bit it might be a bit advanced for me at this stage... Do you have any youtube vids or other resources that might help me out with those concepts?

[–] [email protected] 2 points 1 year ago

I understand that this might be a little intimidating at first, but trust it's worth it! Riverpod has a lot of features and is very flexible. Official docs are a great starting point. CodeWithAndrea's riverpod guide is also great.

There are a ton of videos as well, just search for them, I have no special recommendation

load more comments
view more: next ›