【问题标题】:How to call Void() function in another Stateful widget如何在另一个有状态小部件中调用 Void() 函数
【发布时间】:2020-07-31 14:55:25
【问题描述】:

我想在另一个有状态的小部件中调用 void play() 方法

ma​​in.dart

    import 'dart:io';
import 'package:audioplayer/audioplayer.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'dart:async';
import 'package:path_provider/path_provider.dart';
import 'package:record_mp3/record_mp3.dart';
import 'package:permission_handler/permission_handler.dart';
import 'regitration.dart';
import 'voiceCreate.dart';
import 'stopwatch.dart';

void main() => runApp(MyApp());

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

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

  @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();
                        },
                      ),
                    );
                  },
                ),
                ListTile(
                  title: Text('Sign up'),
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) {
                          return StopWatch();
                        },
                      ),
                    );
                  },
                ),
                ListTile(
                  title: Text('Sign in'),
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) {
                          return LoginScreen();
                        },
                      ),
                    );
                    // add sign in page
                  },
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) {
                  return MyHomePage();
                }),
              );
            },
            // Add your onPressed code here!

            child: Icon(Icons.add),
            backgroundColor: Colors.tealAccent.shade700,
          ),
          backgroundColor: Colors.grey.shade900,
          appBar: AppBar(
            title: Text('Myvo'),
            centerTitle: true,
            backgroundColor: Colors.tealAccent.shade700,
          ),
          body: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Expanded(
                      child: GestureDetector(
                        child: IconButton(
                            icon: Icon(Icons.mic),
                            color: Colors.white,
                            iconSize: 40,
                            onPressed: () async {
                              startRecord();
                            }),
                      ),
                    ),
                    Expanded(
                      child: GestureDetector(
                        child: IconButton(
                            icon: Icon(Icons.pause),
                            color: Colors.white,
                            iconSize: 40,
                            onPressed: () async {
                              pauseRecord();
                            }),
                      ),
                    ),
                    Expanded(
                      child: GestureDetector(
                        child: IconButton(
                            icon: Icon(Icons.stop),
                            color: Colors.white,
                            iconSize: 40,
                            onPressed: () async {
                              stopRecord();
                            }),
                      ),
                    ),
                  ],
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 20.0),
                  child: Text(
                    statusText,
                    style: TextStyle(color: Colors.red, fontSize: 20),
                  ),
                ),
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    play();
                  },
                  child: Container(
                    margin: EdgeInsets.only(top: 30),
                    alignment: AlignmentDirectional.center,
                    width: 100,
                    height: 50,
                    child: isComplete && recordFilePath != null
                        ? Text(
                            "play",
                            style: TextStyle(color: Colors.red, fontSize: 20),
                          )
                        : Container(),
                  ),
                ),
              ]),
        ),
      ),
    );
  }

  Future<bool> checkPermission() async {
    if (!await Permission.microphone.isGranted) {
      PermissionStatus status = await Permission.microphone.request();
      if (status != PermissionStatus.granted) {
        return false;
      }
    }
    return true;
  }

  void startRecord() async {
    bool hasPermission = await checkPermission();
    if (hasPermission) {
      statusText = "Recording...";
      recordFilePath = await getFilePath();
      isComplete = false;
      RecordMp3.instance.start(recordFilePath, (type) {
        statusText = "Record error--->$type";
        setState(() {});
      });
    } else {
      statusText = "No microphone permission";
    }
    setState(() {});
  }

  void pauseRecord() {
    if (RecordMp3.instance.status == RecordStatus.PAUSE) {
      bool s = RecordMp3.instance.resume();
      if (s) {
        statusText = "Recording...";
        setState(() {});
      }
    } else {
      bool s = RecordMp3.instance.pause();
      if (s) {
        statusText = "Recording pause...";
        setState(() {});
      }
    }
  }

  void stopRecord() {
    bool s = RecordMp3.instance.stop();
    if (s) {
      statusText = "Record complete";
      isComplete = true;
      setState(() {});
    }
  }

  void resumeRecord() {
    bool s = RecordMp3.instance.resume();
    if (s) {
      statusText = "Recording...";
      setState(() {});
    }
  }

  String recordFilePath; //maybe**strong text** this need to take

  void play() {
    if (recordFilePath != null && File(recordFilePath).existsSync()) {
      AudioPlayer audioPlayer = AudioPlayer();
      audioPlayer.play(recordFilePath, isLocal: true);
    }
  }

  int i = 0;

  Future<String> getFilePath() async {
    Directory storageDirectory = await getApplicationDocumentsDirectory();
    String sdPath = storageDirectory.path + "/record";
    var d = Directory(sdPath);
    if (!d.existsSync()) {
      d.createSync(recursive: true);
    }
    return sdPath + "/test_${i++}.mp3";
  }
}

voiceCreate.dart 我想在onPressed时打给这里

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var _isRecording = false;

MyHomePage.play()**//getting error here** 

  _startRecording() {
    this.setState(() {
      _isRecording = true;
    });
  }

  _stopRecording() {
    this.setState(() {
      _isRecording = false;
    });
  }

@override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.white,
        body: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.mic),
                color: Colors.black,
                iconSize: 70,
                onPressed: () => _startRecording(),
              ),
              IconButton(
                icon: Icon(Icons.stop),
                color: Colors.black,
                iconSize: 70,
                onPressed: () => _stopRecording(),
              ),
              IconButton(
                  icon: Icon(Icons.play_arrow),
                  color: Colors.black,
                  onPressed: () => `MyHomePage(title: "My title", play: this.play()),`**// getting error here**
              ),

已经调用了starRecordingstopRecording

onPressed 时所有三个函数都不起作用,它们是 startRecording、stopRecording 和 play。这些功能在 main.dart 中可以正常工作,但在 voiceCreate.dart 中却不行

【问题讨论】:

  • 我以前回答过这样的问题,看看here

标签: flutter


【解决方案1】:

Dart 函数是一流的,这意味着您可以像变量一样传递它们。在您的小部件类中有一个 VoidCallback 成员。

MyHomePage(this.playCallback, {Key key, this.title}) : super(key: key);
  final String title;
  final VoidCallback playCallback;

你可以在构造函数中传递它。

child: MyHomePage(play)

然后你可以在你的状态对象中访问它。

class _MyHomePageState extends State<MyHomePage> {
    ...

    onPressed: widget.playCallback

    ...    
}

警告

这不是实现此目的的推荐方法。如果可能,小部件必须包含零业务逻辑。像这个答案这样的小技巧会让你的代码变得非常复杂。考虑阅读state management 解决方案。

【讨论】:

    【解决方案2】:

    我认为其他 2 个让您大致了解如何按照您想要的方式进行操作。 使用这些方法没有任何问题。它们将起作用,除非当导航开始变得有点复杂时,您将开始使用更多页面并且您可能需要继续将其向下传递。

    因此,像easeccy 提及更多地关注状态管理。 Blocs、Redux 之类的东西都非常有帮助。 原因就像颤振如何根据 setstate() 构建事物一样,状态管理类允许您在不同的地方添加相同的事件,只要它们属于相同的实例。所有的建设者都将根据他们产生的状态进行重建。

    例如如果您要向该集团添加一个播放事件,则适用于集团。无论您身在何处,只要您能够向页面提供 bloc,您就可以添加事件,并且屏幕上的任何内容(如果您要构建多个 dart 文件)都将根据您的状态进行重建会产生的。

    集团的例子 main.dart

    void main() async{
    
      runApp(
        //Having this before return MaterialApp() ensures that you can call it 
        //anywhere in the app below this tree
        BlocProvider<TheTypeOfBloc>(
          create: (context) => TheTypeOfBloc(),
          child: App(),
      )
    
    }
    

    您拥有的任何其他小部件/构建

     @override
      Widget build(BuildContext context) {
        return BlocListener<TheTypeOfBloc,TheTypeOfState>(
         listener : (context,state){
         //tldr of listen =  when your state change do something.
         //so your void function can be call here
              if(state is playState){
                 _play();
              }
         }
         child:BlocBuilder<TheTypeOfBloc,TheTypeOfState> (
        // basically anything below here gets rebuild when you change a state.
          builder: (context,state) {
             return //what you want to build here like listener based on the 
                    //state or just build normally without the builder.
          }
         ),
        );
     }
    

    【讨论】:

      【解决方案3】:

      答案 3.0

      根据您显示的完整代码,这里是仅根据您的代码的解决方案。我相信它会为你消除一些困惑。

      • play()main.dart 中的FloatingActionButton 正确传递给MyHomePage。我没有使用main.dart 的完整代码,但特别是您的部分,您将移至MyHomePage

      ma​​in.dart

                 floatingActionButton: FloatingActionButton(
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) {
                        // you need to pass two positional arguments
                        // since your MyHomePage accepts two arguments
                        // one is your title and one is your function
                        return MyHomePage(title: "My Title", play: play);
                      }),
                    );
                  }
      
      • 其次,使用您的方法在您的MyHomePage 构造函数中传递。这将保持原样。只需查看此处代码的最后一行,看看我们如何使用来自您的main.dart 传递的play() 方法
      class MyHomePage extends StatefulWidget {
        // accepting the method in the constructor
        // so you can pass it with the title from your main.dart
        MyHomePage({Key key, this.title, this.play}) : super(key: key);
        
        final String title;
        final Function play; // this is the function
        
        @override
        _MyHomePageState createState() => _MyHomePageState();
      }
      

      现在如何在你的类中使用它

      class _MyHomePageState extends State<MyHomePage> {
        var _isRecording = false;
      
        _startRecording() {
          this.setState(() {
            _isRecording = true;
          });
        }
      
        _stopRecording() {
          this.setState(() {
            _isRecording = false;
          });
        }
      
      @override
        Widget build(BuildContext context) {
          return MaterialApp(
            home: Scaffold(
              backgroundColor: Colors.white,
              body: Center(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    IconButton(
                      icon: Icon(Icons.mic),
                      color: Colors.black,
                      iconSize: 70,
                      onPressed: () => _startRecording(),
                    ),
                    IconButton(
                      icon: Icon(Icons.stop),
                      color: Colors.black,
                      iconSize: 70,
                      onPressed: () => _stopRecording(),
                    ),
                    IconButton(
                        icon: Icon(Icons.play_arrow),
                        color: Colors.black,
                        onPressed: () => widget.play() // call like this
                    )
      

      注意:请同时查看我代码中的 cmets

      现在检查这是否适合您。这应该可以解决您的问题。

      【讨论】:

      • 嘿@LinuMariya,不不,你做错了。让我向您展示如何根据您的新代码执行此操作。然后你更新我
      • @LinuMariya 我已经编辑了代码。根据您遇到的错误,这是全新的答案。告诉我,它在 Answer 2.0 部分下
      • 对 main.dart 感到困惑,分享 main.dart 的完整视图,请检查并帮助
      • @LinuMariya 请检查新答案,从一开始就遵循答案Answer 3.0如果有帮助,请告诉我:)
      • 现在我意识到,main.dart 中的所有三个函数在 onPressed 时都不起作用
      猜你喜欢
      • 1970-01-01
      • 2018-12-04
      • 2020-09-10
      • 1970-01-01
      • 2021-09-28
      • 2021-01-22
      • 2021-12-15
      • 2021-07-12
      • 1970-01-01
      相关资源
      最近更新 更多