flutter 父组件调用子组件方法

2021-12-28  本文已影响0人  晓函

1、在子组件中
重要的是 这句代码 GlobalKey<_ChildState> globalKey = GlobalKey();

import 'package:flutter/material.dart';
 
GlobalKey<_ChildState> globalKey = GlobalKey();
 
class Child extends StatefulWidget {
  Child({
    Key key,
  }) : super(key: key);
  @override
  _ChildState createState() => _ChildState();
}
class _ChildState extends State<Child> {
    
    //子组件方法
    childMethod(){}
    ....
}

2、父组件中

import 'package:flutter/material.dart';
 
class Parent extends StatefulWidget {
  Parent({}) : super(key: key);
  @override
  _ParentState createState() => _ParentState();
}
class _ParentState extends State<Parent> {
    parentMethod(){
        //父组件中调用
        globalKey.currentState.childMethod(_contractOrder)
    }
    @override
    Widget build(BuildContext context) {
        return Child(key: globalKey)
    }
上一篇下一篇

猜你喜欢

热点阅读