Skip to content

Commit

Permalink
Add extensions on CupertinoDynamicColor to try and solve what's block…
Browse files Browse the repository at this point in the history
…ing #30 (#45)
  • Loading branch information
GroovinChip committed Apr 18, 2021
1 parent 3a36cc8 commit 4cffae2
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions lib/src/styles/colors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import 'package:flutter/cupertino.dart';

import 'macos_theme.dart';

extension DynamicColorX on CupertinoDynamicColor {
Color macosResolve(Color resolvable, BuildContext context) {
return (resolvable is CupertinoDynamicColor)
? resolvable.macosResolveFrom(context)
: resolvable;
}

bool get isPlatformBrightnessDependent {
return color != darkColor ||
elevatedColor != darkElevatedColor ||
highContrastColor != darkHighContrastColor ||
highContrastElevatedColor != darkHighContrastElevatedColor;
}

bool get isHighContrastDependent {
return color != highContrastColor ||
darkColor != darkHighContrastColor ||
elevatedColor != highContrastElevatedColor ||
darkElevatedColor != darkHighContrastElevatedColor;
}

bool get isInterfaceElevationDependent {
return color != elevatedColor ||
darkColor != darkElevatedColor ||
highContrastColor != highContrastElevatedColor ||
darkHighContrastColor != darkHighContrastElevatedColor;
}

CupertinoDynamicColor macosResolveFrom(BuildContext context) {
Brightness brightness = Brightness.light;
if (this.isPlatformBrightnessDependent) {
brightness = MacosTheme.maybeBrightnessOf(context) ?? Brightness.light;
}
bool isHighContrastEnabled = false;
if (this.isHighContrastDependent) {
isHighContrastEnabled =
MediaQuery.maybeOf(context)?.highContrast ?? false;
}

final CupertinoUserInterfaceLevelData level =
this.isInterfaceElevationDependent
? CupertinoUserInterfaceLevel.maybeOf(context) ??
CupertinoUserInterfaceLevelData.base
: CupertinoUserInterfaceLevelData.base;

final Color resolved;
switch (brightness) {
case Brightness.light:
switch (level) {
case CupertinoUserInterfaceLevelData.base:
resolved = isHighContrastEnabled ? highContrastColor : color;
break;
case CupertinoUserInterfaceLevelData.elevated:
resolved = isHighContrastEnabled
? highContrastElevatedColor
: elevatedColor;
break;
}
break;
case Brightness.dark:
switch (level) {
case CupertinoUserInterfaceLevelData.base:
resolved =
isHighContrastEnabled ? darkHighContrastColor : darkColor;
break;
case CupertinoUserInterfaceLevelData.elevated:
resolved = isHighContrastEnabled
? darkHighContrastElevatedColor
: darkElevatedColor;
break;
}
}

Element? _debugContext;
assert(() {
_debugContext = context as Element;
return true;
}());
return ResolvedCupertinoDynamicColor(
resolved,
color,
darkColor,
highContrastColor,
darkHighContrastColor,
elevatedColor,
darkElevatedColor,
highContrastElevatedColor,
darkHighContrastElevatedColor,
_debugContext,
//_debugLabel,
);
}
}

class ResolvedCupertinoDynamicColor extends CupertinoDynamicColor {
const ResolvedCupertinoDynamicColor(
Color resolvedColor,
Color color,
Color darkColor,
Color highContrastColor,
Color darkHighContrastColor,
Color elevatedColor,
Color darkElevatedColor,
Color highContrastElevatedColor,
Color darkHighContrastElevatedColor,
Element? debugResolveContext,
) : super(
color: color,
darkColor: darkColor,
highContrastColor: highContrastColor,
darkHighContrastColor: darkHighContrastColor,
elevatedColor: elevatedColor,
darkElevatedColor: darkElevatedColor,
highContrastElevatedColor: highContrastElevatedColor,
darkHighContrastElevatedColor: darkHighContrastElevatedColor,
);
}

0 comments on commit 4cffae2

Please sign in to comment.