Home > Enterprise >  using "mixin" to factor out code to different files
using "mixin" to factor out code to different files

Time:03-24

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

CodePudding user response:

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
  }
}
  • Related