Search code examples
flutterdartflutter-navigation

Flutter - Passing data between widgets on same screen


I have one screen split into 2 halves. Top half has a listview and the bottom will display the details based on the onTap: from the listTile within the listview above.

My code looks something like this, after removing all irrelevant pieces:

return Scaffold(
  appBar: AppBar(
    ...
    // app bar details
      ...
    ), // AppBar

body: SingleChildScrollView (
        child: Column(
            children: <Widget>[ 
             ...
             Container(
                ...
                child: populatelistview();
                ), 

             SizedBox(height: ...),
              ...
              ...
             Container(
                ...
                child: detailsView();
                ),
             ... 
            ],
          ), // Column
     ) // SingleChildScrollView

So, in order to get the detailsView populated with data based on onTap() from listview, I will have to pass some variable (or some id), from the listview to detailsView.

I have used Navigator.push before (refer: https://flutter.dev/docs/cookbook/navigation/passing-data), but that would work only when moving from one screen to another. I am having trouble sending data from listview to detailsView, on the same screen.

Maybe I am looking at it differently or just unable to search properly.

Would appreciate if someone could point me in the right direction.

If this question is already answered before, then I apologize for the same - please do mark it as duplicate and point me to the correct discussion.

Thanks in advance.

Stay Safe!!


Solution

  • Thanks to everyone who tried helping me out. However, I have now resolved this issue by using Inherited Widget.

    Inherited Widget was more suitable for my situation where I needed data to flow from one widget to another without change of screen.