// 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.");},
[[["容易理解","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-26 (世界標準時間)。"],[[["\u003cp\u003eBanner ads are rectangular image or text ads that occupy a spot within an app's layout, refreshing automatically at intervals.\u003c/p\u003e\n"],["\u003cp\u003eThis guide provides instructions and code samples for integrating AdMob banner ads into Flutter apps, including sizing and event handling.\u003c/p\u003e\n"],["\u003cp\u003eAlways test with the provided test ad unit IDs before publishing your app to avoid account suspension.\u003c/p\u003e\n"],["\u003cp\u003eStandard and adaptive banner sizes are available, and custom sizes can be defined using the \u003ccode\u003eAdSize\u003c/code\u003e class.\u003c/p\u003e\n"],["\u003cp\u003eWhen displaying a banner ad, use an \u003ccode\u003eAdWidget\u003c/code\u003e and ensure it is disposed of properly when no longer needed.\u003c/p\u003e\n"]]],[],null,["Select platform: [Android](/admob/android/banner \"View this page for the Android platform docs.\") [iOS](/admob/ios/banner \"View this page for the iOS platform docs.\") [Unity](/admob/unity/banner \"View this page for the Unity platform docs.\") [Flutter](/admob/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/admob/answer/9993556).\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](/admob/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\nAndroid \n\n ca-app-pub-3940256099942544/9214589741\n\niOS \n\n ca-app-pub-3940256099942544/2435281174\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/82c58b3202d8b9391e4d3ec39b045032e90cf331/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 BannerAd(\n adUnitId: \"\u003cvar translate=\"no\"\u003e_adUnitId\u003c/var\u003e\",\n request: const AdRequest(),\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/82c58b3202d8b9391e4d3ec39b045032e90cf331/samples/admob/banner_example/lib/main.dart#L138-L206\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 `BannerAdListener`, 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/82c58b3202d8b9391e4d3ec39b045032e90cf331/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 AdMob 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[Use automatic refresh for Banner ads](//support.google.com/admob/answer/3245199).\n\n| **Note:** When setting a refresh rate in the AdMob UI, the automatic refresh occurs only if the banner is visible on screen.\n\nDisplay a banner ad\n\nTo display a `BannerAd` 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 `BannerAd`\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/82c58b3202d8b9391e4d3ec39b045032e90cf331/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 `BannerAdListener.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\nComplete example on GitHub\n\nSee a full example of the banner integration covered in this page in\n[banner_example](//github.com/googleads/googleads-mobile-flutter/tree/main/samples/admob/banner_example).\n\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](/admob/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](/admob/flutter/banner/collapsible)."]]