如何将数据从 TabBarView Parent 传递到选项卡?

时间:2021-03-11 16:52:40

标签: flutter dart

我有三个标签,它们将从 TabView(控制标签的小部件)接收一个字符串。

我的代码如下:

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

如何将字符串传递给videosListForTab() 和imagesListForTab()? 这些小部件与 TabBarView 小部件不在同一个文件夹中。

2 个答案:

答案 0 :(得分:1)

更改您的选项卡小部件构造函数以接收参数。 例如:

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();
  }
}

您可以通过这种方式创建小部件:

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

您还可以使用 {} 定义您的小部件构造函数:

class VideosListForTab extends StatelessWidget {

  final String value;

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

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

您可以通过这种方式创建小部件:

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

答案 1 :(得分:0)

尝试将您的功能(我猜它是一个功能)更改为类似的东西

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

List<Widget> imagesListForTab(String value ){...}
相关问题