如何在Flutter中调用另一个状态的函数而不初始化其类?

发布于 2025-01-14 10:05:41 字数 2821 浏览 1 评论 0原文

我正在编写一个语言学习应用程序,但我陷入了困境。我想做的是,当用户按下下一个按钮时,我想增加索引并显示 Lesson.dart 中的其他页面。我有很多页面,例如听力、视频等。

我想调用 nextPage() 而不初始化课程类。

create_word.dart

class CreateWord extends StatefulWidget {

  var pathToPlay = '';
  String word = '';
  String sentence = '';
  CreateWord(this.pathToPlay, this.word, this.sentence);

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

class _CreateWordState extends State<CreateWord> {

  late AudioPlayer player;
  @override
  void initState() {
    super.initState();
    player = AudioPlayer();
  }
  @override
  void dispose() {
    player.dispose();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          Row(
            children: [

              // When pressed this button, call nextPage() in lesson.dart              

              ElevatedButton(
                child: Text("Play Sound", style: TextStyle(color: Colors.white, fontSize: 13),),
                onPressed: () async{
                  await player.setAsset(widget.pathToPlay);
                  player.play();
                },
              ), // The Button


              Text(widget.word),
              Text(widget.sentence)
            ],
          ),
        ],
      ),
    );
  }
}

课程.dart

class Lesson extends StatefulWidget {

  int lesson_index = 0;
  Lesson(this.lesson_index);

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

class LessonState extends State<Lesson> {
  final lessonKey = GlobalKey<LessonState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xff413250),
      appBar: buildAppBar(),
      body: buildBody(),
      //bottomNavigationBar: buildNavbar(),
    );
  }

  late int _lesson_index = widget.lesson_index;
  int _page_index = 0;

Widget setLesson(){
    var page = data[_lesson_index]['pages'][_page_index];

    //switch("video"){
    switch(data.isNotEmpty ? page['type'] : null){
      case "text":
        return Text("Text");
      case "video":
        return CreateVideo(page['path']);
      case "word":
        return CreateWord(page['path'], page['word'], page['sentence']);
      case "audio_match":
        return CreateAudioMatch(page['answers'], page['text'], page['correct_answer_index'], page['complete']);
      case "complete_text":
        return CreateCompleteText(page['text'], page['answer'], page['complete']);
      default:
        return Text("Bir hata oluştu. " + page.toString());
    }
  }

  // Call this when button pressed in 

  void nextPage(){
    setState(() {
      _page_index < data[_lesson_index]['pages'].length - 1 ? _page_index++ : null;
    });
  }
}

I'm writing a language learning app and I'm stuck. What am I trying to do is when user pressed the next button, I want to increase the index and show other page in lesson.dart. I have many pages like listening, video etc.

And I want to call nextPage() without initialize Lesson class.

create_word.dart

class CreateWord extends StatefulWidget {

  var pathToPlay = '';
  String word = '';
  String sentence = '';
  CreateWord(this.pathToPlay, this.word, this.sentence);

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

class _CreateWordState extends State<CreateWord> {

  late AudioPlayer player;
  @override
  void initState() {
    super.initState();
    player = AudioPlayer();
  }
  @override
  void dispose() {
    player.dispose();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          Row(
            children: [

              // When pressed this button, call nextPage() in lesson.dart              

              ElevatedButton(
                child: Text("Play Sound", style: TextStyle(color: Colors.white, fontSize: 13),),
                onPressed: () async{
                  await player.setAsset(widget.pathToPlay);
                  player.play();
                },
              ), // The Button


              Text(widget.word),
              Text(widget.sentence)
            ],
          ),
        ],
      ),
    );
  }
}

lesson.dart

class Lesson extends StatefulWidget {

  int lesson_index = 0;
  Lesson(this.lesson_index);

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

class LessonState extends State<Lesson> {
  final lessonKey = GlobalKey<LessonState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xff413250),
      appBar: buildAppBar(),
      body: buildBody(),
      //bottomNavigationBar: buildNavbar(),
    );
  }

  late int _lesson_index = widget.lesson_index;
  int _page_index = 0;

Widget setLesson(){
    var page = data[_lesson_index]['pages'][_page_index];

    //switch("video"){
    switch(data.isNotEmpty ? page['type'] : null){
      case "text":
        return Text("Text");
      case "video":
        return CreateVideo(page['path']);
      case "word":
        return CreateWord(page['path'], page['word'], page['sentence']);
      case "audio_match":
        return CreateAudioMatch(page['answers'], page['text'], page['correct_answer_index'], page['complete']);
      case "complete_text":
        return CreateCompleteText(page['text'], page['answer'], page['complete']);
      default:
        return Text("Bir hata oluştu. " + page.toString());
    }
  }

  // Call this when button pressed in 

  void nextPage(){
    setState(() {
      _page_index < data[_lesson_index]['pages'].length - 1 ? _page_index++ : null;
    });
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情出卖 2025-01-21 10:05:41

您可以执行 static

static nextPage...

函数,然后从任何地方获取它,如下所示:

Lesson.nextPage

You can do you function static

static nextPage...

and then get it from anywhere, like this:

Lesson.nextPage

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文