繁体   English   中英

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

[英]How to call a method in another stateful widget

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

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. 

}

),
}

需要从底部有状态小部件的第一个有状态小部件调用所有方法

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

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

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
  
  static _MyAppState of(BuildContext context) {
    return context.findAncestorStateOfType<_MyAppState>();
  }
}

class _MyAppState extends State<MyApp> {
  String statusText = "";
  bool isComplete = false;
  
  void startRecord() {
    print('Hello');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    MyApp.of(context).startRecord();
    
    return Scaffold(
      body: Placeholder(),
    );
  }
}

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

您要做的就是调用MyHomePage()并让它接受要在 Widget 内部使用的startRecording()

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // here you pass the your function
      home: MyHomePage(onPressed: startRecording)
    );
  }

2.接收MyHomePage()中的数据

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()
}

像这样简单地定义class之外的function为独立的function但是如果你想从ZABBBA2F2A29FDCC40内部调用。 这是代码。

在不同的 class 作为 static function 内:

onPressed: () {
          _MyAppState().startRecord(); //call using the class name.
        }

像这样在你的onpressed Statement里面。

应该管用。

或者你可以做的是在 class 之外定义 function。 然后在任何你想要的地方使用它。 像这样:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM