如何从另一个 class(当 onUnityAdsFinish 时)触发有状态小部件中的函数?

How to trigger a function in a stateful widget from another class (when onUnityAdsFinish)?

我在有状态小部件中有一个游戏屏幕,其中包含让玩家后退一步(撤消之前的动作)的功能。我想在 onUnityAdsFinish() 从另一个小部件运行时触发此功能。

这是 game.dart 上玩游戏的有状态小部件:

class GameScreen extends StatefulWidget {
  @override
  GameScreenState createState() => GameScreenState();
}

class GameScreenState extends State<GameScreen> with SingleTickerProviderStateMixin {

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container( 
            // game content here
        ),
        MaterialButton(
          child: Text("Tap Here to Watch Ad to Undo Move"),
          onPressed: () => unityBloc.showUnityAd(),
          // (side note: unityBloc is accessable because I have it declared as "final unityBloc = UnityBloc();" higher up the widget tree on app.dart.)
        ),
      ],
    ),                  
  }

  // This is the function I want to call after the ad is finished.
  void undoMove() {
    setState(() {
      // code to undo move
    });
  }
}

当按下按钮观看广告时,它会调用 unity_bloc.dart 上我的 UnityBloc class 中的 showUnityAd() 函数:

import 'package:unity_ads_flutter/unity_ads_flutter.dart';

class UnityBloc with UnityAdsListener {
...
  showUnityAd() {
    UnityAdsFlutter.show('rewardedVideo').whenComplete(() => null);
  } // called when undo button is pressed on game.dart

  @override
  void onUnityAdsFinish(String placementId, FinishState result) {
    print("Finished $placementId with $result");
    // here is where I want to call the undoMove() function from game.dart
  }
}

广告显示完美,onUnityAdsFinish 中的打印语句在广告结束时打印到控制台,但此时我不知道如何调用 undoMove。

我花了一整天的时间观看有关回调和全局键等的教程和阅读 Whosebug,但我还是不明白。

有人可以帮忙吗?当 onUnityAdsFinish() 在 unity_bloc.dart 上触发时,如何在 game.dart 上触发 undoMove() 函数? (或者他们的 another/better 是这样做的吗?)

谢谢!

您可以在 UnityBloc class 中拥有一个包含函数的变量,并将 undoMove 方法传递给 showUnityAd 方法以将其分配给变量,并且然后你可以在onUnityAdsFinish方法中调用它。

所以UnityBloc变成这样:

class UnityBloc with UnityAdsListener {
  void Function() doOnUnityAdsFinish;

  ...

  showUnityAd({@required unityAdsFinishFunction}) {
    ...
    doOnUnityAdsFinish = unityAdsFinishFunction;
  } // called when undo button is pressed on game.dart

  @override
  void onUnityAdsFinish(String placementId, FinishState result) {
   ...
   doOnUnityAdsFinish?.call(); //Only calls if the function is not null
  }
}

你可以在 MaterialButtononPressed 中像这样:

 MaterialButton(
          child: Text("Tap Here to Watch Ad to Undo Move"),
          onPressed: () => unityBloc.showUnityAd(unityAdsFinishFunction: undoMove ),   
        ),

最简单的方法是 global/static 变量(回调)或 ValueNotifier/Stream。 也许,更清洁的方法是一些具有依赖注入功能的包,如 (get, get_it).

class UnityBloc with UnityAdsListener {
  /// pass a callback into the constructor.
  /// Function<String,FinishState> onAdCompleted ;
  /// UnityBloc([this.onAdCompleted]);

  /// easier approach, not very "flexible".
  static Function<String,FinishState> onAdCompleted ;

  showUnityAd() {
    UnityAdsFlutter.show('rewardedVideo').whenComplete(() => null);
  } // called when undo button is pressed on game.dart

  @override
  void onUnityAdsFinish(String placementId, FinishState result) {
    onAdCompleted?.call(placementId, result); 
    // here is where I want to call the undoMove() function from game.dart
  }
}


class GameScreenState extends State<GameScreen> with SingleTickerProviderStateMixin {

  @override
  void initState(){
     super.initState();
     UnityBloc.onAdCompleted = _onAddCompleted;
  }

  @override
  void dispose(){
     super.dispose();
     if( UnityBloc.onAdCompleted == _onAddCompleted) {
         UnityBloc.onAdCompleted = null;
     }
  }

  void _onAddCompleted(String placementId, FinishState result) 
    => print("Finished $placementId with $result"); 

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container( 
            // game content here
        ),
        MaterialButton(
          child: Text("Tap Here to Watch Ad to Undo Move"),
          onPressed: () => unityBloc.showUnityAd(),
          // (side note: unityBloc is accessable because I have it declared as "final unityBloc = UnityBloc();" higher up the widget tree on app.dart.)
        ),
      ],
    ),                  
  }

  // This is the function I want to call after the ad is finished.
  void undoMove() {
    setState(() {
      // code to undo move
    });
  }
}