How to pass data from TabBarView Parent to tabs?

Viewed 704

I have three Tabs that will receive a string from TabView (the widget that rules the Tabs).

My code looks like:

body: TabBarView(
            children: [
              FloatingActionButtonWidget(),
              videosListForTab(), // <-- want to send string to 
              imagesListForTab(),  // <-- want to send string to 
              Icon(Icons.label),
            ],
          )

How can I pass the string to videosListForTab() and imagesListForTab()? The widgets are not in the same folder as the TabBarView widget.

2 Answers

Change your Tabs Widget Constructor to receive the parameters. For example:

class VideosListForTab extends StatelessWidget {

  final String value;

  // This way, when instantiating the Widget, you have to pass the String
  VideosListForTab(this.value);

  @override
  Widget build(BuildContext context) {
    // return your Widget here
    return Container();
  }
}

The you can create the Widget this way:

body: TabBarView(
        children: [
          FloatingActionButtonWidget(),
          videosListForTab("parameter as you want"), // <-- want to send string to 
          imagesListForTab(),  // <-- want to send string to 
          Icon(Icons.label),
        ],
      )

You can also define your widget constructor using {}:

class VideosListForTab extends StatelessWidget {

  final String value;

  VideosListForTab({this.value = ""});

  @override
  Widget build(BuildContext context) {
    // return your Widget here
    return Container();
  }
}

The you can create the Widget this way:

body: TabBarView(
        children: [
          FloatingActionButtonWidget(),
          videosListForTab(
              value: "parameter as you want"
          ), // <-- want to send string to 
          imagesListForTab(),  // <-- want to send string to 
          Icon(Icons.label),
        ],
      )

Try to change your functions (i guess it's a functions) into something like

List<Widget> videosListForTab(String value ){...}

List<Widget> imagesListForTab(String value ){...}
Related