From da21720645318986fcb41292f41b823df60b89a8 Mon Sep 17 00:00:00 2001 From: Bhargav Raviya Date: Sat, 30 Sep 2023 11:08:21 +0530 Subject: [PATCH] changes BannerAdWidget --- lib/controllers/crud_controller.dart | 4 +-- lib/main.dart | 5 +++- lib/widgets/banner_ad_widget.dart | 45 ++++++++++++++++++---------- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/lib/controllers/crud_controller.dart b/lib/controllers/crud_controller.dart index 3cf7336..013f363 100644 --- a/lib/controllers/crud_controller.dart +++ b/lib/controllers/crud_controller.dart @@ -84,9 +84,7 @@ class CRUDController extends BaseController { Future update(String id, dynamic obj) async { try { var uri = Uri.parse("$basePath$resourcePath/$id"); - final response = await httpClient - .put(uri, body: jsonEncode(obj), headers: headers) - .catchError(handleError); + final response = await httpClient.put(uri, body: jsonEncode(obj), headers: headers); if (response.statusCode == HttpStatus.ok) { final responseData = jsonDecode(response.body); return responseData['data']; diff --git a/lib/main.dart b/lib/main.dart index cc134ba..de10b77 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,4 @@ +import 'dart:io'; import 'package:flutter/material.dart'; import 'config/config.dart'; // import 'package:firebase_core/firebase_core.dart'; @@ -8,7 +9,9 @@ void main() async { // Remote Config // await Firebase.initializeApp(); // if used then add on firebase remote config // Google Mobile Ads Start - MobileAds.instance.initialize(); + if (Platform.isAndroid || Platform.isIOS) { + MobileAds.instance.initialize(); + } // Google Mobile Ads End runApp(const MainApp()); } diff --git a/lib/widgets/banner_ad_widget.dart b/lib/widgets/banner_ad_widget.dart index bd11afe..278f99d 100644 --- a/lib/widgets/banner_ad_widget.dart +++ b/lib/widgets/banner_ad_widget.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:flutter/material.dart'; import 'package:google_mobile_ads/google_mobile_ads.dart'; import 'package:moonlight/config/app_config.dart'; @@ -6,22 +8,39 @@ class BannerAdWidget extends StatefulWidget { const BannerAdWidget({Key? key}) : super(key: key); @override - _BannerAdWidgetState createState() => _BannerAdWidgetState(); + State createState() => _BannerAdWidgetState(); } class _BannerAdWidgetState extends State { + late BannerAd _bannerAd; + late String _adUnitId; + bool _isBannerAdReady = false; + Future _initGoogleMobileAds() { return MobileAds.instance.initialize(); } - late BannerAd _bannerAd; - bool _isBannerAdReady = false; - @override void initState() { + super.initState(); + if (Platform.isAndroid || Platform.isIOS) { + _adUnitId = AppConfig.bannerAdUnitId; + bannerAdLoad(); + } else { + _isBannerAdReady = false; + } + } + + @override + void dispose() { + _bannerAd.dispose(); + super.dispose(); + } + + bannerAdLoad() { _bannerAd = BannerAd( - adUnitId: AppConfig.bannerAdUnitId, - request: AdRequest(), + adUnitId: _adUnitId, + request: const AdRequest(), size: AdSize.banner, listener: BannerAdListener( onAdLoaded: (_) { @@ -30,25 +49,21 @@ class _BannerAdWidgetState extends State { }); }, onAdFailedToLoad: (ad, err) { - print('Failed to load a banner ad: ${err.message}'); + debugPrint('Failed to load a banner ad: ${err.message}'); _isBannerAdReady = false; ad.dispose(); }, ), ); - _bannerAd.load(); } - @override - void dispose() { - _bannerAd.dispose(); - super.dispose(); - } - @override Widget build(BuildContext context) { - return Container( + if (!_isBannerAdReady) { + return const SizedBox(); + } + return SizedBox( width: _bannerAd.size.width.toDouble(), height: _bannerAd.size.height.toDouble(), child: AdWidget(ad: _bannerAd),