main.dart 应用启动文件
void main()async {
runApp(
MyApp(),
);
}
构建 初始页面my_app.dart
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
//添加拉起广告
// AdManager.showAd(state, _context); //不适合在此添加
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner:false,
initialRoute:"tabs", //导航页面
onGenerateRoute:Routes.onGenerateRoute,
navigatorKey:Routes.navigatorKey,
);
}
}
初始导航页面设置tabs.dart
class Tabs extends StatefulWidget {
@override
_TabsState createState() => _TabsState();
}
class _TabsState extends State<Tabs> with WidgetsBindingObserver {
int _currentIndex = 0;
//HomePage 首页,HistoryPage历史记录页,GuidePage教程页,MinePage个人中心页
final pages = [HomePage(), HistoryPage(), GuidePage(), MinePage()];
@override
void dispose() {
// TODO: implement dispose
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
//在此添加拉起"广告页"或"功能页"
AdManager.showAd(state, _context);
}
}