using "mixin" to factor out code to different files

Issue

I am trying to factor out some code(inspired from this)

part "game.dart":

part 'taps.dart';

class MyGame extends FlameGame with HasTappables {}

Trying to factor out to this file "taps.dart":

part of 'game.dart';

mixin Taps on MyGame {

  @override
  void onTapDown(int pointerId, TapDownInfo info) {
    super.onTapDown(pointerId, info);
}

Problem is that "onTapDown" is not called?

Update:

This will not work:

class MainClass with OneMixin {
  void test(){
    helloOne();
  }
}

mixin OneMixin on MainClass {
  void helloOne() {
    test();
  }
}

Solution

Something like this would work:

abstract class FlameGame {
  int? a;
}

class MyGame extends FlameGame with Taps {
  int? b;
}

mixin Taps on FlameGame {
  void method() {
    print(a); // Possible
    print(b); // Not possible
    print((this as MyGame).b); // possible but unsave
  }
}

Answered By – puaaaal

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *