Skip to content

Commit

Permalink
changes BannerAdWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
bhargavraviya committed Sep 30, 2023
1 parent a07413e commit da21720
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
4 changes: 1 addition & 3 deletions lib/controllers/crud_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ class CRUDController extends BaseController {
Future<dynamic> 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'];
Expand Down
5 changes: 4 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'config/config.dart';
// import 'package:firebase_core/firebase_core.dart';
Expand All @@ -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());
}
Expand Down
45 changes: 30 additions & 15 deletions lib/widgets/banner_ad_widget.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -6,22 +8,39 @@ class BannerAdWidget extends StatefulWidget {
const BannerAdWidget({Key? key}) : super(key: key);

@override
_BannerAdWidgetState createState() => _BannerAdWidgetState();
State<BannerAdWidget> createState() => _BannerAdWidgetState();
}

class _BannerAdWidgetState extends State<BannerAdWidget> {
late BannerAd _bannerAd;
late String _adUnitId;
bool _isBannerAdReady = false;

Future<InitializationStatus> _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: (_) {
Expand All @@ -30,25 +49,21 @@ class _BannerAdWidgetState extends State<BannerAdWidget> {
});
},
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),
Expand Down

0 comments on commit da21720

Please sign in to comment.