स्क्रोल करने वाले बैनर के लिए, इनलाइन अडैप्टिव बैनर का इस्तेमाल करना

प्लैटफ़ॉर्म चुनें: Android iOS Flutter Android (लेगसी)

अडैप्टिव बैनर, रिस्पॉन्सिव विज्ञापनों की अगली जनरेशन हैं. ये हर डिवाइस के लिए विज्ञापन के साइज़ को ऑप्टिमाइज़ करके, परफ़ॉर्मेंस को बेहतर बनाते हैं. तय साइज़ वाले बैनर, सिर्फ़ तय ऊंचाई वाले बैनर को सपोर्ट करते थे. हालांकि, अडैप्टिव बैनर की मदद से, डेवलपर विज्ञापन की चौड़ाई तय कर सकते हैं. साथ ही, इस चौड़ाई के हिसाब से विज्ञापन का सही साइज़ तय किया जा सकता है.

सबसे अच्छा विज्ञापन का साइज़ चुनने के लिए, इनलाइन अडैप्टिव बैनर, तय ऊंचाई की बजाय डिवाइस की स्क्रीन की पूरी ऊंचाई का इस्तेमाल करते हैं. इससे परफ़ॉर्मेंस को बेहतर बनाने के मौके मिलते हैं.

इनलाइन अडैप्टिव बैनर का इस्तेमाल कब करें

ऐंकर किए गए अडैप्टिव बैनर की तुलना में, इनलाइन अडैप्टिव बैनर बड़े और लंबे होते हैं. इनकी ऊंचाई कम या ज़्यादा की जा सकती है. अपनी इस विशेषता की वजह से इनलाइन अडैप्टिव बैनर, डिवाइस की स्क्रीन जितने लंबे भी हो सकते हैं.

इन्हें स्क्रोल किए जा सकने वाले कॉन्टेंट में दिखाने के लिए डिज़ाइन किया गया है. उदाहरण के लिए:

ज़रूरी शर्तें

आगे बढ़ने से पहले, सेट अप करें Google Mobile Ads Flutter Plugin.

शुरू करने से पहले

अपने ऐप्लिकेशन में अडैप्टिव बैनर लागू करते समय, इन बातों का ध्यान रखें:

  • पक्का करें कि आपने Google Mobile Ads Flutter Plugin का सबसे नया वर्शन इस्तेमाल किया हो. साथ ही, अगर AdMob मीडिएशन का इस्तेमाल किया जा रहा है, तो मीडिएशन अडैप्टर के सबसे नए वर्शन इस्तेमाल करें.

  • इनलाइन अडैप्टिव बैनर के साइज़, उपलब्ध पूरी चौड़ाई का इस्तेमाल करने पर सबसे अच्छे दिखते हैं. ज़्यादातर मामलों में, यह उस डिवाइस की स्क्रीन की पूरी चौड़ाई होती है जिस पर बैनर दिखाया जा रहा है. लागू होने वाले सेफ़ एरिया को ध्यान में रखें.

  • विज्ञापन का साइज़ पाने के लिए, इन तरीकों का इस्तेमाल किया जा सकता है

    • AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(int width)
    • AdSize.getLandscapeInlineAdaptiveBannerAdSize(int width)
    • AdSize.getPortraitInlineAdaptiveBannerAdSize(int width)
    • AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight)
  • इनलाइन अडैप्टिव बैनर एपीआई का इस्तेमाल करने पर, Google Mobile Ads Flutter Plugin दी गई चौड़ाई और इनलाइन फ़्लैग वाला AdSize दिखाता है. आपके इस्तेमाल किए जा रहे एपीआई के आधार पर, ऊंचाई या तो शून्य होती है या maxHeight होती है. विज्ञापन की असल ऊंचाई, उसके दिखाए जाने पर उपलब्ध होती है.

  • इनलाइन अडैप्टिव बैनर को स्क्रोल किए जा सकने वाले कॉन्टेंट में दिखाने के लिए डिज़ाइन किया गया है. एपीआई के आधार पर, बैनर डिवाइस की स्क्रीन जितना लंबा हो सकता है या उसकी ऊंचाई तय की जा सकती है.

लागू करना

कोई सामान्य इनलाइन अडैप्टिव बैनर लागू करने के लिए, यहां दिया गया तरीका अपनाएं.

  1. इनलाइन अडैप्टिव बैनर का साइज़ पाएं. आपको जो साइज़ मिलेगा उसका इस्तेमाल, अडैप्टिव बैनर का अनुरोध करने के लिए किया जाएगा. अडैप्टिव विज्ञापन का साइज़ पाने के लिए, पक्का करें कि:
    1. डेंसिटी इंडिपेंडेंट पिक्सल में, उस डिवाइस की चौड़ाई पाएं जिस पर बैनर दिखाना है. अगर आपको स्क्रीन की पूरी चौड़ाई में फ़िट होने वाला बैनर नहीं चाहिए, तो अपनी ज़रूरत के हिसाब से चौड़ाई डालें. स्क्रीन की चौड़ाई पाने के लिए, MediaQuery.of(context) का इस्तेमाल किया जा सकता है.
    2. मौजूदा ओरिएंटेशन के लिए, इनलाइन अडैप्टिव AdSize ऑब्जेक्ट पाने के लिए, ऐड साइज़ क्लास पर सही स्टैटिक मेथड इस्तेमाल करें. जैसे, AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(int width)
    3. अगर आपको बैनर की ऊंचाई तय करनी है, तो स्टैटिक मेथड AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight) का इस्तेमाल किया जा सकता है.
  2. अपने विज्ञापन यूनिट आईडी, अडैप्टिव विज्ञापन के साइज़, और विज्ञापन के अनुरोध वाले ऑब्जेक्ट की मदद से, BannerAd ऑब्जेक्ट बनाएं.
  3. विज्ञापन लोड करें.
  4. onAdLoaded कॉलबैक में, अपडेट किया गया प्लैटफ़ॉर्म विज्ञापन का साइज़ पाने के लिए, BannerAd.getPlatformAdSize() का इस्तेमाल करें. इसके बाद, AdWidget कंटेनर की ऊंचाई अपडेट करें.

कोड का उदाहरण

यहां एक उदाहरण दिया गया है. इसमें एक ऐसा विजेट दिखाया गया है जो स्क्रीन की चौड़ाई के हिसाब से, इनसेट को ध्यान में रखते हुए, इनलाइन अडैप्टिव बैनर लोड करता है:

import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

/// This example demonstrates inline adaptive banner ads.
///
/// Loads and shows an inline adaptive banner ad in a scrolling view,
/// and reloads the ad when the orientation changes.
class InlineAdaptiveExample extends StatefulWidget {
  @override
  _InlineAdaptiveExampleState createState() => _InlineAdaptiveExampleState();
}

class _InlineAdaptiveExampleState extends State<InlineAdaptiveExample> {
  static const _insets = 16.0;
  BannerAd? _inlineAdaptiveAd;
  bool _isLoaded = false;
  AdSize? _adSize;
  late Orientation _currentOrientation;

  double get _adWidth => MediaQuery.of(context).size.width - (2 * _insets);

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _currentOrientation = MediaQuery.of(context).orientation;
    _loadAd();
  }

  void _loadAd() async {
    await _inlineAdaptiveAd?.dispose();
    setState(() {
      _inlineAdaptiveAd = null;
      _isLoaded = false;
    });

    // Get an inline adaptive size for the current orientation.
    AdSize size = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(
        _adWidth.truncate());

    _inlineAdaptiveAd = BannerAd(
      // TODO: replace this test ad unit with your own ad unit.
      adUnitId: 'ca-app-pub-3940256099942544/9214589741',
      size: size,
      request: AdRequest(),
      listener: BannerAdListener(
        onAdLoaded: (Ad ad) async {
          print('Inline adaptive banner loaded: ${ad.responseInfo}');

          // After the ad is loaded, get the platform ad size and use it to
          // update the height of the container. This is necessary because the
          // height can change after the ad is loaded.
          BannerAd bannerAd = (ad as BannerAd);
          final AdSize? size = await bannerAd.getPlatformAdSize();
          if (size == null) {
            print('Error: getPlatformAdSize() returned null for $bannerAd');
            return;
          }

          setState(() {
            _inlineAdaptiveAd = bannerAd;
            _isLoaded = true;
            _adSize = size;
          });
        },
        onAdFailedToLoad: (Ad ad, LoadAdError error) {
          print('Inline adaptive banner failedToLoad: $error');
          ad.dispose();
        },
      ),
    );
    await _inlineAdaptiveAd!.load();
  }

  /// Gets a widget containing the ad, if one is loaded.
  ///
  /// Returns an empty container if no ad is loaded, or the orientation
  /// has changed. Also loads a new ad if the orientation changes.
  Widget _getAdWidget() {
    return OrientationBuilder(
      builder: (context, orientation) {
        if (_currentOrientation == orientation &&
            _inlineAdaptiveAd != null &&
            _isLoaded &&
            _adSize != null) {
          return Align(
              child: Container(
            width: _adWidth,
            height: _adSize!.height.toDouble(),
            child: AdWidget(
              ad: _inlineAdaptiveAd!,
            ),
          ));
        }
        // Reload the ad if the orientation changes.
        if (_currentOrientation != orientation) {
          _currentOrientation = orientation;
          _loadAd();
        }
        return Container();
      },
    );
  }

  @override
  Widget build(BuildContext context) => Scaffold(
      appBar: AppBar(
        title: Text('Inline adaptive banner example'),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: _insets),
          child: ListView.separated(
            itemCount: 20,
            separatorBuilder: (BuildContext context, int index) {
              return Container(
                height: 40,
              );
            },
            itemBuilder: (BuildContext context, int index) {
              if (index == 10) {
                return _getAdWidget();
              }
              return Text(
                'Placeholder text',
                style: TextStyle(fontSize: 24),
              );
            },
          ),
        ),
      ));

  @override
  void dispose() {
    super.dispose();
    _inlineAdaptiveAd?.dispose();
  }
}