// Get an AnchoredAdaptiveBannerAdSize before loading the ad.finalsize=awaitAdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(MediaQuery.sizeOf(context).width.truncate(),);
void_loadAd()async{// Get an AnchoredAdaptiveBannerAdSize before loading the ad.finalsize=awaitAdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(MediaQuery.sizeOf(context).width.truncate(),);if(size==null){// Unable to get width of anchored banner.return;}BannerAd(adUnitId:_adUnitId,request:constAdRequest(),size:size,listener:BannerAdListener(onAdLoaded:(ad){// Called when an ad is successfully received.debugPrint("Ad was loaded.");setState((){_bannerAd=adasBannerAd;});},onAdFailedToLoad:(ad,err){// Called when an ad request failed.debugPrint("Ad failed to load with error: $err");ad.dispose();},),).load();}
onAdOpened:(Adad){// Called when an ad opens an overlay that covers the screen.debugPrint("Ad was opened.");},onAdClosed:(Adad){// Called when an ad removes an overlay that covers the screen.debugPrint("Ad was closed.");},onAdImpression:(Adad){// Called when an impression occurs on the ad.debugPrint("Ad recorded an impression.");},onAdClicked:(Adad){// Called when an a click event occurs on the ad.debugPrint("Ad was clicked.");},onAdWillDismissScreen:(Adad){// iOS only. Called before dismissing a full screen view.debugPrint("Ad will be dismissed.");},
如果您的廣告單元已設為會自動重新整理,當廣告載入失敗,就無需再次發送請求。Google Mobile Ads SDK 會依照您在 Ad Manager 使用者介面指定的重新整理頻率執行。若未啟用重新整理功能,則需重新發送請求。如要進一步瞭解廣告單元的重新整理功能,例如怎麼設定重新整理頻率,請參閱「行動應用程式中廣告的重新整理頻率」。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-25 (世界標準時間)。"],[[["\u003cp\u003eBanner ads are integrated into Flutter apps using the Ad Manager package and are displayed using the \u003ccode\u003eAdWidget\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eIt is crucial to use test ads during development and replace them with real ad unit IDs before publishing to avoid account suspension.\u003c/p\u003e\n"],["\u003cp\u003eBanner ads can be customized with standard or custom sizes, but ensure the width does not exceed the device's screen width.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eAdManagerBannerAdListener\u003c/code\u003e allows monitoring ad lifecycle events like loading, opening, closing, and impressions for better control.\u003c/p\u003e\n"],["\u003cp\u003eFor optimal performance on Android, fixed position banners are recommended, and inline banners within scroll views should be used on Android 10 or later.\u003c/p\u003e\n"]]],[],null,["Select platform: [Android](/ad-manager/mobile-ads-sdk/android/banner \"View this page for the Android platform docs.\") [iOS](/ad-manager/mobile-ads-sdk/ios/banner \"View this page for the iOS platform docs.\") [Unity](/ad-manager/mobile-ads-sdk/unity/banner \"View this page for the Unity platform docs.\") [Flutter](/ad-manager/mobile-ads-sdk/flutter/banner \"View this page for the Flutter platform docs.\")\n\n\u003cbr /\u003e\n\nBanner ads occupy a spot within an app's layout, either at the top or bottom of\nthe device screen. They stay on screen while users are interacting with the app,\nand can refresh automatically after a certain period of time.\n\nThis guide gets you started with\n\n[anchored adaptive banner ads](//support.google.com/admanager/answer/10017110#Adaptive).\n\nAnchored adaptive banners optimizes the ad size for each device using an ad\nwidth you specify.\n\nAnchored adaptive banner ads are fixed aspect ratio ads rather than\n[fixed size](/ad-manager/mobile-ads-sdk/flutter/banner/fixed-size) ads. The aspect ratio is similar to\n320x50. Once you specify the full width available, Google Mobile Ads SDK\nreturns an ad with optimal height for that width. The optimal height for the ad\nremains constant across different ad requests, and the content surrounding the\nad stays in place when the ad refreshes.\n\nAlways test with test ads\n\nWhen building and testing your apps, make sure you use test ads rather than\nlive, production ads. Failure to do so can lead to suspension of your account.\n\nThe easiest way to load test ads is to use our dedicated test ad unit ID for\nbanners:\n\n`/21775744923/example/adaptive-banner`\n\nThe test ad units are configured to return test ads for every request, and\nyou're free to use them in your own apps while coding, testing, and debugging.\nJust make sure you replace them with your own ad unit IDs before publishing your\napp.\n\nGet the ad size\n\nTo request a banner ad with the correct ad size, follow these steps:\n\n1. Get the width of the device's screen in density-independent pixels (dp) using\n `MediaQuery.of(context)`. If you don't want to use the full screen width, you\n can set your own width.\n\n2. Use the appropriate static method on the `AdSize` class to get an `AdSize`\n object. For example, use\n `AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(int width)` to get\n the ad size for the current orientation.\n\n // Get an AnchoredAdaptiveBannerAdSize before loading the ad.\n final size = await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(\n MediaQuery.sizeOf(context).width.truncate(),\n ); \n https://github.com/googleads/googleads-mobile-flutter/blob/9e63c9ad1459a7bbeeb9465ab3c6e25309f54683/samples/admob/banner_example/lib/main.dart#L152-L155\n\nLoad an ad\n\nThe following example loads a banner ad: \n\n void _loadAd() async {\n // Get an AnchoredAdaptiveBannerAdSize before loading the ad.\n final size = await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(\n MediaQuery.sizeOf(context).width.truncate(),\n );\n\n if (size == null) {\n // Unable to get width of anchored banner.\n return;\n }\n\n unawaited(BannerAd(\n adUnitId: \"\u003cvar translate=\"no\"\u003e_adUnitId\u003c/var\u003e\",\n request: const AdManagerAdRequest(),\n size: size,\n listener: BannerAdListener(\n onAdLoaded: (ad) {\n // Called when an ad is successfully received.\n debugPrint('Ad was loaded.');\n setState(() {\n _bannerAd = ad as BannerAd;\n });\n },\n onAdFailedToLoad: (ad, err) {\n // Called when an ad request failed.\n debugPrint('Ad failed to load with error: $err');\n ad.dispose();\n },\n ),\n ).load());\n } \n https://github.com/googleads/googleads-mobile-flutter/blob/9e63c9ad1459a7bbeeb9465ab3c6e25309f54683/packages/google_mobile_ads/example/lib/snippets/banner_ad_snippets.dart#L39-L69\n\nReplace \u003cvar translate=\"no\"\u003e_adUnitId\u003c/var\u003e with your own ad unit ID.\n\nBanner ad events\n\nThrough the use of `AdManagerBannerAdListener`, you can listen for lifecycle events, such\nas when an ad is loaded. This example implements each method and logs a message\nto the console: \n\n onAdOpened: (Ad ad) {\n // Called when an ad opens an overlay that covers the screen.\n debugPrint(\"Ad was opened.\");\n },\n onAdClosed: (Ad ad) {\n // Called when an ad removes an overlay that covers the screen.\n debugPrint(\"Ad was closed.\");\n },\n onAdImpression: (Ad ad) {\n // Called when an impression occurs on the ad.\n debugPrint(\"Ad recorded an impression.\");\n },\n onAdClicked: (Ad ad) {\n // Called when an a click event occurs on the ad.\n debugPrint(\"Ad was clicked.\");\n },\n onAdWillDismissScreen: (Ad ad) {\n // iOS only. Called before dismissing a full screen view.\n debugPrint(\"Ad will be dismissed.\");\n }, \n https://github.com/googleads/googleads-mobile-flutter/blob/9e63c9ad1459a7bbeeb9465ab3c6e25309f54683/samples/admob/banner_example/lib/main.dart#L182-L201\n\nRefresh an ad\n\nIf you configured your ad unit to refresh, you don't need to request another ad\nwhen the ad fails to load. Google Mobile Ads SDK respects any refresh rate\nyou specified in the Ad Manager UI. If you haven't enabled\nrefresh, issue a new request. For more details on ad unit refresh, such as\nsetting a refresh rate, see\n\n[Refresh rate for ads in mobile apps](//support.google.com/admanager/answer/6022114).\n\n| **Note:** When setting a refresh rate in the Ad Manager UI, the automatic refresh occurs only if the banner is visible on screen.\n\nDisplay a banner ad\n\nTo display a `AdManagerBannerAd` as a widget, you must instantiate an `AdWidget`\nwith a supported ad after calling `load()`. You can create the widget before\ncalling `load()`, but `load()` must be called before adding it to the widget\ntree.\n\n`AdWidget` inherits from Flutter's Widget class and can be used like any other\nwidget. On iOS, make sure you place the widget in a widget with a specified\nwidth and height. Otherwise, your ad may not be displayed. A `AdManagerBannerAd`\ncan be placed in a container with a size that matches the ad: \n\n if (_bannerAd != null)\n Align(\n alignment: Alignment.bottomCenter,\n child: SafeArea(\n child: SizedBox(\n width: _bannerAd!.size.width.toDouble(),\n height: _bannerAd!.size.height.toDouble(),\n child: AdWidget(ad: _bannerAd!),\n ),\n ),\n ), \n https://github.com/googleads/googleads-mobile-flutter/blob/9e63c9ad1459a7bbeeb9465ab3c6e25309f54683/samples/admob/banner_example/lib/main.dart#L79-L89\n\nAn ad must be disposed when access to it is no longer needed. The best practice\nfor when to call `dispose()` is either after the `AdWidget` is removed from the\nwidget tree or in the `AdManagerBannerAdListener.onAdFailedToLoad()`\ncallback.\n\nThat's it! Your app is now ready to display banner ads.\n\nScrolling limitation on Android 9 and below\n\nWe are aware that some older or less powerful devices running Android 9 or\nearlier may have [suboptimal performance](https://github.com/flutter/flutter/wiki/Hybrid-Composition#performance)\nwhen displaying inline banner ads within scrolling views. We recommend only\nusing these types of banners on Android 10 or later. Fixed position banners such\nas anchored banners are not affected and can be used with optimal performance on\nall Android API levels.\n\nLearn about other banner types\n\nFamiliarize yourself with other types of banners defined in this section for\nyour Flutter application.\n\nInline adaptive banners\n\nInline adaptive banners have variable height and are larger, taller banners\ncompared to anchored adaptive banners. Inline adaptive banners are recommended\nover anchored adaptive banner ads for apps that place banner ads in scrollable\ncontent. For more details, see [inline adaptive banners](/ad-manager/mobile-ads-sdk/flutter/banner/inline-adaptive).\n\nCollapsible banners\n\nCollapsible banner ads are banner ads that are initially presented as a larger\noverlay, with a button to collapse the ad to a smaller size. Consider using this\nbanner to further optimize your performance. For more details, see\n[collapsible banner ads](/ad-manager/mobile-ads-sdk/flutter/banner/collapsible)."]]