There’s (at least) two ways to pass data from a child widget to a parent widget in Flutter; Shared object using Singleton pattern or callbacks. I am leaving the Singleton pattern using BLoC to another day, so this article talks only about callbacks.
If you are in a hurry, follow this gist.
Imagine that you have created a Voting App; where users can vote for the best mobile development framework. And this app consists of 5 widgets, Just as simple as this:
1- Text widget displays ‘Vote for the best Mobile App framework:’.
2- Two Buttons widgets; Flutter & React.
_3- _RichText widget displays ‘You voted for: ….’.
4- A widget to represents the Home Screen and contains all other widgets.
Most of this work is completely usual, except the data-binding part, in other words ‘passing data of the selected name (Flutter/React) from the clicked-button to the RichText’ so RichText can display it: ‘You voted for: Flutter’.
“It is not enough for code to work.”
― Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship
Will not go in details with this approach as it’s not clean nor scalable at all. Anyway, this can be achieved by building all previous widgets inside the same parent(Home Screen widget), so they all can access same global variables, where this variables can be used as a data pool to share data between widgets. See this diagram:
#mobile-apps #dart #flutter #flutter-widget #data analysis