-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add prototype of Google AdMob ads (#1760)
## Description This pull request adds Google AdMob for displaying ads in Sharezone. It's an experiment that we will run in the next weeks. Because we set the [`tagForUnderAgeOfConsent`](https://developers.google.com/admob/flutter/privacy/gdpr#tag_for_under_age_of_consent) to `true`, we don't need to show a consent dialog because only ["limited ads"](https://developers.google.com/admob/flutter/privacy/ad-serving-modes#limited_ads) will be displayed. Use the activation code "ads" to enable ads. ## Demo | Dashboard | Timetable | Homework | Info Screen | |--------|--------|-----|---------| | ![Screenshot_20241012-221743](https://github.com/user-attachments/assets/280dadcf-4272-4507-b99c-fe31a7093521) | ![Screenshot_20241012-222240](https://github.com/user-attachments/assets/744fba56-aa97-49e0-9c13-deaf5873dadf) | ![Screenshot_20241012-222232](https://github.com/user-attachments/assets/e812351b-22f9-4f0e-b682-9625d13e3d82) | ![Screenshot_20241013-184158](https://github.com/user-attachments/assets/6920ff44-0f2f-4b38-91f2-538625bf57b4) | Additionally, we show after every fifth homework check a full-screen ad: https://github.com/user-attachments/assets/62ca4b3f-c30a-4835-b10d-678fddf48f1a
- Loading branch information
1 parent
33b4813
commit 2a07153
Showing
37 changed files
with
2,000 additions
and
547 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) 2024 Sharezone UG (haftungsbeschränkt) | ||
// Licensed under the EUPL-1.2-or-later. | ||
// | ||
// You may obtain a copy of the Licence at: | ||
// https://joinup.ec.europa.eu/software/page/eupl | ||
// | ||
// SPDX-License-Identifier: EUPL-1.2 | ||
|
||
import 'package:flutter/widgets.dart'; | ||
import 'package:google_mobile_ads/google_mobile_ads.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:sharezone/ads/ads_controller.dart'; | ||
|
||
class AdBanner extends StatefulWidget { | ||
const AdBanner({ | ||
super.key, | ||
required this.adUnitId, | ||
}); | ||
|
||
final String adUnitId; | ||
|
||
@override | ||
State<AdBanner> createState() => _AdBannerState(); | ||
} | ||
|
||
class _AdBannerState extends State<AdBanner> { | ||
BannerAd? ad; | ||
bool _isLoaded = false; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
if (context.read<AdsController>().isQualifiedForAds()) { | ||
WidgetsBinding.instance.addPostFrameCallback((_) async { | ||
final size = | ||
await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize( | ||
MediaQuery.sizeOf(context).width.truncate()); | ||
|
||
if (size == null) { | ||
// Unable to get width of anchored banner. | ||
return; | ||
} | ||
|
||
if (!mounted) { | ||
return; | ||
} | ||
|
||
ad = BannerAd( | ||
adUnitId: widget.adUnitId, | ||
request: context.read<AdsController>().createAdRequest(), | ||
size: size, | ||
listener: BannerAdListener( | ||
onAdLoaded: (ad) { | ||
debugPrint('$ad loaded.'); | ||
setState(() { | ||
_isLoaded = true; | ||
}); | ||
}, | ||
onAdFailedToLoad: (ad, err) { | ||
debugPrint('BannerAd failed to load: $err'); | ||
ad.dispose(); | ||
}, | ||
), | ||
)..load(); | ||
}); | ||
} | ||
} | ||
|
||
@override | ||
void dispose() { | ||
ad?.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final areAdsVisible = context.watch<AdsController>().areAdsVisible; | ||
if (!areAdsVisible || _isLoaded == false) return Container(); | ||
return SizedBox( | ||
height: ad!.size.height.toDouble(), | ||
width: ad!.size.width.toDouble(), | ||
child: AdWidget(ad: ad!), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2024 Sharezone UG (haftungsbeschränkt) | ||
// Licensed under the EUPL-1.2-or-later. | ||
// | ||
// You may obtain a copy of the Licence at: | ||
// https://joinup.ec.europa.eu/software/page/eupl | ||
// | ||
// SPDX-License-Identifier: EUPL-1.2 | ||
|
||
import 'package:flutter/gestures.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:sharezone/sharezone_plus/page/sharezone_plus_page.dart'; | ||
import 'package:sharezone_widgets/sharezone_widgets.dart'; | ||
|
||
void showAdInfoDialog(BuildContext context) async { | ||
final navigateToPlusPage = await showDialog<bool>( | ||
context: context, | ||
builder: (BuildContext context) { | ||
return AlertDialog( | ||
title: const Text('Werbung in Sharezone'), | ||
content: Text.rich( | ||
TextSpan( | ||
children: [ | ||
const TextSpan( | ||
text: | ||
'Innerhalb der nächsten Wochen führen wir ein Experiment mit Werbung in Sharezone durch. Wenn du keine Werbung sehen möchten, kannst du ', | ||
), | ||
TextSpan( | ||
text: 'Sharezone Plus', | ||
style: const TextStyle( | ||
color: primaryColor, | ||
decoration: TextDecoration.underline, | ||
), | ||
recognizer: TapGestureRecognizer() | ||
..onTap = () => Navigator.of(context).pop(true), | ||
), | ||
const TextSpan( | ||
text: ' erwerben.', | ||
), | ||
], | ||
), | ||
), | ||
actions: <Widget>[ | ||
ElevatedButton( | ||
style: ElevatedButton.styleFrom(foregroundColor: Colors.white), | ||
child: const Text('OK'), | ||
onPressed: () => Navigator.of(context).pop(), | ||
), | ||
], | ||
); | ||
}, | ||
); | ||
|
||
if (navigateToPlusPage == true && context.mounted) { | ||
navigateToSharezonePlusPage(context); | ||
} | ||
} |
Oops, something went wrong.