embedded assets, provider, and example

This commit is contained in:
julian 2023-04-20 09:32:50 -06:00
parent 7581e4c5f8
commit e3888be1b1
4 changed files with 129 additions and 34 deletions

View file

@ -9,14 +9,12 @@ import 'package:stackwallet/utilities/theme/color_theme.dart';
import 'package:uuid/uuid.dart';
@Collection(inheritance: false)
class ColorTheme {
static String themesDirPath = "/djhfgj/sdfd/themes/";
class StackTheme {
final String assetBundleUrl;
/// should be a uuid
@Index(unique: true, replace: true)
final String id;
final String internalId;
/// the theme name that will be displayed in app
final String name;
@ -39,21 +37,18 @@ class ColorTheme {
}
@ignore
Color get background => _background ??= Color(
backgroundString.toBigIntFromHex.toInt(),
);
Color get background => _background ??= Color(backgroundInt);
@ignore
Color? _background;
final String backgroundString;
final int backgroundInt;
// ==== backgroundAppBar =====================================================
@ignore
Color get backgroundAppBar => _backgroundAppBar ??= Color(
backgroundAppBarString.toBigIntFromHex.toInt(),
);
Color get backgroundAppBar =>
_backgroundAppBar ??= Color(backgroundAppBarInt);
@ignore
Color? _backgroundAppBar;
final String backgroundAppBarString;
final int backgroundAppBarInt;
// ===========================================================================
@ignore
@ -67,45 +62,82 @@ class ColorTheme {
Gradient? _gradientBackground;
final String gradientBackgroundString;
// ===========================================================================
@ignore
Map<Coin, Color> get coinColors =>
_coinColors ??= parseCoinColors(coinColorsString);
_coinColors ??= parseCoinColors(coinColorsJsonString);
@ignore
Map<Coin, Color>? _coinColors;
final String coinColorsString;
final String coinColorsJsonString;
// ==== assets =====================================================
final String circleLock;
// ===========================================================================
ColorTheme({
required this.id,
// ===========================================================================
// ===========================================================================
final ThemeAssets assets;
// ===========================================================================
// ===========================================================================
StackTheme({
required this.internalId,
required this.assetBundleUrl,
required this.name,
required this.brightnessString,
required this.backgroundString,
required this.backgroundAppBarString,
required this.backgroundInt,
required this.backgroundAppBarInt,
required this.gradientBackgroundString,
required this.coinColorsString,
required this.circleLock,
required this.coinColorsJsonString,
required this.assets,
});
factory ColorTheme.fromJson(Map<String, dynamic> json) {
factory StackTheme.fromJson({
required Map<String, dynamic> json,
required String applicationThemesDirectoryPath,
}) {
final _id = const Uuid().v1();
return ColorTheme(
id: _id,
return StackTheme(
internalId: _id,
name: json["name"] as String,
assetBundleUrl: json["assetBundleUrl"] as String,
brightnessString: json["brightness"] as String,
backgroundString: json["colors"]["background"] as String,
backgroundAppBarString: json["colors"]["backgroundAppBar"] as String,
backgroundInt: parseColor(json["colors"]["background"] as String),
backgroundAppBarInt:
parseColor(json["colors"]["backgroundAppBar"] as String),
gradientBackgroundString:
jsonEncode(json["gradients"]["gradientBackground"] as Map),
coinColorsString: jsonEncode(json["coinColors"] as Map),
circleLock:
"$themesDirPath/$_id/${json["assets"]["circleLock"] as String}",
coinColorsJsonString: jsonEncode(json["coinColors"] as Map),
assets: ThemeAssets.fromJson(
json: json,
applicationThemesDirectoryPath: applicationThemesDirectoryPath,
internalThemeUuid: _id,
),
);
}
/// Grab the int value of the hex color string.
/// 8 char string value expected where the first 2 are opacity
static int parseColor(String colorHex) {
try {
final int colorValue = colorHex.toBigIntFromHex.toInt();
if (colorValue >= 0 && colorValue <= 0xFFFFFFFF) {
return colorValue;
} else {
throw ArgumentError(
'"$colorHex" and corresponding int '
'value "$colorValue" is not a valid color.',
);
}
} catch (_) {
throw ArgumentError(
'"$colorHex" is not a valid hex number',
);
}
}
/// parse coin colors json and fetch color or use default
static Map<Coin, Color> parseCoinColors(String jsonString) {
final json = jsonDecode(jsonString) as Map;
final map = Map<String, dynamic>.from(json);
@ -125,3 +157,25 @@ class ColorTheme {
return result;
}
}
@Embedded(inheritance: false)
class ThemeAssets {
final String plus;
// todo: add all assets expected in json
ThemeAssets({
required this.plus,
});
factory ThemeAssets.fromJson({
required Map<String, dynamic> json,
required String applicationThemesDirectoryPath,
required String internalThemeUuid,
}) {
return ThemeAssets(
plus:
"$applicationThemesDirectoryPath/$internalThemeUuid/${json["assets"]["svg"]["plus.svg"] as String}",
);
}
}

View file

@ -193,14 +193,14 @@ final Map<String, dynamic> darkJson = {
{
"type": "standard",
"color": "0x0F2D3132",
"spread_radius": 3,
"blur_radius": 4
"spread_radius": 3.0,
"blur_radius": 4.0
},
{
"type": "home_view_button_bar",
"color": "0x0F2D3132",
"spread_radius": 3,
"blur_radius": 4
"spread_radius": 3.0,
"blur_radius": 4.0
}
]
};

View file

@ -0,0 +1,41 @@
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:stackwallet/models/isar/sw_theme.dart';
import 'package:stackwallet/themes/defaults/dark.dart';
final applicationThemesDirectoryPathProvider = StateProvider((ref) => "");
final themeProvider = StateProvider<StackTheme>((ref) {
// Return default if no theme was properly loaded on startup. This should
// technically never actually be read but we don't want an optional.
// Ideally Riverpod would would give us some kind of 'late' provider option
return StackTheme.fromJson(
json: darkJson,
// Explicitly use ref.read here as we do not want any rebuild on this
// value change.
applicationThemesDirectoryPath:
ref.read(applicationThemesDirectoryPathProvider),
);
});
/// example
class ExampleWidget extends ConsumerWidget {
const ExampleWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
return Column(
children: [
const Text("Hello, world!"),
SvgPicture.file(
File(
ref.watch(themeProvider).assets.plus,
),
),
],
);
}
}