diff --git a/lib/common.dart b/lib/common.dart index b7024bbe1..afbaa1083 100644 --- a/lib/common.dart +++ b/lib/common.dart @@ -192,4 +192,3 @@ bool isAndroid = false; bool isIOS = false; bool isWeb = false; bool isDesktop = false; -BuildContext? currentCtx; diff --git a/lib/home_page.dart b/lib/home_page.dart index f29e18a66..96e2c1001 100644 --- a/lib/home_page.dart +++ b/lib/home_page.dart @@ -24,7 +24,6 @@ class _HomePageState extends State { @override void initState() { super.initState(); - currentCtx = context; if (isAndroid) { Timer(Duration(seconds: 5), () { _updateUrl = FFI.getByName('software_update_url'); diff --git a/lib/main.dart b/lib/main.dart index 32bd1a3d2..9c6e152be 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -16,6 +16,7 @@ Future main() async { runApp(App()); } +final globalKey = GlobalKey(); class App extends StatelessWidget { @override @@ -29,6 +30,7 @@ class App extends StatelessWidget { ChangeNotifierProvider.value(value: FFI.canvasModel), ], child: MaterialApp( + navigatorKey: globalKey, debugShowCheckedModeBanner: false, title: 'RustDesk', theme: ThemeData( diff --git a/lib/server_page.dart b/lib/server_page.dart index 6eb8e00de..72f1df4fd 100644 --- a/lib/server_page.dart +++ b/lib/server_page.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:flutter_hbb/main.dart'; import 'package:flutter_hbb/model.dart'; import 'package:provider/provider.dart'; @@ -155,12 +156,6 @@ class PermissionChecker extends StatefulWidget { } class _PermissionCheckerState extends State { - @override - void initState() { - super.initState(); - currentCtx = context; - } - @override Widget build(BuildContext context) { final serverModel = Provider.of(context); @@ -169,9 +164,11 @@ class _PermissionCheckerState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ cardTitle(translate("Configuration Permissions")), - PermissionRow(translate("Media"), serverModel.mediaOk, _toAndroidInitService), + PermissionRow( + translate("Media"), serverModel.mediaOk, _toAndroidInitService), const Divider(height: 0), - PermissionRow(translate("Input"), serverModel.inputOk, _toAndroidInitInput), + PermissionRow( + translate("Input"), serverModel.inputOk, showInputWarnAlert), const Divider(), serverModel.mediaOk ? ElevatedButton.icon( @@ -189,32 +186,42 @@ class _PermissionCheckerState extends State { BuildContext? loginReqAlertCtx; -void showLoginReqAlert(BuildContext context, String peerID, String name) async { +void showLoginReqAlert(String peerID, String name) async { + if (globalKey.currentContext == null) return; await showDialog( - context: context, + barrierDismissible: false, + context: globalKey.currentContext!, builder: (alertContext) { loginReqAlertCtx = alertContext; return AlertDialog( title: Text("Control Request"), - content:Column( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text(translate("Do you accept?")), - SizedBox(width: 20), - Row( + content: Container( + height: 100, + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.start, children: [ - CircleAvatar(child: Text(name[0])), - SizedBox(width: 10), - Text(name), - SizedBox(width: 5), - Text(peerID) + Text(translate("Do you accept?")), + SizedBox(height: 20), + Row( + children: [ + CircleAvatar(child: Text(name[0])), + SizedBox(width: 10), + Text(name), + SizedBox(width: 5), + Text(peerID) + ], + ), ], - ), - ], - ), + )), actions: [ TextButton( + child: Text(translate("Dismiss")), + onPressed: () { + FFI.setByName("login_res", "false"); + Navigator.of(alertContext).pop(); + }), + ElevatedButton( child: Text(translate("Accept")), onPressed: () { FFI.setByName("login_res", "true"); @@ -224,12 +231,6 @@ void showLoginReqAlert(BuildContext context, String peerID, String name) async { FFI.serverModel.setPeer(true); Navigator.of(alertContext).pop(); }), - TextButton( - child: Text(translate("Dismiss")), - onPressed: () { - FFI.setByName("login_res", "false"); - Navigator.of(alertContext).pop(); - }) ], ); }); @@ -240,6 +241,7 @@ clearLoginReqAlert() { if (loginReqAlertCtx != null) { Navigator.of(loginReqAlertCtx!).pop(); FFI.serverModel.updateClientState(); + loginReqAlertCtx = null; } } @@ -362,9 +364,44 @@ Future _toAndroidInitInput() async { debugPrint("_toAndroidInitInput:$res"); } +showInputWarnAlert() async { + if (globalKey.currentContext == null) return; + await showDialog( + context: globalKey.currentContext!, + builder: (alertContext) { + return AlertDialog( + title: Text("获取输入权限引导"), + // content: Text("请在接下来的系统设置页面 \n进入 [服务] 配置页面\n将[RustDesk Input]服务开启"), + content: Text.rich(TextSpan( + style: TextStyle(), + children: [ + TextSpan(text:"请在接下来的系统设置页\n进入"), + TextSpan(text:" [服务] ",style: TextStyle(color: MyTheme.accent)), + TextSpan(text:"配置页面\n将"), + TextSpan(text:" [RustDesk Input] ",style: TextStyle(color: MyTheme.accent)), + TextSpan(text:"服务开启") + ] + )), + actions: [ + TextButton( + child: Text(translate("Do nothing")), + onPressed: () { + Navigator.of(alertContext).pop(); + }), + ElevatedButton( + child: Text(translate("Go System Setting")), + onPressed: () { + _toAndroidInitInput(); + Navigator.of(alertContext).pop(); + }), + ], + ); + }); +} + void toAndroidChannelInit() { FFI.setMethodCallHandler((method, arguments) { - debugPrint("flutter got android msg"); + debugPrint("flutter got android msg,$method,$arguments"); try { switch (method) { case "try_start_without_auth": @@ -372,10 +409,7 @@ void toAndroidChannelInit() { FFI.serverModel.updateClientState(); debugPrint( "pre show loginAlert:${FFI.serverModel.isFileTransfer.toString()}"); - if(currentCtx!=null){ - showLoginReqAlert( - currentCtx!, FFI.serverModel.peerID, FFI.serverModel.peerName); - } + showLoginReqAlert(FFI.serverModel.peerID, FFI.serverModel.peerName); debugPrint("from jvm:try_start_without_auth done"); break; } @@ -388,6 +422,7 @@ void toAndroidChannelInit() { case "stop_capture": { FFI.serverModel.setPeer(false); + clearLoginReqAlert(); break; } case "on_permission_changed":