Skip to content

Commit 1ddff86

Browse files
authored
Added TargetPlatform.supportsAccentColor helper (#26)
2 parents 8e66fa0 + da9a4a5 commit 1ddff86

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ SystemThemeBuilder(builder: (context, accent) {
9999
});
100100
```
101101

102+
### Checking if accent color is supported
103+
104+
The `flutter/foundation` package provides a `defaultTargetPlatform` getter, which can be used to check what platform the current app is running on.
105+
106+
You can check if the current platform supports accent colors using this extension method:
107+
108+
```dart
109+
import 'package:flutter/foundation.dart' show defaultTargetPlatform;
110+
111+
void main() {
112+
final supported = defaultTargetPlatform.supportsAccentColor;
113+
114+
print('Accent color is: ${supported ? 'supported' : 'not supported'} on the current platform');
115+
}
116+
```
117+
102118
## Contribution
103119

104120
Feel free to [open an issue](https://github.com/bdlukaa/system_theme/issues/new) if you find an error or [make pull requests](https://github.com/bdlukaa/system_theme/pulls).

example/lib/main.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:system_theme/system_theme.dart';
3+
import 'package:flutter/foundation.dart' show defaultTargetPlatform;
34

45
void main() async {
56
WidgetsFlutterBinding.ensureInitialized();
@@ -32,8 +33,10 @@ class _MyAppState extends State<MyApp> {
3233
accent.darkest,
3334
];
3435
return Scaffold(
35-
body: Column(
36-
children: colors.map((color) {
36+
body: Column(children: [
37+
Text(
38+
'Accent color: ${defaultTargetPlatform.supportsAccentColor ? 'supported' : 'not supported'}'),
39+
...colors.map((color) {
3740
return Expanded(
3841
child: Container(
3942
color: color,
@@ -58,7 +61,7 @@ class _MyAppState extends State<MyApp> {
5861
),
5962
);
6063
}).toList(),
61-
),
64+
]),
6265
);
6366
});
6467
}

lib/system_theme.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import 'dart:async';
22
import 'dart:io';
33

4-
import 'package:flutter/foundation.dart' show debugPrint, kIsWeb;
4+
import 'package:flutter/foundation.dart'
5+
show TargetPlatform, debugPrint, kIsWeb;
56
import 'package:flutter/services.dart'
67
show Color, EventChannel, MethodChannel, MissingPluginException;
78
import 'package:flutter/widgets.dart' show WidgetsFlutterBinding;
@@ -19,6 +20,19 @@ const _eventChannel = EventChannel('system_theme_events/switch_callback');
1920
/// Platform channel handler for invoking native methods.
2021
const MethodChannel _channel = MethodChannel('system_theme');
2122

23+
extension PlatformHelpers on TargetPlatform {
24+
/// A helper that can be used to check if the current platform supports
25+
/// accent colors.
26+
bool get supportsAccentColor =>
27+
kIsWeb ||
28+
[
29+
TargetPlatform.windows,
30+
TargetPlatform.macOS,
31+
TargetPlatform.android,
32+
TargetPlatform.linux,
33+
].contains(this);
34+
}
35+
2236
/// Class to return current system theme state on Windows.
2337
///
2438
/// [accentColor] returns the current accent color as a [SystemAccentColor].

0 commit comments

Comments
 (0)