From 39191c5ca468c5d8766db418bc273159e50c31fc Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Thu, 12 Oct 2023 23:40:19 +0200 Subject: [PATCH] fix: Fix widget positions --- lib/App.dart | 271 ++++++++++-------- lib/app_wrappers/CheckViewAlarmsLive.dart | 4 +- .../InitCurrentLocationFromSettings.dart | 13 +- lib/app_wrappers/ManageQuickActions.dart | 36 +-- .../RegisterBackgroundListeners.dart | 8 +- lib/app_wrappers/UniLinksHandler.dart | 27 +- lib/app_wrappers/UpdateLocationHistory.dart | 7 +- lib/main.dart | 17 +- .../location_history.dart | 2 +- .../manager_service/background_locator.dart | 6 +- 10 files changed, 206 insertions(+), 185 deletions(-) diff --git a/lib/App.dart b/lib/App.dart index e540f0df..da50e918 100644 --- a/lib/App.dart +++ b/lib/App.dart @@ -13,6 +13,17 @@ import 'package:locus/widgets/DismissKeyboard.dart'; import 'package:modal_bottom_sheet/modal_bottom_sheet.dart'; import 'package:provider/provider.dart'; +import 'app_wrappers/CheckViewAlarmsLive.dart'; +import 'app_wrappers/HandleNotifications.dart'; +import 'app_wrappers/InitCurrentLocationFromSettings.dart'; +import 'app_wrappers/ManageQuickActions.dart'; +import 'app_wrappers/PublishTaskPositionsOnUpdate.dart'; +import 'app_wrappers/RegisterBackgroundListeners.dart'; +import 'app_wrappers/ShowUpdateDialog.dart'; +import 'app_wrappers/UniLinksHandler.dart'; +import 'app_wrappers/UpdateLastLocationToSettings.dart'; +import 'app_wrappers/UpdateLocaleToSettings.dart'; +import 'app_wrappers/UpdateLocationHistory.dart'; import 'constants/themes.dart'; ColorScheme createColorScheme( @@ -51,83 +62,115 @@ class App extends StatelessWidget { child: DynamicColorBuilder( builder: (ColorScheme? lightColorScheme, ColorScheme? darkColorScheme) => - PlatformApp( - title: 'Locus', - material: (_, __) => MaterialAppData( - theme: (() { - if (lightColorScheme != null) { + Expanded( + child: PlatformApp( + title: 'Locus', + material: (_, __) => MaterialAppData( + theme: (() { + if (lightColorScheme != null) { + return LIGHT_THEME_MATERIAL.copyWith( + colorScheme: settings.primaryColor == null + ? lightColorScheme + : createColorScheme( + lightColorScheme, + settings.primaryColor!, + Brightness.light, + ), + primaryColor: + settings.primaryColor ?? lightColorScheme.primary, + ); + } + return LIGHT_THEME_MATERIAL.copyWith( colorScheme: settings.primaryColor == null - ? lightColorScheme + ? null : createColorScheme( - lightColorScheme, + lightColorScheme ?? + ColorScheme.fromSwatch( + primarySwatch: + createMaterialColor(settings.primaryColor!), + ), settings.primaryColor!, Brightness.light, ), - primaryColor: - settings.primaryColor ?? lightColorScheme.primary, + primaryColor: settings.primaryColor, ); - } - - return LIGHT_THEME_MATERIAL.copyWith( - colorScheme: settings.primaryColor == null - ? null - : createColorScheme( - lightColorScheme ?? - ColorScheme.fromSwatch( - primarySwatch: - createMaterialColor(settings.primaryColor!), - ), - settings.primaryColor!, - Brightness.light, - ), - primaryColor: settings.primaryColor, - ); - })(), - darkTheme: (() { - if (settings.getAndroidTheme() == AndroidTheme.miui) { - return DARK_THEME_MATERIAL_MIUI.copyWith( - colorScheme: settings.primaryColor == null - ? null - : createColorScheme( - const ColorScheme.dark(), - settings.primaryColor!, - Brightness.dark, + })(), + darkTheme: (() { + if (settings.getAndroidTheme() == AndroidTheme.miui) { + return DARK_THEME_MATERIAL_MIUI.copyWith( + colorScheme: settings.primaryColor == null + ? null + : createColorScheme( + const ColorScheme.dark(), + settings.primaryColor!, + Brightness.dark, + ), + primaryColor: settings.primaryColor, + elevatedButtonTheme: ElevatedButtonThemeData( + style: ElevatedButton.styleFrom( + backgroundColor: + settings.primaryColor ?? MIUI_PRIMARY_COLOR, + foregroundColor: Colors.white, + splashFactory: NoSplash.splashFactory, + textStyle: const TextStyle( + color: Colors.white, + fontSize: 18, + fontWeight: FontWeight.w700, ), - primaryColor: settings.primaryColor, - elevatedButtonTheme: ElevatedButtonThemeData( - style: ElevatedButton.styleFrom( - backgroundColor: - settings.primaryColor ?? MIUI_PRIMARY_COLOR, - foregroundColor: Colors.white, - splashFactory: NoSplash.splashFactory, - textStyle: const TextStyle( - color: Colors.white, - fontSize: 18, - fontWeight: FontWeight.w700, ), ), - ), - ); - } + ); + } + + if (darkColorScheme != null) { + return DARK_THEME_MATERIAL.copyWith( + colorScheme: settings.primaryColor == null + ? darkColorScheme + : createColorScheme( + darkColorScheme, + settings.primaryColor!, + Brightness.dark, + ), + primaryColor: + settings.primaryColor ?? darkColorScheme.primary, + scaffoldBackgroundColor: HSLColor.fromColor( + settings.primaryColor ?? darkColorScheme.background) + .withLightness(0.08) + .toColor(), + dialogBackgroundColor: settings.primaryColor == null + ? darkColorScheme.background + : HSLColor.fromColor(settings.primaryColor!) + .withLightness(0.15) + .toColor(), + inputDecorationTheme: + DARK_THEME_MATERIAL.inputDecorationTheme.copyWith( + fillColor: settings.primaryColor == null + ? null + : HSLColor.fromColor(settings.primaryColor!) + .withLightness(0.3) + .withSaturation(.5) + .toColor(), + ), + ); + } - if (darkColorScheme != null) { return DARK_THEME_MATERIAL.copyWith( colorScheme: settings.primaryColor == null - ? darkColorScheme + ? null : createColorScheme( - darkColorScheme, + const ColorScheme.dark(), settings.primaryColor!, Brightness.dark, ), - primaryColor: - settings.primaryColor ?? darkColorScheme.primary, - scaffoldBackgroundColor: HSLColor.fromColor( - settings.primaryColor ?? darkColorScheme.background) - .withLightness(0.08) - .toColor(), + primaryColor: settings.primaryColor, + scaffoldBackgroundColor: settings.primaryColor == null + ? null + : HSLColor.fromColor(settings.primaryColor!) + .withLightness(0.08) + .toColor(), dialogBackgroundColor: settings.primaryColor == null - ? darkColorScheme.background + ? null : HSLColor.fromColor(settings.primaryColor!) .withLightness(0.15) .toColor(), @@ -141,74 +184,60 @@ class App extends StatelessWidget { .toColor(), ), ); - } + })(), + themeMode: ThemeMode.system, + ), + cupertino: (_, __) => CupertinoAppData( + theme: settings.primaryColor == null + ? LIGHT_THEME_CUPERTINO + : LIGHT_THEME_CUPERTINO.copyWith( + primaryColor: settings.primaryColor, + ), + ), + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + builder: (context, child) => Stack( + children: [ + const UpdateLocationHistory(), + const UniLinksHandler(), + const UpdateLastLocationToSettings(), + const RegisterBackgroundListeners(), + const UpdateLocaleToSettings(), + const HandleNotifications(), + const CheckViewAlarmsLive(), + const ManageQuickActions(), + const InitCurrentLocationFromSettings(), + const ShowUpdateDialog(), + const PublishTaskPositionsOnUpdate(), + if (child != null) child, + ], + ), + onGenerateRoute: (routeSettings) { + final screen = (() { + if (settings.getRequireBiometricAuthenticationOnStart()) { + return const BiometricsRequiredStartupScreen(); + } - return DARK_THEME_MATERIAL.copyWith( - colorScheme: settings.primaryColor == null - ? null - : createColorScheme( - const ColorScheme.dark(), - settings.primaryColor!, - Brightness.dark, - ), - primaryColor: settings.primaryColor, - scaffoldBackgroundColor: settings.primaryColor == null - ? null - : HSLColor.fromColor(settings.primaryColor!) - .withLightness(0.08) - .toColor(), - dialogBackgroundColor: settings.primaryColor == null - ? null - : HSLColor.fromColor(settings.primaryColor!) - .withLightness(0.15) - .toColor(), - inputDecorationTheme: - DARK_THEME_MATERIAL.inputDecorationTheme.copyWith( - fillColor: settings.primaryColor == null - ? null - : HSLColor.fromColor(settings.primaryColor!) - .withLightness(0.3) - .withSaturation(.5) - .toColor(), - ), - ); - })(), - themeMode: ThemeMode.system, - ), - cupertino: (_, __) => CupertinoAppData( - theme: settings.primaryColor == null - ? LIGHT_THEME_CUPERTINO - : LIGHT_THEME_CUPERTINO.copyWith( - primaryColor: settings.primaryColor, - ), - ), - localizationsDelegates: AppLocalizations.localizationsDelegates, - supportedLocales: AppLocalizations.supportedLocales, - onGenerateRoute: (routeSettings) { - final screen = (() { - if (settings.getRequireBiometricAuthenticationOnStart()) { - return const BiometricsRequiredStartupScreen(); - } + if (!settings.userHasSeenWelcomeScreen) { + return const WelcomeScreen(); + } - if (!settings.userHasSeenWelcomeScreen) { - return const WelcomeScreen(); - } + return const LocationsOverviewScreen(); + })(); - return const LocationsOverviewScreen(); - })(); + if (isCupertino(context)) { + return MaterialWithModalsPageRoute( + builder: (_) => screen, + settings: routeSettings, + ); + } - if (isCupertino(context)) { - return MaterialWithModalsPageRoute( + return NativePageRoute( builder: (_) => screen, - settings: routeSettings, + context: context, ); - } - - return NativePageRoute( - builder: (_) => screen, - context: context, - ); - }, + }, + ), ), ), ); diff --git a/lib/app_wrappers/CheckViewAlarmsLive.dart b/lib/app_wrappers/CheckViewAlarmsLive.dart index 3131a6da..8a1e55be 100644 --- a/lib/app_wrappers/CheckViewAlarmsLive.dart +++ b/lib/app_wrappers/CheckViewAlarmsLive.dart @@ -19,14 +19,14 @@ class CheckViewAlarmsLive extends StatefulWidget { } class _CheckViewAlarmsLiveState extends State { - late final CurrentLocationService _currentLocation; late final StreamSubscription _subscription; @override void initState() { super.initState(); - _subscription = _currentLocation.stream.listen((position) async { + final currentLocation = context.read(); + _subscription = currentLocation.stream.listen((position) async { final l10n = AppLocalizations.of(context); final viewService = context.read(); final userLocation = await LocationPointService.fromPosition(position); diff --git a/lib/app_wrappers/InitCurrentLocationFromSettings.dart b/lib/app_wrappers/InitCurrentLocationFromSettings.dart index ad669888..7f1770ec 100644 --- a/lib/app_wrappers/InitCurrentLocationFromSettings.dart +++ b/lib/app_wrappers/InitCurrentLocationFromSettings.dart @@ -21,12 +21,15 @@ class _InitCurrentLocationFromSettingsState super.initState(); _currentLocation = context.read(); - final settings = context.read(); - final lastLocation = settings.getLastMapLocation(); - if (lastLocation != null) { - _setLocation(lastLocation); - } + WidgetsBinding.instance.addPostFrameCallback((_) { + final settings = context.read(); + final lastLocation = settings.getLastMapLocation(); + + if (lastLocation != null) { + _setLocation(lastLocation); + } + }); } void _setLocation(final SettingsLastMapLocation rawLocation) { diff --git a/lib/app_wrappers/ManageQuickActions.dart b/lib/app_wrappers/ManageQuickActions.dart index 2f2be0b6..9e37152a 100644 --- a/lib/app_wrappers/ManageQuickActions.dart +++ b/lib/app_wrappers/ManageQuickActions.dart @@ -36,13 +36,15 @@ class _ManageQuickActionsState extends State { void initState() { super.initState(); - final settings = context.read(); + WidgetsBinding.instance.addPostFrameCallback((_) { + final settings = context.read(); - if (settings.userHasSeenWelcomeScreen) { - _registerActions(); - } else { - _removeActions(); - } + if (settings.userHasSeenWelcomeScreen) { + _registerActions(); + } else { + _removeActions(); + } + }); } void _registerActions() { @@ -62,24 +64,22 @@ class _ManageQuickActionsState extends State { showCupertinoModalBottomSheet( context: context, backgroundColor: Colors.transparent, - builder: (_) => - ShortcutScreen( - type: ShortcutType.values.firstWhere( - (element) => element.name == type, - ), - ), + builder: (_) => ShortcutScreen( + type: ShortcutType.values.firstWhere( + (element) => element.name == type, + ), + ), ); } else { Navigator.push( context, NativePageRoute( context: context, - builder: (_) => - ShortcutScreen( - type: ShortcutType.values.firstWhere( - (element) => element.name == type, - ), - ), + builder: (_) => ShortcutScreen( + type: ShortcutType.values.firstWhere( + (element) => element.name == type, + ), + ), ), ); } diff --git a/lib/app_wrappers/RegisterBackgroundListeners.dart b/lib/app_wrappers/RegisterBackgroundListeners.dart index 92c0b41f..e8a5aab1 100644 --- a/lib/app_wrappers/RegisterBackgroundListeners.dart +++ b/lib/app_wrappers/RegisterBackgroundListeners.dart @@ -27,10 +27,12 @@ class _RegisterBackgroundListenersState _settings = context.read(); _taskService = context.read(); - _settings.addListener(_updateListeners); - _taskService.addListener(_updateListeners); + WidgetsBinding.instance.addPostFrameCallback((_) { + _settings.addListener(_updateListeners); + _taskService.addListener(_updateListeners); - _updateListeners(); + _updateListeners(); + }); } @override diff --git a/lib/app_wrappers/UniLinksHandler.dart b/lib/app_wrappers/UniLinksHandler.dart index 6f84639e..8022c19a 100644 --- a/lib/app_wrappers/UniLinksHandler.dart +++ b/lib/app_wrappers/UniLinksHandler.dart @@ -37,7 +37,9 @@ class _UniLinksHandlerState extends State { } }); - _initInitialLink(); + WidgetsBinding.instance.addPostFrameCallback((_) { + _initInitialLink(); + }); } @override @@ -96,18 +98,19 @@ class _UniLinksHandlerState extends State { showPlatformDialog( context: context, - builder: (_) => PlatformAlertDialog( - title: Text(l10n.uniLinksOpenError), - content: Text(error.message ?? l10n.unknownError), - actions: [ - PlatformDialogAction( - child: Text(l10n.closeNeutralAction), - onPressed: () { - Navigator.of(context).pop(); - }, + builder: (_) => + PlatformAlertDialog( + title: Text(l10n.uniLinksOpenError), + content: Text(error.message ?? l10n.unknownError), + actions: [ + PlatformDialogAction( + child: Text(l10n.closeNeutralAction), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ], ), - ], - ), ); } } diff --git a/lib/app_wrappers/UpdateLocationHistory.dart b/lib/app_wrappers/UpdateLocationHistory.dart index 4a40069d..67211fa6 100644 --- a/lib/app_wrappers/UpdateLocationHistory.dart +++ b/lib/app_wrappers/UpdateLocationHistory.dart @@ -15,16 +15,15 @@ class UpdateLocationHistory extends StatefulWidget { } class _UpdateLocationHistoryState extends State { - late final CurrentLocationService _currentLocation; late final StreamSubscription _subscription; - late final LocationHistory _locationHistory; @override void initState() { super.initState(); - _currentLocation = context.read(); - _subscription = _currentLocation.stream.listen(_locationHistory.add); + final currentLocation = context.read(); + final locationHistory = context.read(); + _subscription = currentLocation.stream.listen(locationHistory.add); } @override diff --git a/lib/main.dart b/lib/main.dart index 1dccb720..156c6f84 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -105,22 +105,7 @@ void main() async { create: (_) => CurrentLocationService()), ChangeNotifierProvider(create: (_) => locationHistory), ], - child: const Column( - children: [ - UpdateLocationHistory(), - UniLinksHandler(), - UpdateLastLocationToSettings(), - RegisterBackgroundListeners(), - UpdateLocaleToSettings(), - HandleNotifications(), - CheckViewAlarmsLive(), - ManageQuickActions(), - InitCurrentLocationFromSettings(), - ShowUpdateDialog(), - PublishTaskPositionsOnUpdate(), - App(), - ], - ), + child: const App(), ), ); } diff --git a/lib/services/location_history_service/location_history.dart b/lib/services/location_history_service/location_history.dart index 119ddba5..d143512f 100644 --- a/lib/services/location_history_service/location_history.dart +++ b/lib/services/location_history_service/location_history.dart @@ -32,7 +32,7 @@ class LocationHistory extends ChangeNotifier { return LocationHistory(null); } - return LocationHistory.fromJSON(data as Map); + return LocationHistory.fromJSON(jsonDecode(data) as Map); } // To avoid too many crumbled locations, we only save locations that are at diff --git a/lib/services/manager_service/background_locator.dart b/lib/services/manager_service/background_locator.dart index 61d0e492..6a885c79 100644 --- a/lib/services/manager_service/background_locator.dart +++ b/lib/services/manager_service/background_locator.dart @@ -68,7 +68,7 @@ Future configureBackgroundLocator() { return BackgroundLocator.initialize(); } -Future registerBackgroundLocator(final BuildContext context,) { +Future registerBackgroundLocator(final BuildContext context) { final l10n = AppLocalizations.of(context); FlutterLogs.logInfo( @@ -83,7 +83,7 @@ Future registerBackgroundLocator(final BuildContext context,) { androidSettings: AndroidSettings( accuracy: LocationAccuracy.HIGH, distanceFilter: - BACKGROUND_LOCATION_UPDATES_MINIMUM_DISTANCE_FILTER.toDouble(), + BACKGROUND_LOCATION_UPDATES_MINIMUM_DISTANCE_FILTER.toDouble(), client: isGMSFlavor ? LocationClient.google : LocationClient.android, androidNotificationSettings: AndroidNotificationSettings( notificationTitle: l10n.backgroundLocator_title, @@ -95,7 +95,7 @@ Future registerBackgroundLocator(final BuildContext context,) { ), iosSettings: IOSSettings( distanceFilter: - BACKGROUND_LOCATION_UPDATES_MINIMUM_DISTANCE_FILTER.toDouble(), + BACKGROUND_LOCATION_UPDATES_MINIMUM_DISTANCE_FILTER.toDouble(), accuracy: LocationAccuracy.HIGH, showsBackgroundLocationIndicator: true, stopWithTerminate: false,