Skip to content

Commit

Permalink
Web back handle
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Jan 16, 2024
1 parent 3412fd2 commit 3e45986
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
20 changes: 19 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:developer';

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';
Expand Down Expand Up @@ -93,7 +95,7 @@ class _MyHomePageState extends State<MyHomePage> {
itemBuilder: (context, index) {
return [
const AListScreen(),
const WebScreen(),
WebScreen(key: webGlobalKey),
const SettingsScreen()
][index];
},
Expand All @@ -110,6 +112,12 @@ class _MyHomePageState extends State<MyHomePage> {
destinations: AppRouter.destinations,
selectedIndex: _selectedIndex,
onDestinationSelected: (int index) {
log(index.toString());
// Web
if (_selectedIndex == 1 && index == 1) {
webGlobalKey.currentState?.onClickNavigationBar();
}

setState(() {
_selectedIndex = index;
});
Expand All @@ -121,3 +129,13 @@ class _MyHomePageState extends State<MyHomePage> {
);
}
}

class NavigationBarController extends GetxController {
final _selectedIndex = 0.obs;

int get selectedIndex => _selectedIndex.value;

void setIndex(int index) {
_selectedIndex.value = index;
}
}
5 changes: 3 additions & 2 deletions lib/pages/alist/alist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class AListScreen extends StatelessWidget {
onTap: () async {
AppUpdateDialog.checkUpdateAndShowDialog(context, (b) {
if (!b) {
Get.showSnackbar(GetSnackBar(
Get.showSnackbar(const GetSnackBar(
message: "已经是最新版本",
duration: const Duration(seconds: 2)));
duration: Duration(seconds: 2)));
}
});
},
Expand Down Expand Up @@ -145,6 +145,7 @@ class AListController extends GetxController {
Event.setup(MyEventReceiver(
(isRunning) => isSwitch.value = isRunning, (log) => addLog(log)));
Android().getAListVersion().then((value) => alistVersion.value = value);
Android().isRunning().then((value) => isSwitch.value = value);

super.onInit();
}
Expand Down
61 changes: 22 additions & 39 deletions lib/pages/web/web.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:developer';

import 'package:alist_flutter/generated_api.dart';
import 'package:alist_flutter/utils/intent_utils.dart';
import 'package:android_intent_plus/android_intent.dart';
Expand All @@ -6,19 +8,27 @@ import 'package:flutter/services.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:get/get.dart';

GlobalKey<WebScreenState> webGlobalKey = GlobalKey();

class WebScreen extends StatefulWidget {
const WebScreen({Key? key}) : super(key: key);

@override
State<StatefulWidget> createState() {
return _WebScreenState();
return WebScreenState();
}
}

class _WebScreenState extends State<WebScreen> {
class WebScreenState extends State<WebScreen> {
InAppWebViewController? _webViewController;
double _progress = 0;
String _url = "http://localhost:5244";
bool _canGoBack = false;

onClickNavigationBar() {
log("onClickNavigationBar");
_webViewController?.reload();
}

@override
void initState() {
Expand All @@ -31,10 +41,12 @@ class _WebScreenState extends State<WebScreen> {
@override
Widget build(BuildContext context) {
return PopScope(
onPopInvoked: (b) {
_webViewController?.canGoBack().then((value) => {
if (value) {_webViewController?.goBack()} else {Get.back()}
});
canPop: !_canGoBack,
onPopInvoked: (didPop) async {
log("onPopInvoked $didPop");
if (didPop) return;

_webViewController?.goBack();
},
child: Scaffold(
body: Column(children: <Widget>[
Expand All @@ -51,7 +63,7 @@ class _WebScreenState extends State<WebScreen> {
_webViewController = controller;
},
onLoadStart: (InAppWebViewController controller, Uri? url) {
setState(() {
setState(() async {
_progress = 0;
});
},
Expand Down Expand Up @@ -121,41 +133,12 @@ class _WebScreenState extends State<WebScreen> {
_progress = progress / 100;
if (_progress == 1) _progress = 0;
});
controller.canGoBack().then((value) => setState(() {
_canGoBack = value;
}));
},
),
),
ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
child: const Icon(Icons.arrow_back),
onPressed: () {
if (_webViewController != null) {
_webViewController!.goBack();
}
},
),
ElevatedButton(
child: const Icon(Icons.arrow_forward),
onPressed: () {
if (_webViewController != null) {
_webViewController!.goForward();
}
},
),
ElevatedButton(
child: const Icon(Icons.refresh),
onPressed: () {
if (_webViewController != null) {
_webViewController!.reload();
}
_webViewController!.loadUrl(
urlRequest:
URLRequest(url: WebUri("http://coolapk.com")));
},
),
],
),
]),
));
}
Expand Down

0 comments on commit 3e45986

Please sign in to comment.