flutter_flow_ad_banner.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import 'dart:io';
  2. import 'package:flutter/foundation.dart' show kIsWeb;
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/scheduler.dart';
  5. import 'package:google_mobile_ads/google_mobile_ads.dart';
  6. class FlutterFlowAdBanner extends StatefulWidget {
  7. const FlutterFlowAdBanner({
  8. Key? key,
  9. this.width,
  10. this.height,
  11. required this.showsTestAd,
  12. this.iOSAdUnitID,
  13. this.androidAdUnitID,
  14. }) : super(key: key);
  15. final double? width;
  16. final double? height;
  17. final bool showsTestAd;
  18. final String? iOSAdUnitID;
  19. final String? androidAdUnitID;
  20. @override
  21. _FlutterFlowAdBannerState createState() => _FlutterFlowAdBannerState();
  22. }
  23. class _FlutterFlowAdBannerState extends State<FlutterFlowAdBanner> {
  24. static const AdRequest request = AdRequest();
  25. BannerAd? _anchoredBanner;
  26. AdWidget? adWidget;
  27. @override
  28. void initState() {
  29. super.initState();
  30. SchedulerBinding.instance.addPostFrameCallback((_) {
  31. _createAnchoredBanner(context);
  32. });
  33. }
  34. @override
  35. void dispose() {
  36. super.dispose();
  37. _anchoredBanner?.dispose();
  38. }
  39. @override
  40. Widget build(BuildContext context) {
  41. var loadingText = 'Ad Loading... \n\n';
  42. if (widget.showsTestAd) {
  43. loadingText +=
  44. 'If this takes a long time, you may have to check whether the ad is '
  45. 'being covered from a parent widget. For example, a larger width than '
  46. 'the device screen size or a large border radius encompassing the ad banner '
  47. 'may stop ads from loading.\n\n'
  48. 'If a full-width banner is desired for your app, leave the width and '
  49. 'height of the AdBanner widget empty. AdBanner will automatically'
  50. 'match the size of the banner to the device screen.';
  51. }
  52. return _anchoredBanner != null && adWidget != null
  53. ? Container(
  54. alignment: Alignment.center,
  55. color: Colors.red,
  56. width: _anchoredBanner!.size.width.toDouble(),
  57. height: _anchoredBanner!.size.height.toDouble(),
  58. child: adWidget,
  59. )
  60. : Container(
  61. color: Colors.black,
  62. alignment: Alignment.center,
  63. child: Padding(
  64. padding: const EdgeInsets.all(8.0),
  65. child: Text(
  66. loadingText,
  67. style: const TextStyle(
  68. fontSize: 10.0,
  69. color: Colors.white,
  70. ),
  71. ),
  72. ),
  73. );
  74. }
  75. Future _createAnchoredBanner(BuildContext context) async {
  76. final AdSize? size = widget.width != null && widget.height != null
  77. ? AdSize(
  78. height: widget.height!.toInt(),
  79. width: widget.width!.toInt(),
  80. )
  81. : await AdSize.getAnchoredAdaptiveBannerAdSize(
  82. widget.width == null ? Orientation.portrait : Orientation.landscape,
  83. widget.width == null
  84. ? MediaQuery.sizeOf(context).width.truncate()
  85. : MediaQuery.sizeOf(context).height.truncate(),
  86. );
  87. if (size == null) {
  88. print('Unable to get size of anchored banner.');
  89. return;
  90. }
  91. final isAndroid = !kIsWeb && Platform.isAndroid;
  92. final BannerAd banner = BannerAd(
  93. size: size,
  94. request: request,
  95. adUnitId: widget.showsTestAd
  96. ? isAndroid
  97. ? 'ca-app-pub-3940256099942544/6300978111'
  98. : 'ca-app-pub-3940256099942544/2934735716'
  99. : isAndroid
  100. ? widget.androidAdUnitID!
  101. : widget.iOSAdUnitID!,
  102. listener: BannerAdListener(
  103. onAdLoaded: (ad) {
  104. print('$BannerAd loaded.');
  105. if (mounted) {
  106. setState(() => _anchoredBanner = ad as BannerAd);
  107. }
  108. },
  109. onAdFailedToLoad: (ad, error) {
  110. print('$BannerAd failedToLoad: $error');
  111. ad.dispose();
  112. },
  113. onAdOpened: (Ad ad) => print('$BannerAd onAdOpened.'),
  114. onAdClosed: (Ad ad) => print('$BannerAd onAdClosed.'),
  115. ),
  116. );
  117. await banner.load();
  118. adWidget = AdWidget(ad: banner);
  119. if (mounted) {
  120. setState(() {});
  121. }
  122. return;
  123. }
  124. }