import 'dart:convert'; import 'dart:math'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:get/get.dart'; import 'package:hello_wallpaper/applovin_max/applovin_manage.dart'; import 'package:hello_wallpaper/common/components/view_state_widget.dart'; import 'package:hello_wallpaper/firebase/analytics_util.dart'; import 'package:hello_wallpaper/generated/assets.dart'; import 'package:hello_wallpaper/models/wallpaper_model.dart'; import 'package:hello_wallpaper/modules/catalog/catalog_controller.dart'; import 'package:hello_wallpaper/modules/catalog/catalog_view.dart'; import 'package:hello_wallpaper/modules/discover/discover_controller.dart'; import 'package:hello_wallpaper/modules/discover/discover_view.dart'; import 'package:hello_wallpaper/modules/favorite/favorite_controller.dart'; import 'package:hello_wallpaper/modules/favorite/favorite_view.dart'; import 'package:hello_wallpaper/routes/app_pages.dart'; class HomeController extends GetxController with WidgetsBindingObserver { static HomeController get to => Get.find(); final pages = [ PageItem('Discover', [Assets.imagesDiscoverUnchecked, Assets.imagesDiscoverSelected], const DiscoverView()), PageItem('Favorite', [Assets.imagesFavoriteUnchecked, Assets.imagesFavoriteSelected], const FavoriteView()), PageItem('Catalog', [Assets.imagesCatalogUnchecked, Assets.imagesCatalogSelected], const CatalogView()), ]; late PageController pageController; var currentIndex = 0.obs; var wallpaperModelList = []; var viewState = ViewState.loading; @override void onInit() { super.onInit(); WidgetsBinding.instance.addObserver(this); pageController = PageController(initialPage: currentIndex.value); AnalyticsUtil.initializeFlutterFire(); } @override void onReady() { super.onReady(); _getAllData(); } @override void onClose() { WidgetsBinding.instance.removeObserver(this); pageController.dispose(); super.onClose(); } @override Future didChangeAppLifecycleState(AppLifecycleState state) async { switch (state) { case AppLifecycleState.resumed: await ApplovinManage().showAdIfReady(ApplovinManage().adUnitId1); break; case AppLifecycleState.inactive: case AppLifecycleState.hidden: case AppLifecycleState.paused: case AppLifecycleState.detached: break; } } /// PageView页面改变回调 void onPageChanged(int index) { currentIndex.value = index; } /// 点击BottomNavigationBar void onTapNavigationBar(int index) { pageController.jumpToPage(index); ApplovinManage().showAdIfReady(ApplovinManage().adUnitId1); } /// 点击设置 void onTapSettings() { Get.toNamed(AppPages.settings); } // void _showPopMenu(context) { // showMenu( // context: context, // surfaceTintColor: Colors.white, // position: RelativeRect.fromLTRB(double.infinity, statusToolBarHeight, 0.0, 0.0), // items: [ // PopupMenuItem( // value: 'delete', // onTap: deleteOnTap, // child: const ListTile( // leading: Icon(Icons.delete), // title: Text('Delete All'), // ), // ), // ], // ); // } /// 获取所有数据 Future _getAllData() async { // 读取json文件,获取数据 var data = jsonDecode(await rootBundle.loadString(Assets.jsonLuxWallpaper)); if (data != null && data is List) { wallpaperModelList = data.map((e) => WallpaperModel.fromJson(e)).toList(); viewState = wallpaperModelList.isNotEmpty ? ViewState.normal : ViewState.empty; } if (Get.isRegistered()) { DiscoverController.to.getData(); } if (Get.isRegistered()) { FavoriteController.to.getTodayHottestList(); } if (Get.isRegistered()) { CatalogController.to.getData(); } } /// 随机取一个分类 WallpaperModel? getRandomCls() { if (wallpaperModelList.isNotEmpty) { Random random = Random(); int i = random.nextInt(wallpaperModelList.length); return wallpaperModelList[i]; } return null; } } class PageItem { late final String label; late final List icons; late final StatelessWidget widget; PageItem(this.label, this.icons, this.widget); }