简体   繁体   English

Flutter - 如何从父级调用子级小部件的方法

[英]Flutter - how to call child widget's method from parent

Let's say we have two stateful widgets .假设我们有两个有状态的小部件

ParentWidget(){

}

ChildWidget() {
  someMethod(){
    // some code, for example setState code
  }

}

Now when I use the ChildWidget in ParentWidget , how do I call the someMethod() ?现在,当我在ParentWidget使用ChildWidget时,如何调用someMethod()

If you need call function on widget you can use:如果您需要在小部件上调用函数,您可以使用:

context.findAncestorWidgetOfExactType<T>()

If you need call function on state of that widget you can use:如果您需要在该小部件的状态上调用函数,您可以使用:

context.findRootAncestorStateOfType<T>();

read more at:阅读更多信息:

https://api.flutter.dev/flutter/widgets/BuildContext/findAncestorWidgetOfExactType.html https://api.flutter.dev/flutter/widgets/BuildContext/findAncestorWidgetOfExactType.html

https://api.flutter.dev/flutter/widgets/BuildContext/findRootAncestorStateOfType.html https://api.flutter.dev/flutter/widgets/BuildContext/findRootAncestorStateOfType.html

Here is way what I've used.这是我使用过的方式。

  • Make a GlobalKey instance创建一个 GlobalKey 实例
  • Pass the Globalkey as a Key parameter to child widget.将 Globalkey 作为 Key 参数传递给子小部件。
  • Call GlobalKey.currentState.method();调用 GlobalKey.currentState.method();
GlobalKey<ChildWidgetState> globalKey = GlobalKey();

ParentWidget(){

   ChildWidget(key: globalKey);

   ...

   globalKey.currentState.someMethod();


}

ChildWidget() {
  ChildWidget({Key key}) : super(key: key);

  someMethod(){
    // some code, for example setState code
  }

}

TestCode测试代码

import 'package:flutter/material.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

GlobalKey<ChildWidgetState> globalKey = GlobalKey();

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        InkWell(
          onTap: () {
            globalKey.currentState.someMethod();
          },
          child: Text('ParentWidget'),
        ),

        ChildWidget(key: globalKey),
      ],
    );
  }
}


class ChildWidget extends StatefulWidget {
  ChildWidget({Key key}) : super(key: key);
  
  @override
  ChildWidgetState createState() => ChildWidgetState();
  
}

class ChildWidgetState extends State<ChildWidget> {
  void someMethod() {
    print('someMethod is called');
  }
  @override
  Widget build(BuildContext context) {
    return Text('childWidget');
  }
}

暂无
暂无

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

相关问题 如何从 Flutter 中的子小部件调用父小部件 function - How to call parent widget function from child widget in Flutter 如何从子小部件调用父小部件中的 function | Flutter - How to Call a function in parent widget from child widget | Flutter 如何在flutter中从父小部件调用子小部件的initState()方法 - How to Invoke initState() method of child widget from parent widget in flutter 如何从 flutter 中的父小部件访问所有孩子的 state? - How to access all of child's state from Parent Widget in flutter? 如何从其父窗口小部件中调用 Map 控制器 Flutter Map 的移动方法 - How to call the Map Controller's move method of Flutter Map from its parent widget 如何从父级调用另一个小部件的子方法 - How to call child method from parent for another widget 如何从其子 Widget 更新 Parent Widget 的状态,同时在 Flutter 中更新 Child 的状态? - How to update the state of Parent Widget from its Child Widget while also updating the Child's state in Flutter? Flutter - 如何从子小部件调用父小部件函数,同时还使用变量保持状态? - Flutter - How do I call a Parent widget function from child widget while also maintaining the state with a variable? flutter如何调用父组件的子组件方法? - How to call method in child component from parent in flutter? Flutter:如何从子小部件更新子(和父)小部件的 state(父小部件得到更新,子小部件没有) - Flutter: How to update state of child (and parent) widget from child widget (parent widget gets update, child does not)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM