diff --git a/ios/.gitignore b/ios/.gitignore new file mode 100644 index 0000000..034771f --- /dev/null +++ b/ios/.gitignore @@ -0,0 +1,38 @@ +.idea/ +.vagrant/ +.sconsign.dblite +.svn/ + +.DS_Store +*.swp +profile + +DerivedData/ +build/ +GeneratedPluginRegistrant.h +GeneratedPluginRegistrant.m + +.generated/ + +*.pbxuser +*.mode1v3 +*.mode2v3 +*.perspectivev3 + +!default.pbxuser +!default.mode1v3 +!default.mode2v3 +!default.perspectivev3 + +xcuserdata + +*.moved-aside + +*.pyc +*sync/ +Icon? +.tags* + +/Flutter/Generated.xcconfig +/Flutter/ephemeral/ +/Flutter/flutter_export_environment.sh diff --git a/ios/Classes/SystemThemePlugin.swift b/ios/Classes/SystemThemePlugin.swift new file mode 100644 index 0000000..752cccc --- /dev/null +++ b/ios/Classes/SystemThemePlugin.swift @@ -0,0 +1,66 @@ +import Flutter +import SwiftUI + +public class SystemThemePlugin: NSObject, FlutterPlugin { + public static func register(with registrar: FlutterPluginRegistrar) { + let channel = FlutterMethodChannel(name: "system_theme", binaryMessenger: registrar.messenger()) + let instance = SystemThemePlugin() + registrar.addMethodCallDelegate(instance, channel: channel) + } + + public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { + switch call.method { + case "SystemTheme.accentColor": + if #available(iOS 14.0, *) { + var colors:[String:[String:Int]?] = [:] + colors["accent"] = Color.accentColor.toHex() + result(colors) + } else { + result(nil) + } + default: + result(FlutterMethodNotImplemented) + } + } +} + +@available(iOS 14.0, *) +extension Color { + // Function to convert SwiftUI Color to a Hex String + func toHex() -> Dictionary? { + // Extract the RGBA components from the Color + let components = self.cgColorComponents + // Check if components contain at least RGB + guard components.count >= 3 else { return nil } + + let r = components[0] + let g = components[1] + let b = components[2] + let a = components.count >= 4 ? components[3] : 1.0 + + return [ + "R":lroundf(Float(r * 255)), + "G":lroundf(Float(g * 255)), + "B":lroundf(Float(b * 255)), + "A":lroundf(Float(a * 255)) + ] + } + + // Helper function to extract RGBA components + private var cgColorComponents: [CGFloat] { + // Convert SwiftUI Color to UIColor + let uiColor = UIColor(self) + + // Get CGColor and its components + guard let components = uiColor.cgColor.components else { + return [] + } + + // Handle the case of grayscale color (1 component) + if components.count == 2 { + return [components[0], components[0], components[0], components[1]] + } + + return components + } +} diff --git a/ios/system_theme.podspec b/ios/system_theme.podspec new file mode 100644 index 0000000..3322ae4 --- /dev/null +++ b/ios/system_theme.podspec @@ -0,0 +1,23 @@ +# +# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html. +# Run `pod lib lint system_theme.podspec` to validate before publishing. +# +Pod::Spec.new do |s| + s.name = 'system_theme' + s.version = '0.0.1' + s.summary = 'A new Flutter plugin project.' + s.description = <<-DESC +A new Flutter plugin project. + DESC + s.homepage = 'http://example.com' + s.license = { :file => '../LICENSE' } + s.author = { 'Your Company' => 'email@example.com' } + s.source = { :path => '.' } + s.source_files = 'Classes/**/*' + s.dependency 'Flutter' + s.platform = :ios, '11.0' + + # Flutter.framework does not contain a i386 slice. + s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' } + s.swift_version = '5.0' +end diff --git a/lib/system_theme.dart b/lib/system_theme.dart index bb94d55..52fd530 100644 --- a/lib/system_theme.dart +++ b/lib/system_theme.dart @@ -28,6 +28,7 @@ extension PlatformHelpers on TargetPlatform { [ TargetPlatform.windows, TargetPlatform.macOS, + TargetPlatform.iOS, TargetPlatform.android, TargetPlatform.linux, ].contains(this); @@ -54,6 +55,8 @@ class SystemTheme { /// - Windows /// - Web /// - Android + /// - iOS + /// - Mac /// /// It returns [kDefaultFallbackColor] for unsupported platforms static final SystemAccentColor accentColor = SystemAccentColor(fallbackColor) diff --git a/pubspec.yaml b/pubspec.yaml index 96e06bf..a2c9e9f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -26,6 +26,9 @@ flutter: macos: package: com.bruno.system_theme pluginClass: SystemThemePlugin + ios: + package: com.bruno.system_theme + pluginClass: SystemThemePlugin android: package: com.bruno.system_theme pluginClass: SystemThemePlugin