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

时间:2020-08-07 12:26:07

标签: flutter

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

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

3 个答案:

答案 0 :(得分: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(),
    );
  }
}

答案 1 :(得分: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()
}

答案 2 :(得分: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.
 }),
}