googleads-mobile-flutter icon indicating copy to clipboard operation
googleads-mobile-flutter copied to clipboard

[iOS] Wrong position of banner ad when using hot-restart and camera

Open dvagala opened this issue 1 year ago • 1 comments

It seems that when a camera controller is not correctly disposed (hot-restart is used), the banner ad totally ignores where it should be rendered, and it places itself at top of the screen. The issue is only on iOS, I tried Android and it's working as it should.

It's not a critical bug since it only takes place during development and using hot-restart, but it's a bit inconvenient. Camera package version: 0.10.5+5

Plugin Version

4.0.0

Steps to Reproduce

  1. Use this widget that shows a banner ad and initializes a camera controller.
  2. Apply hot-restart
import 'dart:async';

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

class TestScreen1 extends StatefulWidget {
  const TestScreen1({super.key});

  @override
  State<TestScreen1> createState() => _TestScreen1State();
}

class _TestScreen1State extends State<TestScreen1> {
  BannerAd? bannerAd;

  @override
  void initState() {
    loadBannerAd();
    initializeCamera();
    super.initState();
  }

  Future<void> loadBannerAd() async {
    const iosTestUnitID = 'ca-app-pub-3940256099942544/2934735716';

    final size = await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(400);
    if (size == null) return;

    await BannerAd(
      adUnitId: iosTestUnitID,
      request: const AdRequest(),
      size: size,
      listener: BannerAdListener(
        onAdLoaded: (ad) {
          setState(() {
            bannerAd = ad as BannerAd;
          });
        },
        onAdFailedToLoad: (ad, err) => ad.dispose(),
      ),
    ).load();
  }

  Future<void> initializeCamera() async {
    final camera = (await availableCameras())
        .firstWhere((element) => element.lensDirection == CameraLensDirection.back);
    final cameraController = CameraController(
      camera,
      ResolutionPreset.veryHigh,
      enableAudio: false,
      imageFormatGroup: ImageFormatGroup.yuv420,
    );

    await cameraController.initialize();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text('Banner container:'),
            Container(
              child: bannerAd == null
                  ? const SizedBox.shrink()
                  : SizedBox(
                      width: bannerAd!.size.width.toDouble(),
                      height: bannerAd!.size.height.toDouble(),
                      child: Container(
                        color: Colors.red,
                        child: AdWidget(ad: bannerAd!),
                      ),
                    ),
            ),
          ],
        ),
      ),
    );
  }
}

Expected results: ee

Actual results: wwwww

Logs
[✓] Flutter (Channel stable, 3.7.0, on macOS 13.0 22A380 darwin-arm64, locale en-SK)
    • Flutter version 3.7.0 on channel stable at /Users/dvagala/fvm/versions/3.7.0
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b06b8b2710 (11 months ago), 2023-01-23 16:55:55 -0800
    • Engine revision b24591ed32
    • Dart version 2.19.0
    • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
    • Android SDK at /Users/dvagala/Library/Android/sdk
    • Platform android-33, build-tools 32.0.0
    • Java binary at: /Applications/Android Studio old.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E222b
    • CocoaPods version 1.14.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.1)
    • Android Studio at /Applications/Android Studio old.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)

[!] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

[✓] VS Code (version 1.84.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.80.0

[✓] Connected device (3 available)
    • iPhone 12 pro Dominik (mobile) • 00008101-001128A23C52001E • ios            • iOS 16.6.1 20G81
    • macOS (desktop)                • macos                     • darwin-arm64   • macOS 13.0 22A380 darwin-arm64
    • Chrome (web)                   • chrome                    • web-javascript • Google Chrome 120.0.6099.129

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.

dvagala avatar Dec 29 '23 13:12 dvagala

Hi @dvagala,

Considering this is only affects dev on hot-restart this is not something we are planning to address in the near future but will keep the issue open for visibility's sake. Thanks!

malandr2 avatar Jan 05 '24 02:01 malandr2