-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented banner ads and setup app for first release
- Loading branch information
1 parent
504d406
commit f8230b3
Showing
13 changed files
with
184 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,5 @@ app.*.map.json | |
/android/app/debug | ||
/android/app/profile | ||
/android/app/release | ||
|
||
/android/key.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import 'dart:io'; | ||
|
||
class AdState { | ||
static bool appInDevMode = false; | ||
|
||
static String get appId => "ca-app-pub-7101902781826262~1954409331"; | ||
|
||
static String get homePageAdUnitId => Platform.isAndroid | ||
? appInDevMode | ||
? "ca-app-pub-3940256099942544/6300978111" | ||
: "ca-app-pub-7101902781826262/7662031583" | ||
: ""; | ||
|
||
static String get detailsPageAdUnitId => Platform.isAndroid | ||
? appInDevMode | ||
? "ca-app-pub-3940256099942544/6300978111" | ||
: "ca-app-pub-7101902781826262/6529462058" | ||
: ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import 'dart:developer'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:google_mobile_ads/google_mobile_ads.dart'; | ||
|
||
class MyBannarAd extends StatefulWidget { | ||
final String adUnit; | ||
const MyBannarAd({ | ||
Key? key, | ||
required this.adUnit, | ||
}) : super(key: key); | ||
|
||
@override | ||
MyBannarAdState createState() => MyBannarAdState(); | ||
} | ||
|
||
class MyBannarAdState extends State<MyBannarAd> { | ||
BannerAd? _anchoredAdaptiveAd; | ||
bool _isLoaded = false; | ||
late Orientation _currentOrientation; | ||
|
||
@override | ||
void didChangeDependencies() { | ||
super.didChangeDependencies(); | ||
_currentOrientation = MediaQuery.of(context).orientation; | ||
_loadAd(); | ||
} | ||
|
||
/// Load another ad, disposing of the current ad if there is one. | ||
Future<void> _loadAd() async { | ||
await _anchoredAdaptiveAd?.dispose(); | ||
setState(() { | ||
_anchoredAdaptiveAd = null; | ||
_isLoaded = false; | ||
}); | ||
|
||
final AnchoredAdaptiveBannerAdSize? size = | ||
await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize( | ||
MediaQuery.of(context).size.width.truncate()); | ||
|
||
if (size == null) { | ||
print('Unable to get height of anchored banner.'); | ||
return; | ||
} | ||
|
||
_anchoredAdaptiveAd = BannerAd( | ||
adUnitId: widget.adUnit, | ||
size: size, | ||
request: AdRequest(), | ||
listener: BannerAdListener( | ||
onAdLoaded: (Ad ad) { | ||
log('$ad loaded: ${ad.responseInfo}'); | ||
setState(() { | ||
// When the ad is loaded, get the ad size and use it to set | ||
// the height of the ad container. | ||
_anchoredAdaptiveAd = ad as BannerAd; | ||
_isLoaded = true; | ||
}); | ||
}, | ||
onAdFailedToLoad: (Ad ad, LoadAdError error) { | ||
log('Anchored adaptive banner failedToLoad: $error'); | ||
ad.dispose(); | ||
}, | ||
), | ||
); | ||
return _anchoredAdaptiveAd!.load(); | ||
} | ||
|
||
/// Gets a widget containing the ad, if one is loaded. | ||
/// | ||
/// Returns an empty container if no ad is loaded, or the orientation | ||
/// has changed. Also loads a new ad if the orientation changes. | ||
Widget _getAdWidget() { | ||
return OrientationBuilder( | ||
builder: (context, orientation) { | ||
if (_currentOrientation == orientation && | ||
_anchoredAdaptiveAd != null && | ||
_isLoaded) { | ||
return SizedBox( | ||
// color: Colors.green, | ||
width: _anchoredAdaptiveAd!.size.width.toDouble(), | ||
height: _anchoredAdaptiveAd!.size.height.toDouble(), | ||
child: AdWidget(ad: _anchoredAdaptiveAd!), | ||
); | ||
} | ||
// Reload the ad if the orientation changes. | ||
if (_currentOrientation != orientation) { | ||
_currentOrientation = orientation; | ||
_loadAd(); | ||
} | ||
return Container( | ||
height: 0, | ||
); | ||
}, | ||
); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
super.dispose(); | ||
_anchoredAdaptiveAd?.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return _getAdWidget(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters