question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

🐛 [iOS] Weird behavior of Ads inside listview with column

See original GitHub issue

Bug report

Describe the bug If an ad is inside a column in a ListView (or SingleChildScrollView) it doesn’t stay anchored correctly to its position when it exits to the top during scrolling.

Check this video

Steps to reproduce

Steps to reproduce the behavior:

  1. Take the inline banner example from the codelabs
  2. Add a column as this:
// COMPLETE: Import ad_helper.dart
import 'package:admob_inline_ads_in_flutter/ad_helper.dart';

import 'package:admob_inline_ads_in_flutter/destination.dart';

// COMPLETE: Import google_mobile_ads.dart
import 'package:google_mobile_ads/google_mobile_ads.dart';

import 'package:flutter/material.dart';

class BannerInlinePage extends StatefulWidget {
  final List<Destination> entries;

  BannerInlinePage({this.entries});

  @override
  State createState() => _BannerInlinePageState();
}

class _BannerInlinePageState extends State<BannerInlinePage> {
  // COMPLETE: Add _kAdIndex
  static final _kAdIndex = 4;

  // COMPLETE: Add a BannerAd instance
  BannerAd _ad;

  // COMPLETE: Add _isAdLoaded
  bool _isAdLoaded = false;

  @override
  void initState() {
    super.initState();

    // COMPLETE: Create a BannerAd instance
    _ad = BannerAd(
      adUnitId: AdManager.bannerAdUnitId,
      size: AdSize.mediumRectangle,
      request: AdRequest(),
      listener: AdListener(
        onAdLoaded: (_) {
          setState(() {
            _isAdLoaded = true;
          });
        },
        onAdFailedToLoad: (ad, error) {
          // Releases an ad resource when it fails to load
          ad.dispose();

          print('Ad load failed (code=${error.code} message=${error.message})');
        },
      ),
    );

    // COMPLETE: Load an ad
    _ad.load();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AdMob Banner Inline Ad'),
      ),
      body: ListView.builder(
        // COMPLETE: Adjust itemCount based on the ad load state
        itemCount: widget.entries.length + (_isAdLoaded ? 1 : 0),
        itemBuilder: (context, index) {
          // COMPLETE: Render a banner ad
          if (_isAdLoaded && index == _kAdIndex) {
            final item = widget.entries[0];

            return Column(
              children: [
                Container(
                  child: AdWidget(ad: _ad),
                  width: _ad.size.width.toDouble(),
                  height: 300.0,
                  alignment: Alignment.center,
                ),
                ListTile(
                  leading: Image.asset(
                    item.asset,
                    width: 48,
                    height: 48,
                    package: 'flutter_gallery_assets',
                    fit: BoxFit.cover,
                  ),
                  title: Text(item.name),
                  subtitle: Text(item.duration),
                  onTap: () {
                    print('Clicked ${item.name}');
                  },
                ),
                ListTile(
                  leading: Image.asset(
                    item.asset,
                    width: 48,
                    height: 48,
                    package: 'flutter_gallery_assets',
                    fit: BoxFit.cover,
                  ),
                  title: Text(item.name),
                  subtitle: Text(item.duration),
                  onTap: () {
                    print('Clicked ${item.name}');
                  },
                ),
              ],
            );
          } else {
            // COMPLETE: Get adjusted item index from _getDestinationItemIndex()
            final item = widget.entries[_getDestinationItemIndex(index)];

            return ListTile(
              leading: Image.asset(
                item.asset,
                width: 48,
                height: 48,
                package: 'flutter_gallery_assets',
                fit: BoxFit.cover,
              ),
              title: Text(item.name),
              subtitle: Text(item.duration),
              onTap: () {
                print('Clicked ${item.name}');
              },
            );
          }
        },
      ),
    );
  }

  @override
  void dispose() {
    // COMPLETE: Dispose a BannerAd object
    _ad?.dispose();
    super.dispose();
  }

  // COMPLETE: Add _getDestinationItemIndex()
  int _getDestinationItemIndex(int rawIndex) {
    if (rawIndex >= _kAdIndex && _isAdLoaded) {
      return rawIndex - 1;
    }
    return rawIndex;
  }
}

  1. Build and scroll up and down. You can also use a SingleChildScrollView with a Column.

Expected behavior

The ad should remain anchored to its position and disappear smoothly.


Additional context

To solve this problem, just don’t use columns. However, this did not happen in the previous version of the package (firebase_admob).


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
[✓] Flutter (Channel stable, 2.0.3, on Mac OS X 10.15.7 19H2 darwin-x64, locale it-IT)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] VS Code (version 1.54.3)
[✓] Connected device (3 available)
 • No issues found!

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand
Dart SDK 2.12.2
Flutter SDK 2.0.3
admob_inline_ads_in_flutter 1.0.0+1

dependencies:
- flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine]
- flutter_gallery_assets 0.2.2
- google_mobile_ads 0.12.0 [meta flutter]

dev dependencies:
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data]

transitive dependencies:
- async 2.5.0 [collection]
- boolean_selector 2.1.0 [source_span string_scanner]
- characters 1.1.0
- charcode 1.2.0
- clock 1.1.0
- collection 1.15.0
- fake_async 1.2.0 [clock collection]
- matcher 0.12.10 [stack_trace]
- meta 1.3.0
- path 1.8.0
- sky_engine 0.0.99
- source_span 1.8.0 [charcode collection path term_glyph]
- stack_trace 1.10.0 [path]
- stream_channel 2.1.0 [async]
- string_scanner 1.1.0 [charcode source_span]
- term_glyph 1.2.0
- test_api 0.2.19 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher]
- typed_data 1.3.0 [collection]
- vector_math 2.1.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:10
  • Comments:11

github_iconTop GitHub Comments

3reactions
mustafa-707commented, Apr 13, 2021

@Nader2004 Same here , i gave up already 0 updates and no one care , the problem with flutter 2 not with the plugin after much experiments , i just got back to flutter 1.22.6 and also i got back to flutter_google_ad_manager cause this plugin have much problems with old android and it cause much crashes and it’s have a bad performance , but for real i used it with rewarded ads cause it’s better but the interstitial and the banners still have much issues

0reactions
blastencommented, Apr 29, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Common Flutter errors
The error is often caused when a ListView (or other kinds of scrollable widgets such as GridView ) is placed inside a Column...
Read more >
Xamarin.Forms Strange behavior of ListView - Stack Overflow
Im having a problem with listview and im almost giving upp. I have a CustomButton inside a ListView, OnPressUp it should trigger an...
Read more >
Weird ImageButton behavior inside ListView - Xamarin.Forms
I have an ImageButton inside a ListView. Everything is working perfectly (binds, tap events..). The XAML code is below. <ImageButton. Source="more_options"
Read more >
Monterey Finder Column View Bug Killing Me - Please Help
Since I downloaded Monterey, whenever I use Finder in column view, it springs backwards when I scroll horizontally.
Read more >
A Much Easier to Use ListView - CodeProject
All projects suffer creeping featurism. Things that start simple and elegant end up as the "before" image in a weight-loss ad. This control...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found