refactor: App widget

This commit is contained in:
Christian Pauly 2022-08-25 18:31:30 +02:00
parent ad062c8c83
commit a67b9ccad1
4 changed files with 125 additions and 129 deletions

View File

@ -2,28 +2,15 @@ import 'dart:async';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter_app_lock/flutter_app_lock.dart'; import 'package:flutter_app_lock/flutter_app_lock.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/matrix.dart';
import 'package:universal_html/html.dart' as html; import 'package:universal_html/html.dart' as html;
import 'package:vrouter/vrouter.dart';
import 'package:fluffychat/config/routes.dart';
import 'package:fluffychat/utils/client_manager.dart'; import 'package:fluffychat/utils/client_manager.dart';
import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/utils/platform_infos.dart';
import 'config/app_config.dart';
import 'config/themes.dart';
import 'utils/background_push.dart'; import 'utils/background_push.dart';
import 'utils/custom_scroll_behaviour.dart'; import 'widgets/fluffy_chat_app.dart';
import 'utils/localized_exception_extension.dart';
import 'utils/space_navigator.dart';
import 'widgets/lock_screen.dart'; import 'widgets/lock_screen.dart';
import 'widgets/matrix.dart';
void main() async { void main() async {
// Our background push shared isolate accesses flutter-internal things very early in the startup proccess // Our background push shared isolate accesses flutter-internal things very early in the startup proccess
@ -58,119 +45,9 @@ void main() async {
lockScreen: const LockScreen(), lockScreen: const LockScreen(),
enabled: false, enabled: false,
) )
: FluffyChatApp(clients: clients, queryParameters: queryParameters), : FluffyChatApp(
clients: clients,
queryParameters: queryParameters,
),
); );
} }
class FluffyChatApp extends StatefulWidget {
final Widget? testWidget;
final List<Client> clients;
final Map<String, String>? queryParameters;
const FluffyChatApp({
Key? key,
this.testWidget,
required this.clients,
this.queryParameters,
}) : super(key: key);
/// getInitialLink may rereturn the value multiple times if this view is
/// opened multiple times for example if the user logs out after they logged
/// in with qr code or magic link.
static bool gotInitialLink = false;
@override
FluffyChatAppState createState() => FluffyChatAppState();
}
class FluffyChatAppState extends State<FluffyChatApp> {
GlobalKey<VRouterState>? _router;
bool? columnMode;
String? _initialUrl;
@override
void initState() {
super.initState();
_initialUrl =
widget.clients.any((client) => client.isLogged()) ? '/rooms' : '/home';
}
@override
Widget build(BuildContext context) {
return DynamicColorBuilder(
builder: (lightColorScheme, darkColorScheme) => AdaptiveTheme(
light: FluffyThemes.buildTheme(
Brightness.light,
lightColorScheme,
),
dark: FluffyThemes.buildTheme(
Brightness.dark,
lightColorScheme,
),
initial: AdaptiveThemeMode.system,
builder: (theme, darkTheme) => LayoutBuilder(
builder: (context, constraints) {
const maxColumns = 3;
var newColumns =
(constraints.maxWidth / FluffyThemes.columnWidth).floor();
if (newColumns > maxColumns) newColumns = maxColumns;
columnMode ??= newColumns > 1;
_router ??= GlobalKey<VRouterState>();
if (columnMode != newColumns > 1) {
Logs().v('Set Column Mode = $columnMode');
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
_initialUrl = _router?.currentState?.url;
columnMode = newColumns > 1;
_router = GlobalKey<VRouterState>();
});
});
}
return VRouter(
key: _router,
title: AppConfig.applicationName,
theme: theme,
scrollBehavior: CustomScrollBehavior(),
logs: kReleaseMode ? VLogs.none : VLogs.info,
darkTheme: darkTheme,
localizationsDelegates: const [
...L10n.localizationsDelegates,
],
navigatorObservers: [
SpaceNavigator.routeObserver,
],
supportedLocales: L10n.supportedLocales,
initialUrl: _initialUrl ?? '/',
routes: AppRoutes(columnMode ?? false).routes,
builder: (context, child) {
LoadingDialog.defaultTitle =
L10n.of(context)!.loadingPleaseWait;
LoadingDialog.defaultBackLabel = L10n.of(context)!.close;
LoadingDialog.defaultOnError =
(e) => (e as Object?)!.toLocalizedString(context);
WidgetsBinding.instance.addPostFrameCallback((_) {
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
systemNavigationBarColor:
Theme.of(context).appBarTheme.backgroundColor,
systemNavigationBarIconBrightness:
Theme.of(context).brightness == Brightness.light
? Brightness.dark
: Brightness.light,
),
);
});
return Matrix(
context: context,
router: _router,
clients: widget.clients,
child: child,
);
},
);
},
),
));
}
}

View File

@ -20,9 +20,9 @@ import 'package:fluffychat/utils/localized_exception_extension.dart';
import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/space_navigator.dart'; import 'package:fluffychat/utils/space_navigator.dart';
import '../../../utils/account_bundles.dart'; import '../../../utils/account_bundles.dart';
import '../../main.dart';
import '../../utils/matrix_sdk_extensions.dart/matrix_file_extension.dart'; import '../../utils/matrix_sdk_extensions.dart/matrix_file_extension.dart';
import '../../utils/url_launcher.dart'; import '../../utils/url_launcher.dart';
import '../../widgets/fluffy_chat_app.dart';
import '../../widgets/matrix.dart'; import '../../widgets/matrix.dart';
import '../bootstrap/bootstrap_dialog.dart'; import '../bootstrap/bootstrap_dialog.dart';
import '../settings_account/settings_account.dart'; import '../settings_account/settings_account.dart';

View File

@ -0,0 +1,107 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:matrix/matrix.dart';
import 'package:vrouter/vrouter.dart';
import 'package:fluffychat/config/routes.dart';
import 'package:fluffychat/config/themes.dart';
import '../config/app_config.dart';
import '../utils/custom_scroll_behaviour.dart';
import '../utils/space_navigator.dart';
import 'matrix.dart';
class FluffyChatApp extends StatefulWidget {
final Widget? testWidget;
final List<Client> clients;
final Map<String, String>? queryParameters;
const FluffyChatApp({
Key? key,
this.testWidget,
required this.clients,
this.queryParameters,
}) : super(key: key);
/// getInitialLink may rereturn the value multiple times if this view is
/// opened multiple times for example if the user logs out after they logged
/// in with qr code or magic link.
static bool gotInitialLink = false;
@override
FluffyChatAppState createState() => FluffyChatAppState();
}
class FluffyChatAppState extends State<FluffyChatApp> {
GlobalKey<VRouterState>? _router;
bool? columnMode;
String? _initialUrl;
@override
void initState() {
super.initState();
_initialUrl =
widget.clients.any((client) => client.isLogged()) ? '/rooms' : '/home';
}
@override
Widget build(BuildContext context) {
return DynamicColorBuilder(
builder: (lightColorScheme, darkColorScheme) => AdaptiveTheme(
light: FluffyThemes.buildTheme(
Brightness.light,
lightColorScheme,
),
dark: FluffyThemes.buildTheme(
Brightness.dark,
lightColorScheme,
),
initial: AdaptiveThemeMode.system,
builder: (theme, darkTheme) => LayoutBuilder(
builder: (context, constraints) {
const maxColumns = 3;
var newColumns =
(constraints.maxWidth / FluffyThemes.columnWidth).floor();
if (newColumns > maxColumns) newColumns = maxColumns;
columnMode ??= newColumns > 1;
_router ??= GlobalKey<VRouterState>();
if (columnMode != newColumns > 1) {
Logs().v('Set Column Mode = $columnMode');
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
_initialUrl = _router?.currentState?.url;
columnMode = newColumns > 1;
_router = GlobalKey<VRouterState>();
});
});
}
return VRouter(
key: _router,
title: AppConfig.applicationName,
theme: theme,
scrollBehavior: CustomScrollBehavior(),
logs: kReleaseMode ? VLogs.none : VLogs.info,
darkTheme: darkTheme,
localizationsDelegates: L10n.localizationsDelegates,
navigatorObservers: [
SpaceNavigator.routeObserver,
],
supportedLocales: L10n.supportedLocales,
initialUrl: _initialUrl ?? '/',
routes: AppRoutes(columnMode ?? false).routes,
builder: (context, child) => Matrix(
context: context,
router: _router,
clients: widget.clients,
child: child,
),
);
},
),
),
);
}
}

View File

@ -11,6 +11,7 @@ import 'package:desktop_notifications/desktop_notifications.dart';
import 'package:flutter_app_lock/flutter_app_lock.dart'; import 'package:flutter_app_lock/flutter_app_lock.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:image_picker/image_picker.dart'; import 'package:image_picker/image_picker.dart';
import 'package:matrix/encryption.dart'; import 'package:matrix/encryption.dart';
@ -23,6 +24,7 @@ import 'package:vrouter/vrouter.dart';
import 'package:fluffychat/config/themes.dart'; import 'package:fluffychat/config/themes.dart';
import 'package:fluffychat/utils/client_manager.dart'; import 'package:fluffychat/utils/client_manager.dart';
import 'package:fluffychat/utils/localized_exception_extension.dart';
import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/uia_request_manager.dart'; import 'package:fluffychat/utils/uia_request_manager.dart';
import 'package:fluffychat/utils/voip_plugin.dart'; import 'package:fluffychat/utils/voip_plugin.dart';
@ -256,6 +258,16 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
} else { } else {
initSettings(); initSettings();
} }
initLoadingDialog();
}
void initLoadingDialog() {
WidgetsBinding.instance.addPostFrameCallback((_) {
LoadingDialog.defaultTitle = L10n.of(context)!.loadingPleaseWait;
LoadingDialog.defaultBackLabel = L10n.of(context)!.close;
LoadingDialog.defaultOnError =
(e) => (e as Object?)!.toLocalizedString(context);
});
} }
Future<void> initConfig() async { Future<void> initConfig() async {