Skip to content

Commit

Permalink
优化导航页面切换
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Jan 18, 2024
1 parent 4d157b7 commit 48e3e37
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 72 deletions.
135 changes: 64 additions & 71 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import 'dart:developer';
import 'dart:ffi';

import 'package:alist_flutter/generated/l10n.dart';
import 'package:alist_flutter/generated_api.dart';
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();
Expand Down Expand Up @@ -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
Expand All @@ -67,86 +69,77 @@ class MyHomePage extends StatefulWidget {
// always marked "final".

final String title;
static const webPageIndex = 0;

@override
State<MyHomePage> 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<MyHomePage> {
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();
}
}
16 changes: 15 additions & 1 deletion lib/pages/web/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@ class WebScreenState extends State<WebScreen> {
super.initState();
}

@override
void dispose() {
_webViewController?.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return PopScope(
canPop: !_canGoBack,
onPopInvoked: (didPop) async {
log("onPopInvoked $didPop");
if (didPop) return;

_webViewController?.goBack();
},
child: Scaffold(
Expand Down Expand Up @@ -90,6 +95,15 @@ class WebScreenState extends State<WebScreen> {

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,
Expand Down
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 48e3e37

Please sign in to comment.