Andrzej Szafarczyk
Andrzej Szafarczyk

Reputation: 31

How do I call a function from another class in Flutter?

I'm new to flutter and have some problems with moving my logic outside the main file. Can someone tell me how to refer to the Player object created in the main file?

Here is the main file, from which I want to call the Player::move() method

EDIT Now the compiler complains that I'm calling the move() method on a NULL object.

      class MainViewState extends State<MainView> {
  GlobalKey<PlayerState> playerKey;
  
  @override
  void initState() {
    playerKey = GlobalKey<PlayerState>();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap:playerKey.currentState.move, // -  here I want to call my move function
      child: Stack(children: <Widget>[
        Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage("image/background.jpg"),
              fit: BoxFit.cover,
            ),
          ),
        ),
        new Player(key: playerKey),
      ]),
    );
  }
}

And here is the file with the move() method, which should change the alignment of my image

    class Player extends StatefulWidget {
  
  Player({Key key}) : super(key: key);
  @override
  PlayerState createState() => PlayerState();
}

class PlayerState extends State<Player> {
  double ypos = 0;
  void move() // method I want to call
  {
   setState(() {
    ypos -= 0.1;
    print(ypos);
   });
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedContainer(
        duration: Duration(milliseconds: 0),
        alignment: Alignment(0, ypos),
        child: Image.asset(
          "image/test.png",
          fit: BoxFit.contain,
          height: 60,
          width: 60,
        ));
  }
}

Upvotes: 1

Views: 1459

Answers (2)

Andrzej Szafarczyk
Andrzej Szafarczyk

Reputation: 31

So thank's to @daringdk I made it work. Overwriting the initstate method and passing the globalKey to the Stateful object was the solution.

**The main file **

   void main() => runApp(MaterialApp(home: MainView()));

class MainView extends StatefulWidget {
  @override
  MainViewState createState() => MainViewState();
}

class MainViewState extends State<MainView> {
  GlobalKey<PlayerState> playerKey;

  @override
  void initState() {
    playerKey = GlobalKey<PlayerState>();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        playerKey.currentState.move(); // - My mistake was here that I thought that I'm supposed to return a function pointer here. <FACEPALM> . The currlybraces made it.
      },
      child: Stack(children: <Widget>[
        Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage("image/background.jpg"),
              fit: BoxFit.cover,
            ),
          ),
        ),
        new Player(key: playerKey),
      ]),
    );
  }
}

The player file

   class Player extends StatefulWidget {
  Player({Key key}) : super(key: key);
  
  @override
  PlayerState createState() => PlayerState();
}

class PlayerState extends State<Player> {
  double ypos = 0;
  void move() // method I want to call
  {
    setState(() {
      ypos -= 0.1;
      print(ypos);
    });
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedContainer(
        duration: Duration(milliseconds: 0),
        alignment: Alignment(0, ypos),
        child: Image.asset(
          "image/test.png",
          fit: BoxFit.contain,
          height: 60,
          width: 60,
        ));
  }
}

Upvotes: 0

Dineshkumar Kovalan
Dineshkumar Kovalan

Reputation: 131

Import the file containing the class in the main file then create an object for the class containing the method and call the method on the object. In case of static method use the class name instead of object reference.

MyClass myClassObject = MyClass(....); myClassObject.myMethod();

MyClass.myMethod(); //for static methods

Upvotes: 1

Related Questions