Skip to content

Commit

Permalink
Prevent app breaks on loading accent color failed. (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa authored Jul 3, 2023
2 parents 72d5da0 + 2fff2cd commit 197f1b9
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions lib/system_theme.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/services.dart' show MethodChannel, Color;
import 'package:flutter/foundation.dart' show debugPrint;
import 'package:flutter/services.dart'
show Color, MethodChannel, MissingPluginException;
import 'package:flutter/widgets.dart' show WidgetsFlutterBinding;

/// Default system accent color.
Expand Down Expand Up @@ -74,16 +76,23 @@ class SystemAccentColor {
Future<void> load() async {
WidgetsFlutterBinding.ensureInitialized();

final colors = await _channel.invokeMethod(kGetSystemAccentColorMethod);
if (colors == null) return;

accent = _retrieve(colors['accent'])!;
light = _retrieve(colors['light']) ?? accent;
lighter = _retrieve(colors['lighter']) ?? accent;
lightest = _retrieve(colors['lightest']) ?? accent;
dark = _retrieve(colors['dark']) ?? accent;
darker = _retrieve(colors['darker']) ?? accent;
darkest = _retrieve(colors['darkest']) ?? accent;
try {
final colors = await _channel.invokeMethod(kGetSystemAccentColorMethod);
if (colors == null) return;

accent = _retrieve(colors['accent'])!;
light = _retrieve(colors['light']) ?? accent;
lighter = _retrieve(colors['lighter']) ?? accent;
lightest = _retrieve(colors['lightest']) ?? accent;
dark = _retrieve(colors['dark']) ?? accent;
darker = _retrieve(colors['darker']) ?? accent;
darkest = _retrieve(colors['darkest']) ?? accent;
} on MissingPluginException {
debugPrint('system_theme does not the current platform');
return;
} catch (_) {
rethrow;
}
}

Color? _retrieve(dynamic map) {
Expand Down

0 comments on commit 197f1b9

Please sign in to comment.