Flutter admob show()方法重建“ /”页面

时间:2018-09-26 14:49:33

标签: dart admob flutter

我为尝试“ admob”插件编写了一个非常简单的应用。但是,我在StateAdExample()类中遇到关于_bannerAd ..load()..show();的问题。

我首先运行应用程序,首先打开Home()类,然后按按钮打开其他屏幕(StateAdExample)。然后,当我按下SHOW BANNER按钮时,它显示了我的横幅,但是在admob再次生成之前,先重建Home(),然后再重建StateAdExample()。当我按下SHOW BANNER按钮时,我可以在App.dart中看到我的print("----- /");

我只想重建我的StateAdExample class()。

App.dart

Route routes(RouteSettings settings) {
switch (settings.name) {
  case "/":
    return MaterialPageRoute(
        settings: settings,
        maintainState: true,
        builder: (BuildContext context) {
          print("----- /");
          return Home();
        });
    break;
  case "/stateAdExapmle":
    return MaterialPageRoute(
        settings: settings,
        builder: (BuildContext context) {
          print("----- /stateAdExapmle");
          return StateAdExample();
        });
    break;
  default:
    return null;
}}

Home.dart

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: ScopedModelDescendant(
          rebuildOnChange: false,
          builder: (BuildContext context, Widget widget, AdScoped model) {
            return Column(
              children: <Widget>[
                RaisedButton(
                  child: Text("2.Screen"),
                  onPressed: () {
                    Navigator.pushNamed(context, "/stateAdExapmle");
                  },
                ),
              ],
            );
          }),
    );
  }
}

StateAdExample.dart

const String testDevice = 'YOUR_DEVICE_ID';

class StateAdExample extends StatefulWidget {
  @override
  _StateAdExapmleState createState() => _StateAdExapmleState();
}

class _StateAdExapmleState extends State<StateAdExample> {
  static final MobileAdTargetingInfo targetingInfo = new MobileAdTargetingInfo(
    testDevices: testDevice != null ? <String>[testDevice] : null,
    keywords: <String>['foo', 'bar'],
    contentUrl: 'http://foo.com/bar.html',
    birthday: new DateTime.now(),
    childDirected: true,
    gender: MobileAdGender.male,
    nonPersonalizedAds: true,
  );

  BannerAd _bannerAd;

  BannerAd createBannerAd() {
    return new BannerAd(
      adUnitId: BannerAd.testAdUnitId,
      size: AdSize.banner,
      targetingInfo: targetingInfo,
      listener: (MobileAdEvent event) {
        print("BannerAd event $event");
      },
    );
  }

  @override
  void initState() {
    super.initState();
    FirebaseAdMob.instance.initialize(appId: FirebaseAdMob.testAppId);
    _bannerAd = createBannerAd()..load();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        children: <Widget>[
          RaisedButton(
              child: const Text('SHOW BANNER'),
              onPressed: () {
                _bannerAd ??= createBannerAd();
                _bannerAd
                  ..load()
                  ..show();
              }),
          RaisedButton(
              child: const Text('REMOVE BANNER'),
              onPressed: () {
                _bannerAd?.dispose();
                _bannerAd = null;
              }),
        ],
      ),
    );
  }
}

如何解决此问题?

0 个答案:

没有答案