How do you navigate programmatically between tabs like in Flutter?

Let's figure it out. 🧡
First of all, we need:

- a TabBar with three tabs
- a TabController to control the selected tab
Then, let's add a TabBarView to contain all the views (pages).

Our custom views have an "onNext" callback that we can use to change the index of the TabController (and update the page).
This works, but by default we can still navigate across tabs by tapping on them.

If we want to lead the user through the pages in order, this is not ideal:
To address this, we can create a ReadOnlyTabBar that uses IgnorePointer to ignore all interactions with the tabs.

Then we can wrap the TabBar with this new widget:
We should also make sure that we can't switch between tabs with interactive drag gestures.

NeverScrollableScrollPhysics() helps with that:
And that's it! We have leveraged TabController, TabBar, and TabBarView to create a multi-step user journey.

For bonus points, we can show a dialog and reset the TabController index to 0 when we press OK:
You can find the full source code for this example on DartPad:

dartpad.dev/?null_safety=t…

Want more Flutter tips like this? Then follow me: @biz84

Happy coding!

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Andrea Bizzotto πŸ’™

Andrea Bizzotto πŸ’™ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @biz84

19 Sep
Every week I send some my best content to my email subscribers.

And in the last month alone, feedback has been overwhelmingly positive.

So which are my most liked tutorials? Read on. πŸ‘‡ Image
My Dart top tips article seems to be an all-time favourite:

codewithandrea.com/videos/top-dar…
Ever had issues with widgets that rebuild when they shouldn't?

Then maybe your app has some side effects. Learn all about them here:

codewithandrea.com/articles/side-…
Read 6 tweets
14 Sep
Remember this thread about how to create a chat with message bubbles?

Well, I found a simpler and more performant solution.

Let's get back to basics. 🧡

What we want to do is to *align* our message bubble to the left or right as needed.

We *could* accomplish this using a Row with MainAxisAlignment.start or MainAxisAlignment.end. Image
But that leads to an unconstrained width issue and breaks our layout in certain cases.

This *could* be fixed with LayoutBuilder + ConstrainedBox.

But there's a better way. Image
Read 6 tweets
13 Sep
Building a Flutter chat UI with message bubbles like this should be easy right?

Not so fast! 🧡 Image
This layout can be built with a ListView that contains some message bubbles.

Each bubble is a Row that holds a DecoratedBox (with a child Text) and a Spacer (or viceversa).

The text expands horizontally in one line and the bubble adjusts accordingly. Image
Here's how the message bubble looks in code: Image
Read 9 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(