Closed drdDavi closed 4 years ago
Hi there. Yes, then retain access to that one instance--in your second screen. One way to do that is to use the keyword, static, on the variable referencing the Ads class instance. The 'Ads' instance created in the first screen is then accessible in the second screen. To get a better understanding of what I mean, take the two screens below and save them in separate Dart files.
First Screen
import 'package:flutter/material.dart';
import 'dart:io' show Platform;
import 'package:firebase_admob/firebase_admob.dart';
import 'package:ads/ads.dart';
import 'secondScreen.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key key}) : super(key: key);
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
final navigatorKey = GlobalKey<NavigatorState>();
static final String appId = Platform.isAndroid
? 'ca-app-pub-3940256099942544~3347511713'
: 'ca-app-pub-3940256099942544~1458002511';
static final String bannerUnitId = Platform.isAndroid
? 'ca-app-pub-3940256099942544/6300978111'
: 'ca-app-pub-3940256099942544/2934735716';
/// Assign a listener.
static AdEventListener eventListener = (MobileAdEvent event) {
if (event == MobileAdEvent.clicked) {
print("The opened ad is clicked on.");
}
};
static Ads ads = Ads(
appId,
bannerUnitId: bannerUnitId,
keywords: <String>['ibm', 'computers'],
contentUrl: 'http://www.ibm.com',
childDirected: false,
testDevices: ['Samsung_Galaxy_SII_API_26:5554'],
testing: false,
listener: eventListener,
);
@override
void initState() {
super.initState();
ads.showBannerAd();
}
@override
void dispose() {
ads.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: navigatorKey,
home: Scaffold(
appBar: AppBar(
title: const Text('AdMob Banner Ad Example'),
),
body: SingleChildScrollView(
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
RaisedButton(
key: ValueKey<String>('SHOW BANNER'),
child: const Text('SHOW BANNER'),
onPressed: () {
ads.showBannerAd(state: this, anchorOffset: null);
}),
RaisedButton(
key: ValueKey<String>('REMOVE BANNER'),
child: const Text('REMOVE BANNER'),
onPressed: () {
ads.hideBannerAd();
}),
RaisedButton(
key: ValueKey<String>('SEONDSCREEN'),
child: const Text('ANOTHER SCREEN'),
onPressed: () {
navigatorKey.currentState.push(
MaterialPageRoute(
builder: (context) => SecondScreen(),
),
);
},
),
].map((Widget button) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: button,
);
}).toList(),
),
),
),
),
);
}
}
Second Screen
import 'package:flutter/material.dart';
import 'package:admob_app/example01.dart';
class SecondScreen extends StatelessWidget {
const SecondScreen({this.key});
final Key key;
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('AdMob Banner Ad Example'),
),
body: SingleChildScrollView(
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
RaisedButton(
key: ValueKey<String>('SHOW BANNER'),
child: const Text('SHOW BANNER'),
onPressed: () {
MyAppState.ads.showBannerAd();
}),
RaisedButton(
key: ValueKey<String>('REMOVE BANNER'),
child: const Text('REMOVE BANNER'),
onPressed: () {
MyAppState.ads.hideBannerAd();
}),
].map((Widget button) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: button,
);
}).toList(),
),
),
),
);
}
}
In the example above, the first screen is placed in the Dart file, example01.dart, and then referenced in the import statement in the second Dart file as follows:
import 'example01.dart';
Thank you sir!
But is there a way to do it with let say a state manager like Provider.
I am not a fan of pass the current state between screen. Can i set it up in a class and call and interact with it from anywhere in the app?
Sorry for the questions, i only have a year’s worth of experience.
Regard
"Can i set it up in a class and call and interact with it from anywhere in the app?"
Indeed, you can. Below is the class, AppAds, for example. It's self-contained. It defines two functions you can then use 'everywhere.' Simply import the Dart file, AppAds.dart, and call the commands, AppAds.showBanner() and AppAds.dispose() appropriately. I'll let you determine how you would do such a thing with your Interstitial ad and your video ad.
AppAds.dart
import 'package:flutter/material.dart';
import 'dart:io' show Platform;
import 'package:firebase_admob/firebase_admob.dart';
import 'package:ads/ads.dart';
class AppAds {
static final String _appId = Platform.isAndroid
? 'ca-app-pub-3940256099942544~3347511713'
: 'ca-app-pub-3940256099942544~1458002511';
static final String _bannerUnitId = Platform.isAndroid
? 'ca-app-pub-3940256099942544/6300978111'
: 'ca-app-pub-3940256099942544/2934735716';
/// Assign a listener.
static AdEventListener _eventListener = (MobileAdEvent event) {
if (event == MobileAdEvent.clicked) {
print("The opened ad is clicked on.");
}
};
static Ads _ads = Ads(
_appId,
bannerUnitId: _bannerUnitId,
keywords: <String>['ibm', 'computers'],
contentUrl: 'http://www.ibm.com',
childDirected: false,
testDevices: ['Samsung_Galaxy_SII_API_26:5554'],
testing: false,
listener: _eventListener,
);
static void showBanner(
{String adUnitId,
AdSize size,
List<String> keywords,
String contentUrl,
bool childDirected,
List<String> testDevices,
bool testing,
AdEventListener listener,
State state,
double anchorOffset,
AnchorType anchorType}) =>
_ads.showBannerAd(
adUnitId: adUnitId,
size: size,
keywords: keywords,
contentUrl: contentUrl,
childDirected: childDirected,
testDevices: testDevices,
testing: testing,
listener: listener,
state: state,
anchorOffset: anchorOffset,
anchorType: anchorType);
static void hideBanner() => _ads.hideBannerAd();
static void dispose() => _ads.dispose();
}
Sir!!
Thanks a million.
Regards
How to close Banner add from different page? Or load Interstitial Ad on Different Page? Given that i can only create one instance? For me now its not doing anything.