ad helper

This commit is contained in:
Valentin CZERYBA 2025-02-18 22:56:50 +01:00
parent 9846c614a1
commit 5c2fa27aa7
8 changed files with 111 additions and 5 deletions

View File

@ -6,6 +6,9 @@
android:label="covas_mobile"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-4855855675386260~3438207239"/>
<activity
android:name=".MainActivity"
android:exported="true"

View File

@ -47,5 +47,8 @@
<true/>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your location is needed for showing nearby events</string>
<key>GADApplicationIdentifier</key>
<string>ca-app-pub-4855855675386260~3438207239</string>
</dict>
</plist>

View File

@ -0,0 +1,38 @@
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 {
await dotenv.load(fileName: ".env");
final adUnitId = dotenv.env['AD_UNIT_ID'] ?? '';
bannerAd = BannerAd(
adUnitId: adUnitId, // Replace with actual ID
size: AdSize.banner,
request: AdRequest(),
listener: BannerAdListener(
onAdLoaded: (ad) {
setStateCallback(() {
isBannerAdLoaded = true;
});
},
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;
}
}

View File

@ -1,8 +1,11 @@
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 'package:google_mobile_ads/google_mobile_ads.dart';
import 'dart:io';
//import 'MyHomePage.dart';
@ -14,7 +17,9 @@ import 'classes/alert.dart';
import 'variable/globals.dart' as globals;
import 'package:permission_handler/permission_handler.dart';
void main() {
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await MobileAds.instance.initialize();
runApp(MyApp());
}
@ -128,9 +133,17 @@ class _LoginDemoState extends State<LoginDemo> with ShowAlertDialog {
@override
void initState() {
super.initState();
AdHelper.loadBannerAd(setState);
_checkLocationPermission();
start();
super.initState();
}
@override
void dispose() {
AdHelper.disposeAd(); // Dispose of ad when the widget is removed
super.dispose();
}
Future<void> _checkLocationPermission() async {
@ -252,7 +265,13 @@ class _LoginDemoState extends State<LoginDemo> with ShowAlertDialog {
onTap: () {
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!),
),
],
),
),

View File

@ -125,7 +125,7 @@ class _EditProfileState extends State<EditSettings>
child: TextButton(
onPressed: () {},
child: Text(
'Mettre à jour les paramètres',
'Mettre à jour',
style: TextStyle(color: Colors.white, fontSize: 25),
),
),

View File

@ -10,6 +10,7 @@ import geolocator_apple
import path_provider_foundation
import shared_preferences_foundation
import url_launcher_macos
import webview_flutter_wkwebview
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
@ -17,4 +18,5 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
WebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "WebViewFlutterPlugin"))
}

View File

@ -304,6 +304,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.2.3"
google_mobile_ads:
dependency: "direct main"
description:
name: google_mobile_ads
sha256: "0d4a3744b5e8ed1b8be6a1b452d309f811688855a497c6113fc4400f922db603"
url: "https://pub.dev"
source: hosted
version: "5.3.1"
http:
dependency: "direct main"
description:
@ -877,6 +885,38 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
webview_flutter:
dependency: transitive
description:
name: webview_flutter
sha256: "889a0a678e7c793c308c68739996227c9661590605e70b1f6cf6b9a6634f7aec"
url: "https://pub.dev"
source: hosted
version: "4.10.0"
webview_flutter_android:
dependency: transitive
description:
name: webview_flutter_android
sha256: "512c26ccc5b8a571fd5d13ec994b7509f142ff6faf85835e243dde3538fdc713"
url: "https://pub.dev"
source: hosted
version: "4.3.2"
webview_flutter_platform_interface:
dependency: transitive
description:
name: webview_flutter_platform_interface
sha256: d937581d6e558908d7ae3dc1989c4f87b786891ab47bb9df7de548a151779d8d
url: "https://pub.dev"
source: hosted
version: "2.10.0"
webview_flutter_wkwebview:
dependency: transitive
description:
name: webview_flutter_wkwebview
sha256: "7310de7efa4e6df8b3d2ff14aef3f290bc00b43363f2d0028845e6de46507fc9"
url: "https://pub.dev"
source: hosted
version: "3.18.1"
xdg_directories:
dependency: transitive
description:

View File

@ -50,6 +50,7 @@ dependencies:
permission_handler: ^11.3.1
url_launcher: ^6.3.1
mapbox_gl: ^0.16.0
google_mobile_ads: ^5.3.1
dev_dependencies:
flutter_test: