Flutter 如何在另一个有状态小部件中调用方法

Flutter 如何在另一个有状态小部件中调用方法,flutter,Flutter,我正在构建一个播客类型的应用程序,因此需要在许多地方调用录制、停止和播放功能,我创建了这些方法,但在其他地方调用这些方法很困难 main.dart class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { String statusText = "

我正在构建一个播客类型的应用程序,因此需要在许多地方调用录制、停止和播放功能,我创建了这些方法,但在其他地方调用这些方法很困难

main.dart

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String statusText = "";
  bool isComplete = false;

void startRecord() //Need to call all of these method in coming stateful widgets         
void stopRecord() //
void pauseRecord()//
void resumeRecord()//
void play() //

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
home: Builder(
        builder: (context) => Scaffold(
          drawer: Drawer(
            elevation: 2.0,
            child: ListView(
              children: <Widget>[
                ListTile(
                  title: Text('Home'),
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) {
                          return MyApp();
                        },
                      ),
                    );
                  },
                ),

      //more code is here 

Expanded(
            child: GestureDetector(
              child: IconButton(
                  icon: Icon(Icons.mic),
                  color: Colors.white,
                  iconSize: 40,
                  onPressed: () async {
                    startRecord();
                  }),
            ),
          ),



}



class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(


onPressed: () {
startRecord()

// need to call the method here. 

}

Pressed: () {
    stopRecord()

// need to call the method here. 

}

Pressed: () {
    play()

// need to call the method here. 

}

),
}
类MyApp扩展StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
字符串statusText=“”;
bool isComplete=false;
void startRecord()//需要在未来的有状态小部件中调用所有这些方法
作废停止记录()//
无效暂停记录()//
作废记录()//
无效播放()//
@凌驾
小部件构建(构建上下文){
返回材料PP(
住家:建筑商(
生成器:(上下文)=>Scaffold(
抽屉(
标高:2.0,
子:ListView(
儿童:[
列表砖(
标题:文本(“主页”),
onTap:(){
导航器。推(
上下文
材料路线(
生成器:(上下文){
返回MyApp();
},
),
);
},
),
//更多代码在这里
扩大(
儿童:手势检测器(
孩子:我的钮扣(
图标:图标(Icons.mic),
颜色:颜色,白色,
iconSize:40,
onPressed:()异步{
startRecord();
}),
),
),
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
已按下:(){
startRecord()
//需要在这里调用该方法。
}
按:(){
停止记录()
//需要在这里调用该方法。
}
按:(){
play()
//需要在这里调用该方法。
}
),
}
需要从第一个有状态小部件调用所有方法,以获得底部有状态小部件

另外,当代码进行时,需要为其他类调用这些方法


两个有状态小部件都在main.dart中。我无法从第一个类调用第二个有状态小部件的方法

您可以使用子小部件的
BuildContext
获取父小部件的状态,如下所示:

类MyApp扩展StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
的静态_MyAppState(BuildContext上下文){
返回context.findAncestorStateOfType();
}
}
类MyAppState扩展了状态{
字符串statusText=“”;
bool isComplete=false;
void startRecord(){
打印(“你好”);
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
主页:MyHomePage(),
);
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
MyApp.of(context.startRecord();
返回脚手架(
正文:占位符(),
);
}
}

您可以使用子小部件的
BuildContext
获取父小部件的状态,如下所示:

类MyApp扩展StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
的静态_MyAppState(BuildContext上下文){
返回context.findAncestorStateOfType();
}
}
类MyAppState扩展了状态{
字符串statusText=“”;
bool isComplete=false;
void startRecord(){
打印(“你好”);
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
主页:MyHomePage(),
);
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
MyApp.of(context.startRecord();
返回脚手架(
正文:占位符(),
);
}
}

这不是一门火箭科学,只是一行简单的代码,你就完成了

您需要做的是调用
MyHomePage()
,让它接受要在小部件中使用的
startRecording()

1.将数据从MyApp()传递到MyHomePage()

2.在MyHomePage()中接收数据

类MyHomePage扩展StatefulWidget{
//让它接受一个函数类型onPressed参数
最终功能已按下;
//建造师
MyHomePage({Key,this.onPressed}):超级(Key:Key);
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
//只需调用从MyApp接收startRecording()的onPressed
onPressed:()=>widget.onPressed()
}

这不是一门火箭科学,只是一行简单的代码,你就完成了

您需要做的是调用
MyHomePage()
,让它接受要在小部件中使用的
startRecording()

1.将数据从MyApp()传递到MyHomePage()

2.在MyHomePage()中接收数据

类MyHomePage扩展StatefulWidget{
//让它接受一个函数类型onPressed参数
最终功能已按下;
//建造师
MyHomePage({Key,this.onPressed}):超级(Key:Key);
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
//只需调用从MyApp接收startRecording()的onPressed
onPressed:()=>widget.onPressed()
}

只需像这样将类外的函数定义为独立函数,但如果
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // here you pass the your function
      home: MyHomePage(onPressed: startRecording)
    );
  }
class MyHomePage extends StatefulWidget {
  // let it accept a function type onPressed argument
  final Function onPressed;
  
  // constructor
  MyHomePage({Key key, this.onPressed}): super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(

    // simply call the onPressed which received your startRecording() from MyApp
    onPressed: () => widget.onPressed()
}
onPressed: () {
          _MyAppState().startRecord(); //call using the class name.
        }
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}


void startRecord(){
   .
   .
   .
}           /// Like here outside the class

class _MyAppState extends State<MyApp> {
  String statusText = "";
  bool isComplete = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(.....



}



class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(


onPressed: () {
 startRecord();       // call Here as follows.
 }),
}