本文介绍了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.

...

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);
                            },
       )])));}
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)发送回主支架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:18