【Flutter】Flutter Webで広告を表示する
Flutter iOS/AndroidであればAdMobのSDKを使って簡単に広告を表示することが出来ます。しかしAdMobはFlutterWebに対応していませんし、AdSenseもそのままでは表示することが出来ません。
ではどうすればよいか?Flutterで広告を表示することを諦めます。HTML側で画面下部に広告を表示し、Flutter側ではその領域に表示しないようにします。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| 〜省略〜 | |
| <style type="text/css"> | |
| <!-- | |
| footer{ | |
| width: 100%; | |
| height: 100px; ←広告の高さと同じ高さを指定する | |
| z-index: 10; ←一番前に表示させる | |
| position: absolute; | |
| bottom: 0; | |
| } | |
| --> | |
| </style> | |
| </head> | |
| <body> | |
| 〜省略〜 | |
| <footer id="footer"> | |
| 〜ここに広告のスクリプトを入れる〜 | |
| </footer> | |
| </body> |
Flutter側ではHTML側で設定した分のmarginを設定して広告の領域には何も表示しないようにします。z-indexを指定しているので広告を隠してしまうことはありませんがボタンが広告に隠れてタップできないなどの問題が生じる可能性があるからです。
開発環境は広告が出ないようにする
できれば開発中は広告を出したくありません。 なぜなら間違って広告をタップしてしまったときにそれが不正と思われるとよろしくないからです。
そこでDeploy用のスクリプトを作りindex.htmlを差し替えることで開発中は広告を出ないようにしています。 開発用のindex.htmlでは広告を表示するスクリプトを呼ばないようにコメントアウトしています。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash -x | |
| cp -r ./index_prod.html ./web/index.html | |
| fvm flutter build web | |
| firebase deploy | |
| cp -r ./index_local.html ./web/index.html |