Flutter 导航抽屉,导航推送同时保留横幅广告

问题描述 投票:0回答:2

如何在导航时保留 Flutter 导航抽屉中的横幅广告?

在 Android Studio Native 中,我可以在 MainActivity 中设置横幅,因此 Fragments 导航不会重新加载横幅广告。

如何在 Flutter 中实现这一目标?

Navigator.of(context).push(MaterialPageRoute<void>(builder: (context) => const SecondScreen()));

这将创建一个新的屏幕和横幅广告丢失。

home.dart

    Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(''),
        ),
        body: SafeArea(
            child: Stack(children: [
          const Center(),
          // TODO: Display a banner when ready
          if (_isBannerAdReady)
            Align(
              alignment: Alignment.bottomCenter,
              child: SizedBox(
                width: _bannerAd.size.width.toDouble(),
                height: _bannerAd.size.height.toDouble(),
                child: AdWidget(ad: _bannerAd),
              ),
            ),
        ])),
        drawer: NavBar());
  }

nav_bar.dart

   class NavBar extends StatefulWidget {
  const NavBar({Key? key}) : super(key: key);

  @override
  _NavBarState createState() => _NavBarState();
}
Widget build(BuildContext context) {
    return Drawer(
      elevation: 10.0,
      child: ListView(
        children: <Widget>[
          DrawerHeader(
            decoration: BoxDecoration(
                color: Colors.grey.shade500
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                const CircleAvatar(
                  radius: 40.0,
                ),
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: const <Widget>[
                    Text('',
                      style: TextStyle(
                          fontWeight: FontWeight.bold,
                          color: Colors.white,
                          fontSize: 25.0
                      ),
                    ),
                    SizedBox(height: 10.0),
                    Text('',
                      style: TextStyle(
                          fontWeight: FontWeight.bold,
                          color: Colors.white,
                          fontSize: 14.0
                      ),
                    ),
                  ],
                )
              ],
            ),
          ),
          ListTile(
            leading: const Icon(Icons.home),
            title: Text('Home ', style: _biggerFont),
            onTap: () {
              Navigator.of(context).pop();
              Navigator.of(context).push(
                  MaterialPageRoute<void>(builder: (context) => const HomeScreen())
              );
            },
          ),
          const Divider(height: 3.0),
          ListTile(
            leading: const Icon(Icons.warning),
            title: Text('2nd Tab', style: _biggerFont),
            onTap: () {
              // Here you can give your route to navigate
              Navigator.of(context).pop();
              Navigator.of(context).push(MaterialPageRoute<void>(builder: (context) => const SecondScreen()));
            },
          ),
          
        ],
      ),
    );
  }

这是我的屏幕:

Screen 1

Screen 2

Screen 3

flutter widget admob ads banner
2个回答
1
投票

您无法在第二屏幕中保留在主页上编码的横幅广告。这是因为在第二个屏幕中该小部件不存在。当您导航到不同的页面时,它具有完全不同的小部件。要在应用程序的所有页面中展示广告,您必须在每个页面中单独编写代码才能展示广告。


0
投票

注意屏幕 2 图像! 我的应用程序中的广告被 admob 网络拦截,因为导航抽屉位于背面,因此广告就在上面。我已经研究这个问题大约3个月了,在此过程中我没有收到任何违反政策的情况。昨天他们终于回复了我的反馈,用两张截图回答了问题。 问题消息“完全或部分阻止 Google 投放的广告的内容”。导航抽屉悬停在广告上方。

© www.soinside.com 2019 - 2024. All rights reserved.