Skip to content

Commit

Permalink
dynamic color support
Browse files Browse the repository at this point in the history
  • Loading branch information
laiiihz committed Jul 19, 2022
1 parent 4cd49d9 commit 477e3be
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 24 deletions.
36 changes: 23 additions & 13 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:alga/utils/hive_boxes/app_config_box.dart';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';

import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand All @@ -24,19 +25,28 @@ class MyApp extends StatelessWidget {
return ValueListenableBuilder(
valueListenable: AppConfigBox.keys(),
builder: (context, _, __) {
final theme = ThemeUtil(Colors.blue);
return MaterialApp.router(
onGenerateTitle: (context) => S.of(context).appName,
theme: theme.getTheme(Brightness.light),
darkTheme: theme.getTheme(Brightness.dark),
themeMode: AppConfigBox.themeMode,
routerDelegate: appRouter.routerDelegate,
routeInformationProvider: appRouter.routeInformationProvider,
routeInformationParser: appRouter.routeInformationParser,
localizationsDelegates: S.localizationsDelegates,
supportedLocales: S.supportedLocales,
locale: AppConfigBox.locale,
);
return DynamicColorBuilder(builder: (light, dark) {
ColorScheme? lightScheme;
ColorScheme? darkScheme;
if (light != null && dark != null) {
lightScheme = light.harmonized();
darkScheme = dark.harmonized();
}
lightScheme ??= kDefaultLightColorScheme;
darkScheme ??= kDefaultDarkColorScheme;
return MaterialApp.router(
onGenerateTitle: (context) => S.of(context).appName,
theme: ThemeUtil(lightScheme).getTheme(Brightness.light),
darkTheme: ThemeUtil(darkScheme).getTheme(Brightness.dark),
themeMode: AppConfigBox.themeMode,
routerDelegate: appRouter.routerDelegate,
routeInformationProvider: appRouter.routeInformationProvider,
routeInformationParser: appRouter.routeInformationParser,
localizationsDelegates: S.localizationsDelegates,
supportedLocales: S.supportedLocales,
locale: AppConfigBox.locale,
);
});
});
}
}
24 changes: 13 additions & 11 deletions lib/utils/theme_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ bool isLight(BuildContext context) => !isDark(context);
bool isSmallDevice(BuildContext context) =>
MediaQuery.of(context).size.width < 768;

final kDefaultLightColorScheme = ColorScheme.fromSeed(
seedColor: Colors.lightBlue,
brightness: Brightness.light,
);

final kDefaultDarkColorScheme = ColorScheme.fromSeed(
seedColor: Colors.lightBlue,
brightness: Brightness.dark,
);

class ThemeUtil {
final Color color;
late ColorScheme lightScheme;
late ColorScheme darkScheme;
ThemeUtil(this.color) {
lightScheme =
ColorScheme.fromSeed(seedColor: color, brightness: Brightness.light);
darkScheme =
ColorScheme.fromSeed(seedColor: color, brightness: Brightness.dark);
}
late ColorScheme colorScheme;
ThemeUtil(this.colorScheme);

static const _appBarTheme = AppBarTheme(elevation: 0);
static const _inputDecorationTheme = InputDecorationTheme(
Expand All @@ -30,8 +33,7 @@ class ThemeUtil {
);

ThemeData getTheme(Brightness brightness) {
ColorScheme scheme =
brightness == Brightness.light ? lightScheme : darkScheme;
ColorScheme scheme = colorScheme;
return ThemeData.from(colorScheme: scheme).copyWith(
splashFactory: InkSparkle.splashFactory,
appBarTheme: _appBarTheme.copyWith(
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import FlutterMacOS
import Foundation

import device_info_plus_macos
import dynamic_color
import file_selector_macos
import flutter_js
import hotkey_manager
Expand All @@ -17,6 +18,7 @@ import window_manager

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
DynamicColorPlugin.register(with: registry.registrar(forPlugin: "DynamicColorPlugin"))
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
FlutterJsPlugin.register(with: registry.registrar(forPlugin: "FlutterJsPlugin"))
HotkeyManagerPlugin.register(with: registry.registrar(forPlugin: "HotkeyManagerPlugin"))
Expand Down
6 changes: 6 additions & 0 deletions macos/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
PODS:
- device_info_plus_macos (0.0.1):
- FlutterMacOS
- dynamic_color (0.0.2):
- FlutterMacOS
- file_selector_macos (0.0.1):
- FlutterMacOS
- flutter_js (0.0.1):
Expand All @@ -23,6 +25,7 @@ PODS:

DEPENDENCIES:
- device_info_plus_macos (from `Flutter/ephemeral/.symlinks/plugins/device_info_plus_macos/macos`)
- dynamic_color (from `Flutter/ephemeral/.symlinks/plugins/dynamic_color/macos`)
- file_selector_macos (from `Flutter/ephemeral/.symlinks/plugins/file_selector_macos/macos`)
- flutter_js (from `Flutter/ephemeral/.symlinks/plugins/flutter_js/macos`)
- FlutterMacOS (from `Flutter/ephemeral`)
Expand All @@ -40,6 +43,8 @@ SPEC REPOS:
EXTERNAL SOURCES:
device_info_plus_macos:
:path: Flutter/ephemeral/.symlinks/plugins/device_info_plus_macos/macos
dynamic_color:
:path: Flutter/ephemeral/.symlinks/plugins/dynamic_color/macos
file_selector_macos:
:path: Flutter/ephemeral/.symlinks/plugins/file_selector_macos/macos
flutter_js:
Expand All @@ -61,6 +66,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
device_info_plus_macos: 1ad388a1ef433505c4038e7dd9605aadd1e2e9c7
dynamic_color: 394d6a888650f8534e029b27d2f8bc5c64e44008
file_selector_macos: f1b08a781e66103e3ba279fd5d4024a2478b3af6
flutter_js: c664361655af1d8fc24e278c7d086e9cbe0d68bc
FlutterMacOS: 57701585bf7de1b3fc2bb61f6378d73bbdea8424
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.0.1"
dynamic_color:
dependency: "direct main"
description:
name: dynamic_color
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.4.0"
extended_text_field:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ dependencies:
path: ^1.8.1
shelf: ^1.3.1
adaptive_breakpoints: ^0.1.4
dynamic_color: ^1.4.0

dev_dependencies:
flutter_test:
Expand Down
3 changes: 3 additions & 0 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "generated_plugin_registrant.h"

#include <dynamic_color/dynamic_color_plugin_c_api.h>
#include <file_selector_windows/file_selector_windows.h>
#include <flutter_js/flutter_js_plugin.h>
#include <hotkey_manager/hotkey_manager_plugin.h>
Expand All @@ -15,6 +16,8 @@
#include <window_manager/window_manager_plugin.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
DynamicColorPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("DynamicColorPluginCApi"));
FileSelectorWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FileSelectorWindows"));
FlutterJsPluginRegisterWithRegistrar(
Expand Down
1 change: 1 addition & 0 deletions windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
dynamic_color
file_selector_windows
flutter_js
hotkey_manager
Expand Down

0 comments on commit 477e3be

Please sign in to comment.