Created
January 4, 2021 17:58
-
-
Save gaetschwartz/eabd86a0d4465fbefe214f88558644a9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import 'package:flutter/material.dart'; | |
import 'package:freezed_annotation/freezed_annotation.dart'; | |
part 'colorscheme.freezed.dart'; | |
part 'colorscheme.g.dart'; | |
@freezed | |
abstract class SerializableColorScheme with _$SerializableColorScheme { | |
@ColorConverter() | |
factory SerializableColorScheme({ | |
@required Color primary, | |
@required Color primaryVariant, | |
@required Color secondary, | |
@required Color secondaryVariant, | |
@required Color surface, | |
@required Color background, | |
@required Color error, | |
@required Color onPrimary, | |
@required Color onSecondary, | |
@required Color onSurface, | |
@required Color onBackground, | |
@required Color onError, | |
@required Brightness brightness, | |
}) = _SerializableColorScheme; | |
factory SerializableColorScheme.fromColorScheme(ColorScheme c) => _SerializableColorScheme( | |
background: c.background, | |
brightness: c.brightness, | |
error: c.error, | |
onBackground: c.onBackground, | |
onError: c.onError, | |
onPrimary: c.onPrimary, | |
onSecondary: c.onSecondary, | |
onSurface: c.onSurface, | |
primary: c.primary, | |
primaryVariant: c.primaryVariant, | |
secondary: c.secondary, | |
secondaryVariant: c.secondaryVariant, | |
surface: c.surface, | |
); | |
factory SerializableColorScheme.light() => | |
SerializableColorScheme.fromColorScheme(const ColorScheme.light()); | |
factory SerializableColorScheme.dark() => | |
SerializableColorScheme.fromColorScheme(const ColorScheme.dark()); | |
factory SerializableColorScheme.fromJson(Map<String, dynamic> json) => | |
_$SerializableColorSchemeFromJson(json); | |
@late | |
ColorScheme get colorScheme => ColorScheme( | |
primary: primary, | |
primaryVariant: primaryVariant, | |
secondary: secondary, | |
secondaryVariant: secondaryVariant, | |
surface: surface, | |
background: background, | |
error: error, | |
onPrimary: onPrimary, | |
onSecondary: onSecondary, | |
onSurface: onSurface, | |
onBackground: onBackground, | |
onError: onError, | |
brightness: brightness); | |
} | |
class ColorConverter implements JsonConverter<Color, int> { | |
const ColorConverter(); | |
@override | |
Color fromJson(int json) => Color(json); | |
@override | |
int toJson(Color color) => color.value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment