refactor main runApp()

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2022-11-02 22:23:23 +08:00
parent 411ce169f0
commit c2d843ace6
3 changed files with 86 additions and 123 deletions

View File

@ -16,6 +16,7 @@ import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:window_manager/window_manager.dart';
import 'package:bot_toast/bot_toast.dart';
// import 'package:window_manager/window_manager.dart';
@ -53,15 +54,27 @@ Future<void> main(List<String> args) async {
switch (wType) {
case WindowType.RemoteDesktop:
desktopType = DesktopType.remote;
runRemoteScreen(argument);
runMultiWindow(
argument,
kAppTypeDesktopRemote,
'RustDesk - Remote Desktop',
);
break;
case WindowType.FileTransfer:
desktopType = DesktopType.fileTransfer;
runFileTransferScreen(argument);
runMultiWindow(
argument,
kAppTypeDesktopFileTransfer,
'RustDesk - File Transfer',
);
break;
case WindowType.PortForward:
desktopType = DesktopType.portForward;
runPortForwardScreen(argument);
runMultiWindow(
argument,
kAppTypeDesktopPortForward,
'RustDesk - Port Forward',
);
break;
default:
break;
@ -120,84 +133,18 @@ void runMobileApp() async {
runApp(App());
}
void runRemoteScreen(Map<String, dynamic> argument) async {
await initEnv(kAppTypeDesktopRemote);
runApp(RefreshWrapper(
builder: (context) => GetMaterialApp(
navigatorKey: globalKey,
debugShowCheckedModeBanner: false,
title: 'RustDesk - Remote Desktop',
theme: MyTheme.lightTheme,
darkTheme: MyTheme.darkTheme,
themeMode: MyTheme.currentThemeMode(),
home: DesktopRemoteScreen(
params: argument,
),
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: supportedLocales,
navigatorObservers: const [
// FirebaseAnalyticsObserver(analytics: analytics),
],
builder: _keepScaleBuilder(),
void runMultiWindow(
Map<String, dynamic> argument,
String appType,
String title,
) async {
await initEnv(appType);
_runApp(
title,
DesktopRemoteScreen(
params: argument,
),
));
}
void runFileTransferScreen(Map<String, dynamic> argument) async {
await initEnv(kAppTypeDesktopFileTransfer);
runApp(
RefreshWrapper(
builder: (context) => GetMaterialApp(
navigatorKey: globalKey,
debugShowCheckedModeBanner: false,
title: 'RustDesk - File Transfer',
theme: MyTheme.lightTheme,
darkTheme: MyTheme.darkTheme,
themeMode: MyTheme.currentThemeMode(),
home: DesktopFileTransferScreen(params: argument),
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: supportedLocales,
navigatorObservers: const [
// FirebaseAnalyticsObserver(analytics: analytics),
],
builder: _keepScaleBuilder(),
),
),
);
}
void runPortForwardScreen(Map<String, dynamic> argument) async {
await initEnv(kAppTypeDesktopPortForward);
runApp(
RefreshWrapper(builder: (context) {
return GetMaterialApp(
navigatorKey: globalKey,
debugShowCheckedModeBanner: false,
title: 'RustDesk - Port Forward',
theme: MyTheme.lightTheme,
darkTheme: MyTheme.darkTheme,
themeMode: MyTheme.currentThemeMode(),
home: DesktopPortForwardScreen(params: argument),
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: supportedLocales,
navigatorObservers: const [
// FirebaseAnalyticsObserver(analytics: analytics),
],
builder: _keepScaleBuilder(),
);
}),
MyTheme.currentThemeMode(),
);
}
@ -206,21 +153,11 @@ void runConnectionManagerScreen() async {
// initialize window
WindowOptions windowOptions =
getHiddenTitleBarWindowOptions(size: kConnectionManagerWindowSize);
runApp(RefreshWrapper(builder: (context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
theme: MyTheme.lightTheme,
darkTheme: MyTheme.darkTheme,
themeMode: MyTheme.currentThemeMode(),
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: supportedLocales,
home: const DesktopServerPage(),
builder: _keepScaleBuilder());
}));
_runApp(
'',
const DesktopServerPage(),
MyTheme.currentThemeMode(),
);
windowManager.waitUntilReadyToShow(windowOptions, () async {
await windowManager.show();
// ensure initial window size to be changed
@ -235,23 +172,44 @@ void runConnectionManagerScreen() async {
});
}
void _runApp(
String title,
Widget home,
ThemeMode themeMode,
) {
final botToastBuilder = BotToastInit();
runApp(RefreshWrapper(
builder: (context) => GetMaterialApp(
navigatorKey: globalKey,
debugShowCheckedModeBanner: false,
title: title,
theme: MyTheme.lightTheme,
darkTheme: MyTheme.darkTheme,
themeMode: themeMode,
home: home,
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: supportedLocales,
navigatorObservers: [
// FirebaseAnalyticsObserver(analytics: analytics),
BotToastNavigatorObserver(),
],
builder: (context, child) {
child = _keepScaleBuilder(context, child);
child = botToastBuilder(context, child);
return child;
},
),
));
}
void runInstallPage() async {
await windowManager.ensureInitialized();
await initEnv(kAppTypeMain);
runApp(RefreshWrapper(
builder: (context) => GetMaterialApp(
debugShowCheckedModeBanner: false,
theme: MyTheme.lightTheme,
themeMode: ThemeMode.light,
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: supportedLocales,
home: const InstallPage(),
builder: _keepScaleBuilder()),
));
_runApp('', const InstallPage(), ThemeMode.light);
windowManager.waitUntilReadyToShow(
WindowOptions(size: Size(800, 600), center: true), () async {
windowManager.show();
@ -303,6 +261,7 @@ class _AppState extends State<App> {
@override
Widget build(BuildContext context) {
// final analytics = FirebaseAnalytics.instance;
final botToastBuilder = BotToastInit();
return RefreshWrapper(builder: (context) {
return MultiProvider(
providers: [
@ -325,15 +284,16 @@ class _AppState extends State<App> {
: !isAndroid
? WebHomePage()
: HomePage(),
navigatorObservers: const [
// FirebaseAnalyticsObserver(analytics: analytics),
],
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: supportedLocales,
navigatorObservers: [
// FirebaseAnalyticsObserver(analytics: analytics),
BotToastNavigatorObserver(),
],
builder: isAndroid
? (context, child) => AccessibilityListener(
child: MediaQuery(
@ -343,22 +303,24 @@ class _AppState extends State<App> {
child: child ?? Container(),
),
)
: _keepScaleBuilder(),
: (context, child) {
child = _keepScaleBuilder(context, child);
child = botToastBuilder(context, child);
return child;
},
),
);
});
}
}
_keepScaleBuilder() {
return (BuildContext context, Widget? child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(
textScaleFactor: 1.0,
),
child: child ?? Container(),
);
};
Widget _keepScaleBuilder(BuildContext context, Widget? child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(
textScaleFactor: 1.0,
),
child: child ?? Container(),
);
}
_registerEventHandler() {

View File

@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:math';
import 'dart:ui' as ui;
import 'package:flutter/gestures.dart';
import 'package:flutter/services.dart';
@ -11,7 +12,6 @@ import '../../models/platform_model.dart';
import '../common.dart';
import '../consts.dart';
import './state_model.dart';
import 'dart:ui' as ui;
/// Mouse button enum.
enum MouseButtons { left, right, wheel }

View File

@ -102,6 +102,7 @@ dependencies:
ref: 5be5113d59c753989dbf1106241379e3fd4c9b18
path: ^1.8.1
auto_size_text: ^3.0.0
bot_toast: ^4.0.3
dev_dependencies:
icons_launcher: ^2.0.4