Flutter:将数据从TabBarView(StatefullWidgets)发送回主支架 [英] Flutter: send Data from TabBarView (StatefullWidgets) back to main Scaffold

查看:43
本文介绍了Flutter:将数据从TabBarView(StatefullWidgets)发送回主支架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个带有标签的应用程序以获取用户输入.问题是,不同的选项卡获得不同的输入,但是我必须收集数据库的输入.我的主意是,主要的Scaffhold会收集所有Tabs的输入并将其写入数据库中!我的问题是,我不知道将数据从选项卡(另一个文件中的statefullWidget)发送到父类(Scaffold)还是从那里运行函数!

I want to create an App with Tabs to get the users input. The Problem is, that the different Tabs get different inputs, but i have to collect the inputs for the Database. My idea her was, that the main Scaffhold collects the inputs from all Tabs and write it in a database! My Problem is, that i dont know to send data from the tab (statefullWidget in an other file) to the parent class (Scaffold) or run a function from there!

请帮助我,抱歉我的英语不好!

Please help me and sorry for my bad english!

乔纳斯(Jonas)

推荐答案

您可以传递一个 Function ,该函数可以随时调用.

You can pass a Function that can be called whenever you want.

MamaBear班

MamaBear class

...

class _MamaBear extends State<MamaBear> {

 void hungryBear(String babyBear) {
   print("$babyBear is hungry");
 }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
       body: Container(
                   child: Column(children: <Widget>[
                      BabyBear(
                           "Mark",
                           (babyBear) {
                                hungryBear(babyBear);
                            },
       )])));}

BabyBear班

BabyBear class

class BabyBear extends StatefulWidget {
  final String babyBearName;
  final Function onBearAction;

  BabyBear(this.babyBearName, this.onBearAction);

  @override
   _BabyBear createState() => _BabyBear();
  }

class _BabyBear extends State<BabyBear> {
  @override
  Widget build(BuildContext context) {
    return Card(
      child: RaisedButton(
          child: Text("Mama I'm hungry"),
          onPressed: () {
            widget.onBearAction(widget.babyBearName);
          }),
    );
  }

}

这篇关于Flutter:将数据从TabBarView(StatefullWidgets)发送回主支架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆