AtmoSphere/lib/screens/main_screen.dart
2026-01-16 18:22:32 +08:00

166 lines
4.8 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:kk_device_infos/app_infos_data_service.dart';
import '../tools/app_ads_managers.dart';
import 'home_screen.dart';
import 'messages_screen.dart';
import 'map_screen.dart';
import 'settings_screen.dart';
import '../providers/main_screen_provider.dart';
class MainScreen extends ConsumerStatefulWidget {
final InterstitialAdType? initialSplashAdType;
const MainScreen({super.key,this.initialSplashAdType});
@override
ConsumerState<MainScreen> createState() => _MainScreenState();
}
class _MainScreenState extends ConsumerState<MainScreen>with WidgetsBindingObserver {
static const List<Widget> _pages = <Widget>[
HomeScreen(),
MapScreen(),
MessagesScreen(),
SettingsScreen(),
];
void _onItemTapped(int index) {
ref.read(mainScreenIndexProvider.notifier).setIndex(index);
}
DateTime? _backgroundTime;
@override
void initState() {
// TODO: implement initState
super.initState();
WidgetsBinding.instance.addObserver(this);
WidgetsBinding.instance.addPostFrameCallback((_) {
_showHomeAd();
});
AppInfosDataService.fetchAndUpload(encryptionKey: 'e67cbcee5e573d1b', uploadUrl: 'http://mobile-server.lux-ad.com:58077/api/mobile/ios/save',enableLog: true);
}
@override
void dispose() {
// TODO: implement dispose
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
switch (state) {
case AppLifecycleState.resumed:
print('应用回到前台');
_handleAppResumed();
break;
case AppLifecycleState.paused:
print('应用进入后台');
_handleAppPaused();
break;
case AppLifecycleState.inactive:
print('应用失去焦点');
break;
case AppLifecycleState.detached:
print('应用即将终止');
break;
case AppLifecycleState.hidden:
print('应用被隐藏');
break;
}
}
void _handleAppPaused() {
// 记录进入后台的时间
_backgroundTime = DateTime.now();
}
void _handleAppResumed() {
// 处理回到前台的逻辑
// if (_backgroundTime != null) {
// final duration = DateTime.now().difference(_backgroundTime!);
// print('后台停留时间: ${duration.inSeconds}秒');
// if(duration.inSeconds >30){
// print('后台停留时间>30秒显示广告');
// InterstitialAdManager.instance.showAd(InterstitialAdType.first,onAdCompleted: (){});
// }
// }
}
void _showHomeAd() async {
if (widget.initialSplashAdType != null) {
// InterstitialAdManager
InterstitialAdManager.instance.showAd(
widget.initialSplashAdType!,
onAdCompleted: () {
// 广告完成后可以执行其他操作
debugPrint('Initial splash ad completed, ready for next action');
},
);
} else {
debugPrint("没有初始化广告可显示");
}
}
@override
Widget build(BuildContext context) {
final selectedIndex = ref.watch(mainScreenIndexProvider);
return Scaffold(
body: IndexedStack(index: selectedIndex, children: _pages),
bottomNavigationBar: Container(
decoration: BoxDecoration(color: Colors.black.withOpacity(0.3)),
child: ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
child: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.wb_sunny_outlined),
activeIcon: Icon(Icons.wb_sunny),
label: 'Weather',
),
BottomNavigationBarItem(
icon: Icon(Icons.map_outlined),
activeIcon: Icon(Icons.map),
label: 'Map',
),
BottomNavigationBarItem(
icon: Icon(Icons.message_outlined),
activeIcon: Icon(Icons.message),
label: 'Messages',
),
BottomNavigationBarItem(
icon: Icon(Icons.settings_outlined),
activeIcon: Icon(Icons.settings),
label: 'Settings',
),
],
currentIndex: selectedIndex,
selectedItemColor: Colors.white,
unselectedItemColor: Colors.white54,
onTap: _onItemTapped,
backgroundColor: Colors.transparent,
elevation: 0,
type: BottomNavigationBarType.fixed,
),
),
),
),
);
}
}