diff --git a/lib/main.dart b/lib/main.dart index 7078a19..5f02f48 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'dart:developer'; +import 'dart:ffi'; import 'package:alist_flutter/generated/l10n.dart'; import 'package:alist_flutter/generated_api.dart'; @@ -6,13 +7,14 @@ import 'package:alist_flutter/pages/alist/alist.dart'; import 'package:alist_flutter/pages/app_update_dialog.dart'; import 'package:alist_flutter/pages/settings/settings.dart'; import 'package:alist_flutter/pages/web/web.dart'; +import 'package:animations/animations.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/scheduler.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_svg/svg.dart'; import 'package:get/get.dart'; +import 'package:lazy_load_indexed_stack/lazy_load_indexed_stack.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); @@ -54,7 +56,7 @@ class MyApp extends StatelessWidget { } } -class MyHomePage extends StatefulWidget { +class MyHomePage extends StatelessWidget { const MyHomePage({super.key, required this.title}); // This widget is the home page of your application. It is stateful, meaning @@ -67,86 +69,77 @@ class MyHomePage extends StatefulWidget { // always marked "final". final String title; + static const webPageIndex = 0; @override - State createState() => _MyHomePageState(); + Widget build(BuildContext context) { + final controller = Get.put(_MainController()); + + return Scaffold( + body: Obx( + () => LazyLoadIndexedStack( + index: controller.selectedIndex.value, + children: [ + WebScreen(key: webGlobalKey), + const AListScreen(), + const SettingsScreen() + ], + ), + ), + bottomNavigationBar: Obx(() => NavigationBar( + destinations: [ + NavigationDestination( + icon: const Icon(Icons.preview), + label: S.current.webPage, + ), + NavigationDestination( + icon: SvgPicture.asset( + "assets/alist.svg", + color: Theme.of(context).hintColor, + width: 32, + height: 32, + ), + label: S.current.appName, + ), + NavigationDestination( + icon: const Icon(Icons.settings), + label: S.current.settings, + ), + ], + selectedIndex: controller.selectedIndex.value, + onDestinationSelected: (int index) { + log(index.toString()); + // Web + if (controller.selectedIndex.value == webPageIndex && + controller.selectedIndex.value == webPageIndex) { + webGlobalKey.currentState?.onClickNavigationBar(); + } + + controller.setPageIndex(index); + }))); + } } -class _MyHomePageState extends State { - int _selectedIndex = 1; - final PageController _pageController = PageController(); +class _MainController extends GetxController { + final selectedIndex = 1.obs; - @override - void initState() { - super.initState(); + setPageIndex(int index) { + selectedIndex.value = index; + } - AppConfig().isAutoOpenWebPageEnabled().then((value) => setState(() { - _selectedIndex = value ? 0 : 1; - })); + @override + void onInit() async { + final webPage = await AppConfig().isAutoOpenWebPageEnabled(); + if (webPage) { + setPageIndex(MyHomePage.webPageIndex); + } WidgetsBinding.instance.addPostFrameCallback((timeStamp) async { if (await AppConfig().isAutoCheckUpdateEnabled()) { - AppUpdateDialog.checkUpdateAndShowDialog(context, null); + AppUpdateDialog.checkUpdateAndShowDialog(Get.context!, null); } }); - } - @override - Widget build(BuildContext context) { - return Scaffold( - body: PageView.builder( - itemBuilder: (context, index) { - return [ - WebScreen(key: webGlobalKey), - const AListScreen(), - const SettingsScreen() - ][index]; - }, - physics: const NeverScrollableScrollPhysics(), - scrollDirection: Axis.horizontal, - controller: _pageController, - onPageChanged: (int index) { - setState(() { - _selectedIndex = index; - }); - }, - ), - bottomNavigationBar: NavigationBar( - destinations: [ - NavigationDestination( - icon: const Icon(Icons.preview), - label: S.current.webPage, - ), - NavigationDestination( - icon: SvgPicture.asset( - "assets/alist.svg", - color: Theme.of(context).hintColor, - width: 32, - height: 32, - ), - label: S.current.appName, - ), - NavigationDestination( - icon: const Icon(Icons.settings), - label: S.current.settings, - ), - ], - selectedIndex: _selectedIndex, - onDestinationSelected: (int index) { - log(index.toString()); - // Web - if (_selectedIndex == 0 && index == 0) { - webGlobalKey.currentState?.onClickNavigationBar(); - } - - setState(() { - _selectedIndex = index; - }); - _pageController.animateToPage(index, - duration: const Duration(milliseconds: 200), - curve: Curves.easeOutCubic); - }, - ), - ); + super.onInit(); } } diff --git a/lib/pages/web/web.dart b/lib/pages/web/web.dart index 344d6ab..81ff623 100644 --- a/lib/pages/web/web.dart +++ b/lib/pages/web/web.dart @@ -40,6 +40,12 @@ class WebScreenState extends State { super.initState(); } + @override + void dispose() { + _webViewController?.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { return PopScope( @@ -47,7 +53,6 @@ class WebScreenState extends State { onPopInvoked: (didPop) async { log("onPopInvoked $didPop"); if (didPop) return; - _webViewController?.goBack(); }, child: Scaffold( @@ -90,6 +95,15 @@ class WebScreenState extends State { return NavigationActionPolicy.CANCEL; }, + onReceivedError: (controller, request, error) async { + final isRunning = await Android().isRunning(); + if (!isRunning) { + Android().startService(); + } else { + await Future.delayed(const Duration(milliseconds: 100)); + } + controller.reload(); + }, onDownloadStartRequest: (controller, url) async { Get.showSnackbar(GetSnackBar( title: S.of(context).downloadThisFile, diff --git a/pubspec.lock b/pubspec.lock index 3363686..6203c43 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -25,6 +25,14 @@ packages: url: "https://pub.dev" source: hosted version: "4.0.3" + animations: + dependency: "direct main" + description: + name: animations + sha256: d3d6dcfb218225bbe68e87ccf6378bbb2e32a94900722c5f81611dad089911cb + url: "https://pub.dev" + source: hosted + version: "2.0.11" args: dependency: transitive description: @@ -237,6 +245,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.6.7" + lazy_load_indexed_stack: + dependency: "direct main" + description: + name: lazy_load_indexed_stack + sha256: e581426391c44708210b1f571046320ea52dea46740d4972d9e1820b4ef94827 + url: "https://pub.dev" + source: hosted + version: "1.1.0" lints: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index b980ff1..3200141 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -43,6 +43,8 @@ dependencies: android_intent_plus: ^4.0.3 flutter_svg: ^2.0.9 permission_handler: ^11.1.0 + lazy_load_indexed_stack: ^1.1.0 + animations: ^2.0.11 dev_dependencies: