0

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

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 中。我无法从第一个类调用第二个有状态小部件的方法

4

3 回答 3

1

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

您要做的就是调用MyHomePage()并让它接受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()
}
于 2020-08-07T13:47:02.683 回答
1

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

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(),
    );
  }
}
于 2020-08-07T13:33:31.237 回答
0

只需将类外部的函数定义为像这样的独立函数但是如果您想从类内部调用。这是代码。

在另一个类中作为静态函数:

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

像这样在你的onpressed Statement.

应该管用。

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

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.
 }),
}
于 2020-08-07T14:03:32.957 回答