-
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extensions on CupertinoDynamicColor to try and solve what's block…
…ing #30
- Loading branch information
1 parent
3a36cc8
commit df29658
Showing
1 changed file
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |