'How to call function in another state without initializing its class in Flutter?

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


Solution 1:[1]

You can do you function static

static nextPage...

and then get it from anywhere, like this:

Lesson.nextPage

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Evgen