简体   繁体   English

如何从子小部件调用父小部件中的 function | Flutter

[英]How to Call a function in parent widget from child widget | Flutter

I have a parent and child widget.我有一个父子小部件。 The parent widget has a function.父小部件有一个 function。 I want to call that function (myParentFunction) from my child widget.我想从我的子小部件中调用 function (myParentFunction)。 This is the parent widget,这是父小部件,

class ParentWidget extends StatelessWidget {

  void myParentFunction() {
    print("This is print from parent");
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: MyCustomButton(),
    );
  }
}

This is the child widget这是子小部件

class MyCustomButton extends StatelessWidget {

  void myChildFunction() {
    print("This is print from child");
    //Here i want to call the myParentFunction()
  }

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      child: Text('Press'),
      onPressed: () {
        newFunction();
      },
    );
  }
}

Here the expected result is when i press the 'Press' button i want the output as This is print from child This is print from parent .这里的预期结果是当我按下“按下”按钮时,我想要 output,因为This is print from child This is print from parent How i rewrite this code to get that functionality.我如何重写此代码以获取该功能。

you should pass the function and call in child widget您应该通过 function 并调用子小部件

class ParentWidget extends StatelessWidget {

  void myParentFunction() {
    print("This is print from parent");
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: MyCustomButton(myParentFunction),
    );
  }
}

class MyCustomButton extends StatelessWidget {

final Function onTap;

MyCustomButton(this.onTap);
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      child: Text('Press'),
      onPressed: onTap,
    );
  }
}

Use a callBack Funtion使用回调函数


class ParentWidget extends StatelessWidget {
  void myParentFunction() {
    print("This is print from parent");
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: MyCustomButton(
        callback: () {
          myParentFunction();
        },
      ),
    );
  }
}

class MyCustomButton extends StatelessWidget {
  final VoidCallback callback;
  MyCustomButton({
    Key? key,
    required this.callback,
  }) : super(key: key);
  void myChildFunction() {
    print("This is print from child");
    //Here i want to call the myParentFunction()
  }

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      child: Text('Press'),
       onPressed: () {
        callback();
      },
    );
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Flutter 中的子小部件调用父小部件 function - How to call parent widget function from child widget in Flutter Flutter - 如何从子小部件调用父小部件函数,同时还使用变量保持状态? - Flutter - How do I call a Parent widget function from child widget while also maintaining the state with a variable? Flutter - 如何从父级调用子级小部件的方法 - Flutter - how to call child widget's method from parent Flutter:当小部件没有父/子关系时,如何从小部件 B 调用小部件 A 中的 function? - Flutter: How do I call a function in widget A from widget B when the widgets do not have a parent/child relationship? 在颤动中从父小部件重置子小部件 - Reset child widget from parent widget in flutter 如何将数据从子状态小部件传递到 Flutter 中的父小部件 - How to pass data from a child Stateful widget to Parent Widget in Flutter 如何在flutter中从父小部件调用子小部件的initState()方法 - How to Invoke initState() method of child widget from parent widget in flutter 如何从父小部件调用子小部件的 setState? - How can I call setState of a child widget from a parent widget? 如何从父小部件在子有状态小部件内运行函数? - How to run a function inside a child stateful widget from parent widget? Flutter,如何从返回的 Widget 调用 Stateful Widget 内部的函数? - Flutter, how to call a function inside Stateful Widget from a returned Widget?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM