From 943c26c4bc3108dfb07f5f9c48c377eff232c61e Mon Sep 17 00:00:00 2001 From: Bharat Sharma Date: Thu, 25 Mar 2021 22:06:21 +0530 Subject: [PATCH] ptch: improve and add ads --- lib/component/appAds.dart | 62 ++++++++++- lib/component/category_tile.dart | 18 ++-- lib/main.dart | 17 ++- lib/screens/category_screen.dart | 177 ++++++++++++++----------------- lib/screens/splash_screen.dart | 10 +- 5 files changed, 160 insertions(+), 124 deletions(-) mode change 100644 => 100755 lib/component/appAds.dart mode change 100644 => 100755 lib/component/category_tile.dart mode change 100644 => 100755 lib/main.dart mode change 100644 => 100755 lib/screens/category_screen.dart mode change 100644 => 100755 lib/screens/splash_screen.dart diff --git a/lib/component/appAds.dart b/lib/component/appAds.dart old mode 100644 new mode 100755 index 8513281..d8b99fe --- a/lib/component/appAds.dart +++ b/lib/component/appAds.dart @@ -1,9 +1,14 @@ import 'dart:io'; +import 'package:google_mobile_ads/google_mobile_ads.dart'; + class AddManager { - static String get appId { + AddManager(this.initialization); + Future initialization; + + String get appId { if (Platform.isAndroid) { - return "ca-app-pub-1828204131712236~3033530989";//"ca-app-pub-3940256099942544~4354546703"; + return "ca-app-pub-1828204131712236~3033530989"; } else if (Platform.isIOS) { return "APPSTORE_KEY"; } else { @@ -11,9 +16,9 @@ class AddManager { } } - static String get bannerAdUnitId { + String get bottomBannerAdId { if (Platform.isAndroid) { - return "ca-app-pub-1828204131712236/2650387606";//"ca-app-pub-3940256099942544/8865242552"; + return "ca-app-pub-1828204131712236/2650387606"; } else if (Platform.isIOS) { return "IOS_BANNER_KEY"; } else { @@ -21,7 +26,17 @@ class AddManager { } } - static String get interstitialAdUnitId { + String get topBannerAdId { + if (Platform.isAndroid) { + return "ca-app-pub-1828204131712236/8522925412"; + } else if (Platform.isIOS) { + return "IOS_BANNER_KEY"; + } else { + throw UnsupportedError("Unsupported Platform"); + } + } + + String get themeInterstitialAdId { if (Platform.isAndroid) { return "ca-app-pub-1828204131712236/8318238523"; } else if (Platform.isIOS) { @@ -30,4 +45,41 @@ class AddManager { throw new UnsupportedError("Unsupported platform"); } } + + String get aboutInterstitialAdId { + if (Platform.isAndroid) { + return "ca-app-pub-1828204131712236/1957517061"; + } else if (Platform.isIOS) { + return "IOS_INTERSTITIAL_AD_ID"; + } else { + throw new UnsupportedError("Unsupported platform"); + } + } + + String get testBanerAdId => 'ca-app-pub-3940256099942544/6300978111'; + String get testInterstitialAdId => 'ca-app-pub-3940256099942544/1033173712'; + + AdListener get adListener => _adListener; + + final AdListener _adListener = AdListener( + // Called when an ad is successfully received. + onAdLoaded: (Ad ad) => print('Ad--${ad.adUnitId} loaded.'), + // Called when an ad request failed. + onAdFailedToLoad: (Ad ad, LoadAdError error) { + ad.dispose(); + print('Ad--${ad.adUnitId} failed to load: $error'); + }, + // Called when an ad opens an overlay that covers the screen. + onAdOpened: (Ad ad) => print('Ad--${ad.adUnitId} opened.'), + // Called when an ad removes an overlay that covers the screen. + onAdClosed: (Ad ad) { + print('Ad--${ad.adUnitId} closed.'); + if (ad is InterstitialAd) { + ad.dispose(); + ad.load(); + } + }, + // Called when an ad is in the process of leaving the application. + onApplicationExit: (Ad ad) => print('Ad--${ad.adUnitId} ---- Left application.'), + ); } diff --git a/lib/component/category_tile.dart b/lib/component/category_tile.dart old mode 100644 new mode 100755 index dc8e9bb..94cda11 --- a/lib/component/category_tile.dart +++ b/lib/component/category_tile.dart @@ -1,5 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:page_transition/page_transition.dart'; import 'package:unito/component/category.dart'; import 'package:unito/screens/unit_screen.dart'; @@ -19,10 +18,11 @@ class CategoryTile extends StatelessWidget { final height = MediaQuery.of(context).size.height; void _openConverterRoute(Category categoryUnit) { - Navigator.of(context).push(PageTransition( - child: UnitConverter(category: categoryUnit), - type: PageTransitionType.rightToLeftWithFade, - )); + Navigator.of(context).push( + MaterialPageRoute( + builder: (context) => UnitConverter(category: categoryUnit), + ), + ); } return Padding( @@ -35,7 +35,7 @@ class CategoryTile extends StatelessWidget { borderRadius: _borderRadius, boxShadow: [ BoxShadow( - color: Color.fromRGBO(46, 46, 46, 0.05), + color: Color.fromRGBO(46, 46, 46, 0.03), offset: Offset(0.0, 0.0), spreadRadius: 5, blurRadius: 10, @@ -61,11 +61,11 @@ class CategoryTile extends StatelessWidget { padding: _padding8, child: Image.asset( category.iconLocation, - height: 0.07226810673443456392 * height, - width: 0.07226810673443456392 * height, + height: 0.065 * height, + width: 0.065 * height, ), ), - SizedBox(height: 0.00222363405336721735 * height), + SizedBox(height: 0.0022 * height), Text( category.name, style: Theme.of(context).textTheme.caption, diff --git a/lib/main.dart b/lib/main.dart old mode 100644 new mode 100755 index c285578..a2deb23 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,16 +1,23 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:google_mobile_ads/google_mobile_ads.dart'; import 'package:provider/provider.dart'; import 'package:unito/app.dart'; +import 'package:unito/component/appAds.dart'; import 'package:unito/theme/themeChanger.dart'; void main() { - /// This ensures that the orientation is always portrait - WidgetsFlutterBinding.ensureInitialized(); - SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) => runApp( - Unito(), - )); + final initFuture = MobileAds.instance.initialize(); + final adManager = AddManager(initFuture); + + /// This ensures that the orientation is always portrait + SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then( + (_) => runApp(Provider.value( + value: adManager, + builder: (context, child) => Unito(), + )), + ); } /// Unit Converter App. diff --git a/lib/screens/category_screen.dart b/lib/screens/category_screen.dart old mode 100644 new mode 100755 index 86f2f60..b66c38d --- a/lib/screens/category_screen.dart +++ b/lib/screens/category_screen.dart @@ -1,7 +1,7 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; -import 'package:page_transition/page_transition.dart'; +import 'package:google_mobile_ads/google_mobile_ads.dart'; import 'package:unito/component/appAds.dart'; import 'package:unito/component/category.dart'; import 'package:unito/component/category_tile.dart'; @@ -10,7 +10,6 @@ import 'package:unito/screens/menu_screen.dart'; import 'package:unito/theme/themeChanger.dart'; import 'package:provider/provider.dart'; import 'package:unito/theme/themes.dart'; -import 'package:firebase_admob/firebase_admob.dart'; /// Builds the main screen. /// Containing the title, buttons and GridView @@ -56,86 +55,55 @@ class _CategoryScreenState extends State { 'assets/icons/time2.png', ]; - BannerAd _bannerAd; - InterstitialAd _interstitialAd; - - bool _isInterstitialAdReady; - - void _loadInterstitialAd() { - print("Loading Interstitial Ad"); - _interstitialAd.load(); - } - - void _reloadInterstitialAd() async { - await _interstitialAd.dispose(); - - _isInterstitialAdReady = false; - - _interstitialAd = InterstitialAd( - adUnitId: AddManager.interstitialAdUnitId, - listener: _onInterstitialAdEvent, - ); - - _interstitialAd.load(); - } - - void _moveToHome() {} - - void _onInterstitialAdEvent(MobileAdEvent event) { - switch (event) { - case MobileAdEvent.loaded: - print("---------------------------> Interstitial Ad Loaded"); - _isInterstitialAdReady = true; - break; - case MobileAdEvent.failedToLoad: - _isInterstitialAdReady = false; - print("---------------------------> Failed to load an interstitial ad"); - break; - case MobileAdEvent.closed: - _moveToHome(); - break; - default: - // NOTHING - } - } - - void _loadBannerAd() { - _bannerAd - ..load() - ..show(anchorType: AnchorType.bottom); - } - - @override - void initState() { - _isInterstitialAdReady = false; - - _bannerAd = BannerAd( - adUnitId: AddManager.bannerAdUnitId, - size: AdSize.banner, - ); - - _interstitialAd = InterstitialAd( - adUnitId: AddManager.interstitialAdUnitId, - listener: _onInterstitialAdEvent, - ); - - _loadBannerAd(); - _loadInterstitialAd(); - - super.initState(); - } + BannerAd _bannerAdBottom; + BannerAd _bannerAdTop; + InterstitialAd _interstitialAdTheme; + InterstitialAd _interstitialAdAbout; @override void dispose() { - _bannerAd?.dispose(); - _interstitialAd?.dispose(); - + _bannerAdBottom.dispose(); + _bannerAdTop.dispose(); + _interstitialAdTheme.dispose(); + _interstitialAdAbout.dispose(); super.dispose(); } @override Future didChangeDependencies() async { super.didChangeDependencies(); + if (_bannerAdBottom == null || _bannerAdTop == null || _interstitialAdTheme == null) { + final _adManager = Provider.of(context); + _adManager.initialization.then((status) { + setState(() { + _bannerAdBottom = BannerAd( + size: AdSize.banner, + adUnitId: _adManager.bottomBannerAdId, + request: AdRequest(), + listener: _adManager.adListener, + )..load(); + + _bannerAdTop = BannerAd( + size: AdSize.banner, + adUnitId: _adManager.topBannerAdId, + request: AdRequest(), + listener: _adManager.adListener, + )..load(); + + _interstitialAdTheme = InterstitialAd( + adUnitId: _adManager.themeInterstitialAdId, + listener: _adManager.adListener, + request: AdRequest(), + )..load(); + + _interstitialAdAbout = InterstitialAd( + adUnitId: _adManager.aboutInterstitialAdId, + listener: _adManager.adListener, + request: AdRequest(), + )..load(); + }); + }); + } if (_categories.isEmpty) { await _retrieveLocalCategories(); } @@ -208,7 +176,7 @@ class _CategoryScreenState extends State { updateCategoryIcons(); return GridView.count( crossAxisCount: 2, - childAspectRatio: 1, + childAspectRatio: 1.2, padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 1.0), children: _categories.map((Category c) { return CategoryTile(category: c); @@ -224,28 +192,24 @@ class _CategoryScreenState extends State { mainAxisAlignment: MainAxisAlignment.start, children: [ /// Spacer - SizedBox(height: 0.02401359593392630365 * height), + SizedBox(height: 0.005501359593392630365 * height), /// Top Icon Row Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, + mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ /// Spacer - SizedBox(width: width / 90), /// Theme toggle icon IconButton( onPressed: () async { - if (_isInterstitialAdReady) { - _interstitialAd.show(); + if (_interstitialAdTheme != null) { + _interstitialAdTheme.show(); } - print("Theme change toogled"); + print('Theme changed toggled'); theme.themeData = isDarkTheme ? appTheme.getLightTheme() : appTheme.getDarkTheme(); - - _moveToHome(); - _reloadInterstitialAd(); }, padding: EdgeInsets.all(0.0), highlightColor: Colors.transparent, @@ -256,7 +220,7 @@ class _CategoryScreenState extends State { color: Theme.of(context).iconTheme.color, ), ), - SizedBox(width: width / 2), + SizedBox(width: width / 3), /// Menu Icon FlatButton( @@ -264,11 +228,13 @@ class _CategoryScreenState extends State { splashColor: Colors.transparent, highlightColor: Colors.transparent, onPressed: () { - print('Height: $height || Width: $width'); - Navigator.of(context).push(PageTransition( - child: MenuScreen(), - type: PageTransitionType.rightToLeftWithFade, - )); + if (_interstitialAdAbout != null) { + _interstitialAdAbout.show(); + } + + Navigator.of(context).push( + MaterialPageRoute(builder: (context) => MenuScreen()), + ); }, child: getIcon(), ), @@ -276,7 +242,7 @@ class _CategoryScreenState extends State { ), /// Spacer - SizedBox(height: 0.01901359593392630365 * height), + SizedBox(height: 0.01001359593392630365 * height), /// Heading Container Row( @@ -290,14 +256,14 @@ class _CategoryScreenState extends State { style: Theme.of(context) .textTheme .headline5 - .copyWith(fontSize: 0.05559085133418043379 * height), + .copyWith(fontSize: 0.05059085133418043379 * height), ), Text( "Converter", style: Theme.of(context) .textTheme .headline5 - .copyWith(fontSize: 0.05559085133418043379 * height), + .copyWith(fontSize: 0.05059085133418043379 * height), ), ], ), @@ -305,11 +271,11 @@ class _CategoryScreenState extends State { ), /// Spacer - SizedBox(height: 0.03901359593392630365 * height), + SizedBox(height: 0.02001359593392630365 * height), /// Container for sub-heading Container( - width: 0.43472222222222220737 * width, + width: 0.39472222222222220737 * width, decoration: BoxDecoration( borderRadius: BorderRadius.circular(70), color: Theme.of(context).accentColor, @@ -321,13 +287,22 @@ class _CategoryScreenState extends State { style: Theme.of(context) .textTheme .subtitle1 - .copyWith(fontSize: 0.01890088945362134749 * height), + .copyWith(fontSize: 0.01590088945362134749 * height), ), ), ), + SizedBox(height: 0.02035451080050826027 * height), + + if (_bannerAdTop == null) + SizedBox(height: 50) + else + Container( + height: 50, + child: AdWidget(ad: _bannerAdTop), + ), /// Spacer - SizedBox(height: 0.02035451080050826027 * height), + // SizedBox(height: 0.02035451080050826027 * height), ], ); } @@ -340,13 +315,21 @@ class _CategoryScreenState extends State { children: [ Expanded(flex: 3, child: _topBar()), Expanded(flex: 5, child: _buildCategoryGrid()), + if (_bannerAdBottom == null) + SizedBox(height: 50) + else + Container( + height: 60, + alignment: Alignment.bottomCenter, + child: AdWidget(ad: _bannerAdBottom), + ), ], ); } return Scaffold( backgroundColor: Theme.of(context).backgroundColor, - resizeToAvoidBottomPadding: false, + resizeToAvoidBottomInset: false, body: SafeArea( child: listView(), ), diff --git a/lib/screens/splash_screen.dart b/lib/screens/splash_screen.dart old mode 100644 new mode 100755 index 9196569..98ba6ca --- a/lib/screens/splash_screen.dart +++ b/lib/screens/splash_screen.dart @@ -1,9 +1,7 @@ import 'dart:async'; -import 'package:firebase_admob/firebase_admob.dart'; import 'package:flare_flutter/flare_actor.dart'; import 'package:flutter/material.dart'; -import 'package:unito/component/appAds.dart'; class SplashScreen extends StatefulWidget { @override @@ -13,7 +11,7 @@ class SplashScreen extends StatefulWidget { class _SplashScreenState extends State { /// Method to display [SplashScreen] for the given duration startTime() async { - var _duration = Duration(seconds: 2); + var _duration = Duration(milliseconds: 1500); return Timer(_duration, navigationPage); } @@ -22,14 +20,10 @@ class _SplashScreenState extends State { Navigator.of(context).pushReplacementNamed('/CategoryScreen'); } - Future _initAdMob() async { - return FirebaseAdMob.instance.initialize(appId: AddManager.appId); - } - @override void initState() { super.initState(); - _initAdMob().then((_) => startTime()); + startTime(); } @override