feature/publicite #37
@ -2,37 +2,26 @@ import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
|
||||
class AdHelper {
|
||||
static BannerAd? bannerAd;
|
||||
static bool isBannerAdLoaded = false;
|
||||
|
||||
static Future<void> loadBannerAd(Function setStateCallback) async {
|
||||
static Future<BannerAd> createBannerAd(Function setStateCallback) async {
|
||||
await dotenv.load(fileName: ".env");
|
||||
final adUnitId = dotenv.env['AD_UNIT_ID'] ?? '';
|
||||
|
||||
bannerAd = BannerAd(
|
||||
adUnitId: adUnitId, // Replace with actual ID
|
||||
BannerAd bannerAd = BannerAd(
|
||||
adUnitId: adUnitId,
|
||||
size: AdSize.banner,
|
||||
request: AdRequest(),
|
||||
listener: BannerAdListener(
|
||||
onAdLoaded: (ad) {
|
||||
setStateCallback(() {
|
||||
isBannerAdLoaded = true;
|
||||
});
|
||||
setStateCallback(() {});
|
||||
},
|
||||
onAdFailedToLoad: (ad, error) {
|
||||
print('Banner Ad failed to load: $error');
|
||||
isBannerAdLoaded = false;
|
||||
ad.dispose();
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
bannerAd?.load();
|
||||
}
|
||||
|
||||
static void disposeAd() {
|
||||
bannerAd?.dispose();
|
||||
bannerAd = null;
|
||||
isBannerAdLoaded = false;
|
||||
bannerAd.load();
|
||||
return bannerAd;
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'classes/ad_helper.dart';
|
||||
import 'dart:convert';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
|
||||
import 'classes/ad_helper.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
|
||||
import 'dart:io';
|
||||
@ -39,6 +39,7 @@ class LoginDemo extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _LoginDemoState extends State<LoginDemo> with ShowAlertDialog {
|
||||
BannerAd? _bannerAd;
|
||||
TextEditingController inputPseudo = TextEditingController();
|
||||
TextEditingController inputPassword = TextEditingController();
|
||||
Future<void> _login(BuildContext context) async {
|
||||
@ -134,18 +135,16 @@ class _LoginDemoState extends State<LoginDemo> with ShowAlertDialog {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
AdHelper.loadBannerAd(setState);
|
||||
AdHelper.createBannerAd(() => setState(() {})).then((ad) {
|
||||
setState(() {
|
||||
_bannerAd = ad;
|
||||
});
|
||||
});
|
||||
|
||||
_checkLocationPermission();
|
||||
start();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
AdHelper.disposeAd(); // Dispose of ad when the widget is removed
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _checkLocationPermission() async {
|
||||
PermissionStatus status = await Permission.location.status;
|
||||
|
||||
@ -196,6 +195,13 @@ class _LoginDemoState extends State<LoginDemo> with ShowAlertDialog {
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
_bannerAd == null
|
||||
? SizedBox.shrink()
|
||||
: SizedBox(
|
||||
height: _bannerAd!.size.height.toDouble(),
|
||||
width: _bannerAd!.size.width.toDouble(),
|
||||
child: AdWidget(ad: _bannerAd!),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 60.0),
|
||||
child: Center(
|
||||
@ -266,12 +272,6 @@ class _LoginDemoState extends State<LoginDemo> with ShowAlertDialog {
|
||||
Navigator.push(
|
||||
context, MaterialPageRoute(builder: (_) => AddProfile()));
|
||||
}),
|
||||
if (AdHelper.isBannerAdLoaded)
|
||||
SizedBox(
|
||||
height: AdHelper.bannerAd!.size.height.toDouble(),
|
||||
width: AdHelper.bannerAd!.size.width.toDouble(),
|
||||
child: AdWidget(ad: AdHelper.bannerAd!),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
@ -16,7 +16,12 @@ import 'package:permission_handler/permission_handler.dart';
|
||||
import "Camera.dart";
|
||||
import 'package:camera/camera.dart';
|
||||
|
||||
void main() {
|
||||
import '../classes/ad_helper.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await MobileAds.instance.initialize();
|
||||
initializeDateFormatting("fr_FR", null).then((_) => runApp(const MyApp()));
|
||||
}
|
||||
|
||||
@ -40,6 +45,8 @@ class ListItemMenu extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<ListItemMenu> {
|
||||
BannerAd? _bannerAd;
|
||||
|
||||
Future<List<Events>> postsFuture = getPosts();
|
||||
List<Events> filteredPosts = [];
|
||||
String geographicalZone = '';
|
||||
@ -138,6 +145,11 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
AdHelper.createBannerAd(() => setState(() {})).then((ad) {
|
||||
setState(() {
|
||||
_bannerAd = ad;
|
||||
});
|
||||
});
|
||||
// Initialize data fetch when the page loads
|
||||
_getCurrentLocation();
|
||||
}
|
||||
@ -543,6 +555,13 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
drawer: MyDrawer(),
|
||||
body: Column(
|
||||
children: [
|
||||
_bannerAd == null
|
||||
? SizedBox.shrink()
|
||||
: SizedBox(
|
||||
height: _bannerAd!.size.height.toDouble(),
|
||||
width: _bannerAd!.size.width.toDouble(),
|
||||
child: AdWidget(ad: _bannerAd!),
|
||||
),
|
||||
if (showInputSearch)
|
||||
_buildSearchField(
|
||||
controller: inputItem,
|
||||
|
Loading…
x
Reference in New Issue
Block a user