본문 바로가기
Flutter & Dart

Flutter & Dart 구글 애드몹 광고 추가하기 2. 개발하기

by Sudarii 2024. 1. 25.
728x90

https://dongkyu.tistory.com/44

 

Flutter & Dart 구글 애드몹 광고 추가하기 1. Google AdMob 설정

https://admob.google.com/intl/ko/home/ Google AdMob: 모바일 앱 수익 창출 인앱 광고를 사용하여 모바일 앱에서 더 많은 수익을 창출하고, 사용이 간편한 도구를 통해 유용한 분석 정보를 얻고 앱을 성장시켜

dongkyu.tistory.com

 

어제에 이어 구글애드몹에대해 작성 하겠다.

 

1. 패키지 설치

https://pub.dev/packages/google_mobile_ads/install

 

google_mobile_ads | Flutter Package

Flutter plugin for Google Mobile Ads, supporting banner, interstitial (full-screen), rewarded and native ads

pub.dev

 

google_mobile_ads 패키지를 설치한다. 

 

flutter pub add google_mobile_ads
dependencies:
  google_mobile_ads: ^4.0.0

 

두가지 방법중 선택해서 설치 하면 된다.

 

2. 광고 넣기

  static final BannerAd _Banner = BannerAd(      
      adUnitId: 'ca-app-pub-3940256099942544/6300978111', //안드 테스트 광고id
      size: AdSize.banner,
      request: const AdRequest(),
      listener: BannerAdListener(
        // Called when an ad is successfully received.
        onAdLoaded: (Ad ad) => print('Ad loaded.'),
        // Called when an ad request failed.
        onAdFailedToLoad: (Ad ad, LoadAdError error) {
          // Dispose the ad here to free resources.
          ad.dispose();
          print('Ad failed to load: $error');
        },
        // Called when an ad opens an overlay that covers the screen.
        onAdOpened: (Ad ad) => print('Ad opened.'),
        // Called when an ad removes an overlay that covers the screen.
        onAdClosed: (Ad ad) => print('Ad closed.'),
        // Called when an impression occurs on the ad.
        onAdImpression: (Ad ad) => print('Ad impression.'),
      ))
    ..load();

 

ca-app-pub-3940256099942544/6300978111 는 안드로이드 배너 테스트 광고 ID 이다.

테스트 과정에서 실 광고 ID넣고 클릭을 하게되면 정책위반으로 문제가 될 수 있다.

 

 Container(
      alignment: Alignment.center,
      width: _Banner.size.width.toDouble(),
      height: _Banner.size.height.toDouble(),
      child: AdWidget(
        ad: _Banner,
      ),
    )

 

배너를 원하는 위치에 넣어주게되면 광고가 추가된다.

 

테스트 모드로 테스트 후 실제 광고 ID를 넣으면 된다.

 

728x90