Flutter: send Data from TabBarView (StatefullWidgets) back to main Scaffold

可紊 提交于 2021-02-08 10:28:29

问题


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


回答1:


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

Small example

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

}



来源:https://stackoverflow.com/questions/51010925/flutter-send-data-from-tabbarview-statefullwidgets-back-to-main-scaffold

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!