简体   繁体   English

如何在来自另一个 class 的有状态小部件中触发 function(当 onUnityAdsFinish 时)?

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

I have a game screen in a stateful widget that includes a function that lets the player take a step back (undo the previous move).我在一个有状态的小部件中有一个游戏屏幕,其中包括一个 function,它可以让玩家后退一步(撤消上一步)。 I would like to trigger this function when onUnityAdsFinish() runs from another widget.当 onUnityAdsFinish() 从另一个小部件运行时,我想触发这个 function。

Here's the stateful widget on game.dart where the game is played:这是 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
    });
  }
}

When the button is pressed to watch the ad, it calls the showUnityAd() function in my UnityBloc class on unity_bloc.dart:当按下按钮观看广告时,它会在我的 UnityBloc class on unity_bloc.dart 中调用 showUnityAd() function:

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

The ad displays perfectly and the print statement in onUnityAdsFinish prints to the console when the ad is finished, but I can't figure out how to call undoMove at that point.广告完美显示,并且 onUnityAdsFinish 中的打印语句在广告完成后打印到控制台,但我不知道此时如何调用 undoMove。

I have spent all day watching tutorials and reading stackoverflows about callbacks and global keys and such, but I'm just still not getting it.我整天都在看教程和阅读关于回调和全局键等的stackoverflows,但我仍然没有得到它。

Can someone please help?有人可以帮忙吗? How can I trigger the undoMove() function on game.dart when onUnityAdsFinish() fires on unity_bloc.dart?当 onUnityAdsFinish() 在 unity_bloc.ZBB14127678960FAE97D873950EA20156 上触发时,如何在 game.dart 上触发 undoMove() function? (Or is their another/better way to do this?) (或者他们是另一种/更好的方法吗?)

Thank you!谢谢!

You can have a variable that holds a function in the UnityBloc class and pass the undoMove method to the showUnityAd method to assign it to the variable and then you can call it in onUnityAdsFinish method.您可以在UnityBloc class 中拥有一个包含 function 的变量,并将showUnityAd undoMove以将其分配给变量,然后您可以在 onUnityAdsFinish 方法中调用它。

So UnityBloc becomes this:所以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
  }
}

And you can it in the MaterialButton 's onPressed like this:你可以在MaterialButtononPressed这样:

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

Simplest approach would be a global/static variable (callback) or a ValueNotifier/Stream.最简单的方法是全局/静态变量(回调)或 ValueNotifier/Stream。 Maybe, a cleaner approach, would be some package with dependency injection capabilities like ( get , get_it ).也许,更清洁的方法是一些 package 具有依赖注入功能,如( getget_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
    });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:如何从另一个有状态类更新 UI 状态小部件 - Flutter:How to update a UI -stateful widget from another stateful class 如何在具有来自另一个有状态小部件的 setState function 的 class 中调用 function ? - How can I call a function in a class that has the setState function from another stateful widget? 将数据从另一个 class 导入到 Stateful Widget - Importing Data from another class to Stateful Widget 如何从另一个有状态小部件调用有状态小部件中的函数? - How to call a function in a Stateful Widget from another StatefulWidget? 如何在另一个有状态小部件中调用 Void() function - How to call Void() function in another Stateful widget 如何从有状态小部件调用 function - How to call function from Stateful widget 如何在另一个类小部件中获取自定义有状态小部件值? - How to Get Custom Stateful Widget value in another class widget? 从flutter中的另一个类的有状态小部件中获取整数的值 - Get the value of an integer from a stateful widget from another class in flutter 从另一个小部件/ class 更新有状态 class 内的变量 - Update a variable inside stateful class from another widget / class 从另一个StatefulWidget的Stateful Widget调用函数 - Call function from Stateful Widget from another StatefulWidget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM