import 'package:animated_theme_switcher/animated_theme_switcher.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_translate/flutter_translate.dart'; import 'package:form_builder_validators/form_builder_validators.dart'; import 'package:provider/provider.dart'; import 'account_manager/account_manager.dart'; import 'init.dart'; import 'layout/splash.dart'; import 'router/router.dart'; import 'settings/settings.dart'; import 'theme/models/theme_preference.dart'; import 'tick.dart'; import 'tools/loggy.dart'; import 'veilid_processor/veilid_processor.dart'; class ReloadThemeIntent extends Intent { const ReloadThemeIntent(); } class VeilidChatApp extends StatelessWidget { const VeilidChatApp({ required this.initialThemeData, super.key, }); static const String name = 'VeilidChat'; final ThemeData initialThemeData; void _reloadTheme(BuildContext context) { log.info('Reloading theme'); final theme = PreferencesRepository.instance.value.themePreferences.themeData(); ThemeSwitcher.of(context).changeTheme(theme: theme); } Widget _buildShortcuts( {required BuildContext context, required Widget Function(BuildContext) builder}) => ThemeSwitcher( builder: (context) => Shortcuts( shortcuts: { LogicalKeySet( LogicalKeyboardKey.alt, LogicalKeyboardKey.keyR): const ReloadThemeIntent(), }, child: Actions(actions: >{ ReloadThemeIntent: CallbackAction( onInvoke: (intent) => _reloadTheme(context)), }, child: Focus(autofocus: true, child: builder(context))))); @override Widget build(BuildContext context) => FutureProvider( initialData: null, create: (context) async => VeilidChatGlobalInit.initialize(), builder: (context, child) { final globalInit = context.watch(); if (globalInit == null) { // Splash screen until we're done with init return const Splash(); } // Once init is done, we proceed with the app final localizationDelegate = LocalizedApp.of(context).delegate; return ThemeProvider( initTheme: initialThemeData, builder: (_, theme) => LocalizationProvider( state: LocalizationProvider.of(context).state, child: MultiBlocProvider( providers: [ BlocProvider( create: (context) => ConnectionStateCubit(ProcessorRepository.instance)), BlocProvider( create: (context) => RouterCubit(AccountRepository.instance), ), BlocProvider( create: (context) => LocalAccountsCubit(AccountRepository.instance), ), BlocProvider( create: (context) => UserLoginsCubit(AccountRepository.instance), ), BlocProvider( create: (context) => ActiveLocalAccountCubit(AccountRepository.instance), ), BlocProvider( create: (context) => PreferencesCubit(PreferencesRepository.instance), ) ], child: BackgroundTicker( child: _buildShortcuts( context: context, builder: (context) => MaterialApp.router( debugShowCheckedModeBanner: false, routerConfig: context.watch().router(), title: translate('app.title'), theme: theme, localizationsDelegates: [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, FormBuilderLocalizations.delegate, localizationDelegate ], supportedLocales: localizationDelegate.supportedLocales, locale: localizationDelegate.currentLocale, ))), )), ); }); @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); properties .add(DiagnosticsProperty('themeData', initialThemeData)); } }