Skip to content

Commit

Permalink
Add IOS support (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa authored Jul 4, 2024
2 parents 219b92d + 7b7685f commit 81822f1
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
38 changes: 38 additions & 0 deletions ios/.gitignore
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions ios/Classes/SystemThemePlugin.swift
Original file line number Diff line number Diff line change
@@ -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<String,Int>? {
// 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
}
}
23 changes: 23 additions & 0 deletions ios/system_theme.podspec
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions lib/system_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extension PlatformHelpers on TargetPlatform {
[
TargetPlatform.windows,
TargetPlatform.macOS,
TargetPlatform.iOS,
TargetPlatform.android,
TargetPlatform.linux,
].contains(this);
Expand All @@ -54,6 +55,8 @@ class SystemTheme {
/// - Windows
/// - Web
/// - Android
/// - iOS
/// - Mac
///
/// It returns [kDefaultFallbackColor] for unsupported platforms
static final SystemAccentColor accentColor = SystemAccentColor(fallbackColor)
Expand Down
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 81822f1

Please sign in to comment.