[英]Flutter navigation drawer, navigate push while retaining banner ads
导航时如何在 Flutter Navigation Drawer 中保留横幅广告?
在 Android Studio Native 中,我可以在 MainActivity 设置横幅,因此片段导航不会重新加载横幅广告。
你如何在 Flutter 中实现这一点?
Navigator.of(context).push(MaterialPageRoute<void>(builder: (context) => const SecondScreen()));
这将创建一个新的屏幕和横幅广告不见了。
主页.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()));
},
),
],
),
);
}
这是我的屏幕:
您不能在第二个屏幕中保留在主页上编码的横幅广告。 这是因为在第二个屏幕中,小部件不存在。 当您导航到不同的页面时,它具有完全不同的小部件。 要在您应用的所有页面中展示广告,您必须在每个页面中单独编码以展示您的广告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.