flutter_google_ad_manager icon indicating copy to clipboard operation
flutter_google_ad_manager copied to clipboard

When added Key, onAdLoaded() callback continues to be called on iOS

Open bocheung77 opened this issue 5 years ago • 4 comments

IOS always to update screen, but android not like this it will collapse in my app, how to fix this?

bocheung77 avatar May 08 '19 06:05 bocheung77

S.P. i find this issue, if i added GlobalKey in ListView Container. then will get collapse

bocheung77 avatar May 08 '19 07:05 bocheung77

@bocheung77 Is the bug related to this plugin?

If there is a relationship, please create a small project that can be reproduced and tell me.

ko2ic avatar May 09 '19 04:05 ko2ic

@ko2ic i don't know, may be? i create a small project for my case

main.dart

import 'package:flutter/material.dart';
import 'test.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        child:  new Center(
          child: FlatButton(
            color: Colors.blueAccent,
            child: Text('GO!'),
            onPressed: () {
              Navigator.of(context).pushAndRemoveUntil(
                  MaterialPageRoute(
                    builder: (context) => TestPage(title: 'asd'),
                  ),
                      (_) => false);
            },
          ),
        ),
      )
    );
  }
}

text.dart

import 'package:flutter/material.dart';
import 'package:flutter_google_ad_manager/flutter_google_ad_manager.dart';


class MyTestDevices extends TestDevices {
  static MyTestDevices _instance;

  factory MyTestDevices() {
    if (_instance == null) _instance = new MyTestDevices._internal();
    return _instance;
  }

  MyTestDevices._internal();

  @override
  List<String> get values => List()..add("XXXXXXXX");
}

class TestPage extends StatefulWidget {
  TestPage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  DFPInterstitialAd _interstitialAd;
  DFPRewardedAd _rewardedAd;
  LifecycleEventHandler _lifecycle;
  Map<int, GlobalKey> keyRedList = new Map<int, GlobalKey>();

  @override
  void initState() {
    super.initState();

    _interstitialAd = DFPInterstitialAd(
      isDevelop: true,
      adUnitId: "XXXXXXXX",
      onAdLoaded: () {
        print('interstitialAd onAdLoaded');
      },
      onAdFailedToLoad: (errorCode) {
        print('interstitialAd onAdFailedToLoad: errorCode:$errorCode');
      },
      onAdOpened: () {
        print('interstitialAd onAdOpened');
      },
      onAdClosed: () {
        print('interstitialAd onAdClosed');
        _interstitialAd.load();
      },
      onAdLeftApplication: () {
        print('interstitialAd onAdLeftApplication');
      },
    );
    _interstitialAd.load();

    _rewardedAd = DFPRewardedAd(
      isDevelop: true,
      adUnitId: "XXXXXXX",
      onAdLoaded: () {
        print('rewardedAd onAdLoaded');
      },
      onAdFailedToLoad: (errorCode) {
        print('rewardedAd onAdFailedToLoad: errorCode:$errorCode');
      },
      onAdOpened: () {
        print('rewardedAd onAdOpened');
      },
      onAdClosed: () {
        print('rewardedAd onAdClosed');
        _rewardedAd.load();
      },
      onAdLeftApplication: () {
        print('rewardedAd onAdLeftApplication');
      },
      onRewarded: (String type, int amount) {
        print('rewardedAd onRewarded: type:$type amount:$amount');
      },
      onVideoStarted: () {
        print('rewardedAd onVideoStarted');
      },
      onVideoCompleted: () {
        print('rewardedAd onVideoCompleted');
      },
    );
    _rewardedAd.load();
    _lifecycle = LifecycleEventHandler(_rewardedAd);
    WidgetsBinding.instance.addObserver(_lifecycle);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(_lifecycle);
    _interstitialAd.dispose();
    _rewardedAd.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView.builder(
        padding: EdgeInsets.all(20.0),
        itemCount: 100,
        itemBuilder: (BuildContext context, int index) {
          keyRedList[index] = new GlobalKey();

          if (index != 0 && index % 4 == 0) {
            return Column(
              children: <Widget>[
                Container(
                  key: keyRedList[index],
                  margin: EdgeInsets.only(top: 16, bottom: 16.0),
                  child: DFPBanner(
                    isDevelop: true,
                    testDevices: MyTestDevices(),
                    adUnitId: '/XXXXXXXXX/XXXXXXXXX',
                    adSize: DFPAdSize.BANNER,
                    onAdLoaded: () {
                      print('Banner onAdLoaded');
                    },
                    onAdFailedToLoad: (errorCode) {
                      print('Banner onAdFailedToLoad: errorCode:$errorCode');
                    },
                    onAdOpened: () {
                      print('Banner onAdOpened');
                    },
                    onAdClosed: () {
                      print('Banner onAdClosed');
                    },
                    onAdLeftApplication: () {
                      print('Banner onAdLeftApplication');
                    },
                  ),
                ),
              ],
            );
          }
          return SizedBox(
            key: keyRedList[index],
            height: 100.0,
            child: Card(
              child: Center(
                child: Text('$index'),
              ),
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          await _rewardedAd.show();
          //await interstitialAd.show();
        },
        tooltip: '?',
        child: Icon(Icons.add),
      ),
    );
  }
}

class LifecycleEventHandler extends WidgetsBindingObserver {
  final DFPRewardedAd rewardedAd;

  LifecycleEventHandler(this.rewardedAd);

  @override
  didChangeAppLifecycleState(AppLifecycleState state) async {
    switch (state) {
      case AppLifecycleState.inactive:
        break;
      case AppLifecycleState.paused:
        await rewardedAd.pause();
        break;
      case AppLifecycleState.suspending:
        break;
      case AppLifecycleState.resumed:
        await rewardedAd.resume();
        break;
    }
  }
}

bocheung77 avatar May 14 '19 03:05 bocheung77

update: if screen have image (like .gif), will be get this bug

bocheung77 avatar Jun 05 '19 06:06 bocheung77