From 6899d311954fb9e802dacf0c97738564b55a960c Mon Sep 17 00:00:00 2001 From: Fabian Freund Date: Mon, 17 Jun 2024 18:14:24 +0200 Subject: [PATCH] use material dynamic color scheme --- app/lib/core/error_observer.dart | 16 +++ app/lib/main.dart | 114 +++--------------- app/lib/presentation/widgets/main_app.dart | 84 +++++++++++++ .../flutter/generated_plugin_registrant.cc | 4 + app/linux/flutter/generated_plugins.cmake | 1 + .../Flutter/GeneratedPluginRegistrant.swift | 2 + app/pubspec.lock | 53 +++++++- app/pubspec.yaml | 1 + .../flutter/generated_plugin_registrant.cc | 3 + app/windows/flutter/generated_plugins.cmake | 1 + 10 files changed, 180 insertions(+), 99 deletions(-) create mode 100644 app/lib/core/error_observer.dart create mode 100644 app/lib/presentation/widgets/main_app.dart diff --git a/app/lib/core/error_observer.dart b/app/lib/core/error_observer.dart new file mode 100644 index 00000000..e8fa201b --- /dev/null +++ b/app/lib/core/error_observer.dart @@ -0,0 +1,16 @@ +import 'package:bang_navigator/core/logger.dart'; +import 'package:riverpod/riverpod.dart'; + +class ErrorObserver extends ProviderObserver { + const ErrorObserver(); + + @override + void providerDidFail( + ProviderBase provider, + Object error, + StackTrace stackTrace, + ProviderContainer container, + ) { + logger.e('Provider $provider threw $error at $stackTrace'); + } +} diff --git a/app/lib/main.dart b/app/lib/main.dart index 19ee5bcb..de8e1bb9 100644 --- a/app/lib/main.dart +++ b/app/lib/main.dart @@ -1,27 +1,12 @@ -import 'package:bang_navigator/core/logger.dart'; -import 'package:bang_navigator/core/providers.dart'; +import 'package:bang_navigator/core/error_observer.dart'; import 'package:bang_navigator/domain/services/app_initialization.dart'; import 'package:bang_navigator/presentation/hooks/on_initialization.dart'; -import 'package:bang_navigator/presentation/widgets/failure_widget.dart'; +import 'package:bang_navigator/presentation/widgets/main_app.dart'; +import 'package:dynamic_color/dynamic_color.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:home_widget/home_widget.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; -class _ErrorObserver extends ProviderObserver { - const _ErrorObserver(); - - @override - void providerDidFail( - ProviderBase provider, - Object error, - StackTrace stackTrace, - ProviderContainer container, - ) { - logger.e('Provider $provider threw $error at $stackTrace'); - } -} - void main() async { WidgetsFlutterBinding.ensureInitialized(); @@ -33,7 +18,7 @@ void main() async { runApp( ProviderScope( - observers: const [_ErrorObserver()], + observers: const [ErrorObserver()], child: HookConsumer( builder: (context, ref, child) { useOnInitialization( @@ -44,84 +29,23 @@ void main() async { }, ); - return const MainApp(); + return DynamicColorBuilder( + builder: (lightDynamic, darkDynamic) { + return MainApp( + theme: ThemeData( + useMaterial3: true, + colorScheme: lightDynamic?.harmonized(), + ), + darkTheme: ThemeData( + useMaterial3: true, + colorScheme: darkDynamic?.harmonized(), + ), + themeMode: ThemeMode.dark, + ); + }, + ); }, ), ), ); } - -class MainApp extends HookConsumerWidget { - const MainApp({super.key}); - - @override - Widget build(BuildContext context, WidgetRef ref) { - final initializationResult = ref.watch(appInitializationServiceProvider); - final router = ref.watch(routerProvider); - - final themeData = useMemoized( - () => ThemeData( - useMaterial3: true, - brightness: Brightness.dark, - // colorScheme: ColorScheme.fromSeed( - // seedColor: const Color(0xFFFFB319), - // brightness: Brightness.dark, - // ), - ), - ); - - return initializationResult.fold( - (initializationState) { - if (!initializationState.initialized) { - return MaterialApp( - debugShowCheckedModeBanner: false, - theme: themeData, - home: Scaffold( - body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const CircularProgressIndicator(), - if (initializationState.stage != null) - Padding( - padding: const EdgeInsets.only(top: 8.0), - child: Text(initializationState.stage!), - ), - ], - ), - ), - ), - ); - } - - return MaterialApp.router( - debugShowCheckedModeBanner: false, - theme: themeData, - routerConfig: router, - ); - }, - onFailure: (errorMessage) { - return MaterialApp( - debugShowCheckedModeBanner: false, - theme: themeData, - home: Scaffold( - appBar: AppBar( - title: const Text('Initiallization Error'), - ), - body: Center( - child: FailureWidget( - title: 'Could not initialize App', - exception: errorMessage.toString(), - onRetry: () async { - await ref - .read(appInitializationServiceProvider.notifier) - .reinitialize(); - }, - ), - ), - ), - ); - }, - ); - } -} diff --git a/app/lib/presentation/widgets/main_app.dart b/app/lib/presentation/widgets/main_app.dart new file mode 100644 index 00000000..2fb2083d --- /dev/null +++ b/app/lib/presentation/widgets/main_app.dart @@ -0,0 +1,84 @@ +import 'package:bang_navigator/core/providers.dart'; +import 'package:bang_navigator/domain/services/app_initialization.dart'; +import 'package:bang_navigator/presentation/widgets/failure_widget.dart'; +import 'package:flutter/material.dart'; +import 'package:hooks_riverpod/hooks_riverpod.dart'; + +class MainApp extends HookConsumerWidget { + final ThemeData? theme; + final ThemeData? darkTheme; + final ThemeMode? themeMode; + + const MainApp({ + required this.theme, + required this.darkTheme, + required this.themeMode, + super.key, + }); + + @override + Widget build(BuildContext context, WidgetRef ref) { + final initializationResult = ref.watch(appInitializationServiceProvider); + final router = ref.watch(routerProvider); + + return initializationResult.fold( + (initializationState) { + if (!initializationState.initialized) { + return MaterialApp( + debugShowCheckedModeBanner: false, + theme: theme, + darkTheme: darkTheme, + themeMode: themeMode, + home: Scaffold( + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const CircularProgressIndicator(), + if (initializationState.stage != null) + Padding( + padding: const EdgeInsets.only(top: 8.0), + child: Text(initializationState.stage!), + ), + ], + ), + ), + ), + ); + } + + return MaterialApp.router( + debugShowCheckedModeBanner: false, + theme: theme, + darkTheme: darkTheme, + themeMode: themeMode, + routerConfig: router, + ); + }, + onFailure: (errorMessage) { + return MaterialApp( + debugShowCheckedModeBanner: false, + theme: theme, + darkTheme: darkTheme, + themeMode: themeMode, + home: Scaffold( + appBar: AppBar( + title: const Text('Initiallization Error'), + ), + body: Center( + child: FailureWidget( + title: 'Could not initialize App', + exception: errorMessage.toString(), + onRetry: () async { + await ref + .read(appInitializationServiceProvider.notifier) + .reinitialize(); + }, + ), + ), + ), + ); + }, + ); + } +} diff --git a/app/linux/flutter/generated_plugin_registrant.cc b/app/linux/flutter/generated_plugin_registrant.cc index a35cce61..18a72331 100644 --- a/app/linux/flutter/generated_plugin_registrant.cc +++ b/app/linux/flutter/generated_plugin_registrant.cc @@ -6,11 +6,15 @@ #include "generated_plugin_registrant.h" +#include #include #include #include void fl_register_plugins(FlPluginRegistry* registry) { + g_autoptr(FlPluginRegistrar) dynamic_color_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "DynamicColorPlugin"); + dynamic_color_plugin_register_with_registrar(dynamic_color_registrar); g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar = fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin"); flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar); diff --git a/app/linux/flutter/generated_plugins.cmake b/app/linux/flutter/generated_plugins.cmake index 2aa89bb0..f9162541 100644 --- a/app/linux/flutter/generated_plugins.cmake +++ b/app/linux/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + dynamic_color flutter_secure_storage_linux sqlite3_flutter_libs url_launcher_linux diff --git a/app/macos/Flutter/GeneratedPluginRegistrant.swift b/app/macos/Flutter/GeneratedPluginRegistrant.swift index f4f0342c..019aabe4 100644 --- a/app/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/app/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,6 +5,7 @@ import FlutterMacOS import Foundation +import dynamic_color import flutter_inappwebview_macos import flutter_secure_storage_macos import package_info_plus @@ -15,6 +16,7 @@ import sqlite3_flutter_libs import url_launcher_macos func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + DynamicColorPlugin.register(with: registry.registrar(forPlugin: "DynamicColorPlugin")) InAppWebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "InAppWebViewFlutterPlugin")) FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin")) FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin")) diff --git a/app/pubspec.lock b/app/pubspec.lock index 22bc3ebf..2b405aab 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -297,6 +297,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.18.0" + dynamic_color: + dependency: "direct main" + description: + name: dynamic_color + sha256: eae98052fa6e2826bdac3dd2e921c6ce2903be15c6b7f8b6d8a5d49b5086298d + url: "https://pub.dev" + source: hosted + version: "1.7.0" exceptions: dependency: "direct main" description: @@ -313,6 +321,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.17" + fake_async: + dependency: transitive + description: + name: fake_async + sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78" + url: "https://pub.dev" + source: hosted + version: "1.3.1" fast_equatable: dependency: "direct main" description: @@ -534,6 +550,11 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.1" + flutter_test: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" flutter_web_plugins: dependency: transitive description: flutter @@ -691,6 +712,30 @@ packages: url: "https://pub.dev" source: hosted version: "6.8.0" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a" + url: "https://pub.dev" + source: hosted + version: "10.0.4" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8" + url: "https://pub.dev" + source: hosted + version: "3.0.3" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3" + url: "https://pub.dev" + source: hosted + version: "3.0.1" lint: dependency: "direct dev" description: @@ -799,10 +844,10 @@ packages: dependency: transitive description: name: path_provider_android - sha256: "9c96da072b421e98183f9ea7464898428e764bc0ce5567f27ec8693442e72514" + sha256: bca87b0165ffd7cdb9cad8edd22d18d2201e886d9a9f19b4fb3452ea7df3a72a url: "https://pub.dev" source: hosted - version: "2.2.5" + version: "2.2.6" path_provider_foundation: dependency: transitive description: @@ -1284,10 +1329,10 @@ packages: dependency: transitive description: name: vm_service - sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc + sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec" url: "https://pub.dev" source: hosted - version: "14.2.4" + version: "14.2.1" watcher: dependency: "direct main" description: diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 9601ae8a..31a52fc6 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -11,6 +11,7 @@ dependencies: collection: ^1.18.0 copy_with_extension: ^5.0.4 drift: ^2.18.0 + dynamic_color: ^1.7.0 exceptions: ^0.6.0 expandable_page_view: ^1.0.17 fast_equatable: ^1.1.0 diff --git a/app/windows/flutter/generated_plugin_registrant.cc b/app/windows/flutter/generated_plugin_registrant.cc index 6eedea70..c5485f98 100644 --- a/app/windows/flutter/generated_plugin_registrant.cc +++ b/app/windows/flutter/generated_plugin_registrant.cc @@ -6,12 +6,15 @@ #include "generated_plugin_registrant.h" +#include #include #include #include #include void RegisterPlugins(flutter::PluginRegistry* registry) { + DynamicColorPluginCApiRegisterWithRegistrar( + registry->GetRegistrarForPlugin("DynamicColorPluginCApi")); FlutterSecureStorageWindowsPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("FlutterSecureStorageWindowsPlugin")); SharePlusWindowsPluginCApiRegisterWithRegistrar( diff --git a/app/windows/flutter/generated_plugins.cmake b/app/windows/flutter/generated_plugins.cmake index 983afec1..81bf1d63 100644 --- a/app/windows/flutter/generated_plugins.cmake +++ b/app/windows/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + dynamic_color flutter_secure_storage_windows share_plus sqlite3_flutter_libs