Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for parsing theme color #61

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/dynamic_widget/basic/appbar_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class AppBarWidgetParser extends WidgetParser {
centerTitle:
map.containsKey("centerTitle") ? map["centerTitle"] as bool : false,
backgroundColor: map.containsKey("backgroundColor")
? parseHexColor(map["backgroundColor"])
? parseColor(buildContext, map["backgroundColor"])
: null,
);

Expand Down
15 changes: 9 additions & 6 deletions lib/dynamic_widget/basic/button_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,29 @@ class RaisedButtonParser extends WidgetParser {
map.containsKey("click_event") ? map['click_event'] : "";

var raisedButton = RaisedButton(
color: map.containsKey('color') ? parseHexColor(map['color']) : null,
color: map.containsKey('color')
? parseColor(buildContext, map['color'])
: null,
disabledColor: map.containsKey('disabledColor')
? parseHexColor(map['disabledColor'])
? parseColor(buildContext, map['disabledColor'])
: null,
disabledElevation: map.containsKey('disabledElevation')
? map['disabledElevation']?.toDouble()
: 0.0,
disabledTextColor: map.containsKey('disabledTextColor')
? parseHexColor(map['disabledTextColor'])
? parseColor(buildContext, map['disabledTextColor'])
: null,
elevation:
map.containsKey('elevation') ? map['elevation']?.toDouble() : 0.0,
padding: map.containsKey('padding')
? parseEdgeInsetsGeometry(map['padding'])
: null,
splashColor: map.containsKey('splashColor')
? parseHexColor(map['splashColor'])
? parseColor(buildContext, map['splashColor'])
: null,
textColor: map.containsKey('textColor')
? parseColor(buildContext, map['textColor'])
: null,
textColor:
map.containsKey('textColor') ? parseHexColor(map['textColor']) : null,
child: DynamicWidgetBuilder.buildFromMap(
map['child'], buildContext, listener),
onPressed: () {
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamic_widget/basic/container_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ContainerWidgetParser extends WidgetParser {
Widget parse(Map<String, dynamic> map, BuildContext buildContext,
ClickListener listener) {
Alignment alignment = parseAlignment(map['alignment']);
Color color = parseHexColor(map['color']);
Color color = parseColor(buildContext, map['color']);
BoxConstraints constraints = parseBoxConstraints(map['constraints']);
//TODO: decoration, foregroundDecoration and transform properties to be implemented.
EdgeInsetsGeometry margin = parseEdgeInsetsGeometry(map['margin']);
Expand Down
4 changes: 2 additions & 2 deletions lib/dynamic_widget/basic/dropcaptext_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class DropCapTextParser extends WidgetParser {
mode: map.containsKey('mode')
? parseDropCapMode(map['mode'])
: DropCapMode.inside,
style: map.containsKey('style') ? parseTextStyle(map['style']) : null,
style: map.containsKey('style') ? parseTextStyle(buildContext, map['style']) : null,
dropCapStyle: map.containsKey('dropCapStyle')
? parseTextStyle(map['dropCapStyle'])
? parseTextStyle(buildContext, map['dropCapStyle'])
: null,
textAlign: map.containsKey('textAlign')
? parseTextAlign(map['textAlign'])
Expand Down
4 changes: 3 additions & 1 deletion lib/dynamic_widget/basic/icon_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class IconWidgetParser extends WidgetParser {
? getIconUsingPrefix(name: map['data'])
: Icons.android,
size: map.containsKey("size") ? map['size']?.toDouble() : null,
color: map.containsKey('color') ? parseHexColor(map['color']) : null,
color: map.containsKey('color')
? parseColor(buildContext, map['color'])
: null,
semanticLabel:
map.containsKey('semanticLabel') ? map['semanticLabel'] : null,
textDirection: map.containsKey('textDirection')
Expand Down
8 changes: 6 additions & 2 deletions lib/dynamic_widget/basic/image_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class AssetImageWidgetParser extends WidgetParser {
double width = map.containsKey('width') ? map['width']?.toDouble() : null;
double height =
map.containsKey('height') ? map['height']?.toDouble() : null;
Color color = map.containsKey('color') ? parseHexColor(map['color']) : null;
Color color = map.containsKey('color')
? parseColor(buildContext, map['color'])
: null;
BlendMode blendMode =
map.containsKey('blendMode') ? parseBlendMode(map['blendMode']) : null;
BoxFit boxFit =
Expand Down Expand Up @@ -207,7 +209,9 @@ class NetworkImageWidgetParser extends WidgetParser {
double width = map.containsKey('width') ? map['width']?.toDouble() : null;
double height =
map.containsKey('height') ? map['height']?.toDouble() : null;
Color color = map.containsKey('color') ? parseHexColor(map['color']) : null;
Color color = map.containsKey('color')
? parseColor(buildContext, map['color'])
: null;
BlendMode blendMode =
map.containsKey('blendMode') ? parseBlendMode(map['blendMode']) : null;
BoxFit boxFit =
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamic_widget/basic/placeholder_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class PlaceholderWidgetParser extends WidgetParser {
ClickListener listener) {
return Placeholder(
color: map.containsKey('color')
? parseHexColor(map['color'])
? parseColor(buildContext, map['color'])
: const Color(0xFF455A64),
strokeWidth:
map.containsKey('strokeWidth') ? map['strokeWidth']?.toDouble() : 2.0,
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamic_widget/basic/scaffold_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ScaffoldWidgetParser extends WidgetParser {
map["floatingActionButton"], buildContext, listener)
: null,
backgroundColor: map.containsKey("backgroundColor")
? parseHexColor(map["backgroundColor"])
? parseColor(buildContext, map["backgroundColor"])
: null,
);

Expand Down
23 changes: 14 additions & 9 deletions lib/dynamic_widget/basic/selectabletext_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SelectableTextWidgetParser implements WidgetParser {
var textSpan;
var textSpanParser = SelectableTextSpanParser();
if (map.containsKey("textSpan")) {
textSpan = textSpanParser.parse(map['textSpan'], listener);
textSpan = textSpanParser.parse(buildContext, map['textSpan'], listener);
}

if (textSpan == null) {
Expand All @@ -24,7 +24,9 @@ class SelectableTextWidgetParser implements WidgetParser {
textAlign: parseTextAlign(textAlignString),
maxLines: maxLines,
textDirection: parseTextDirection(textDirectionString),
style: map.containsKey('style') ? parseTextStyle(map['style']) : null,
style: map.containsKey('style')
? parseTextStyle(buildContext, map['style'])
: null,
// textScaleFactor: textScaleFactor,
);
} else {
Expand All @@ -33,7 +35,9 @@ class SelectableTextWidgetParser implements WidgetParser {
textAlign: parseTextAlign(textAlignString),
maxLines: maxLines,
textDirection: parseTextDirection(textDirectionString),
style: map.containsKey('style') ? parseTextStyle(map['style']) : null,
style: map.containsKey('style')
? parseTextStyle(buildContext, map['style'])
: null,
// textScaleFactor: textScaleFactor,
);
}
Expand Down Expand Up @@ -79,28 +83,29 @@ class SelectableTextWidgetParser implements WidgetParser {
}

class SelectableTextSpanParser {
TextSpan parse(Map<String, dynamic> map, ClickListener listener) {
TextSpan parse(BuildContext buildContext, Map<String, dynamic> map,
ClickListener listener) {
String clickEvent = map.containsKey("recognizer") ? map['recognizer'] : "";
var textSpan = TextSpan(
text: map['text'],
style: parseTextStyle(map['style']),
style: parseTextStyle(buildContext, map['style']),
recognizer: TapGestureRecognizer()
..onTap = () {
listener.onClicked(clickEvent);
},
children: []);

if (map.containsKey('children')) {
parseChildren(textSpan, map['children'], listener);
parseChildren(buildContext, textSpan, map['children'], listener);
}

return textSpan;
}

void parseChildren(
TextSpan textSpan, List<dynamic> childrenSpan, ClickListener listener) {
void parseChildren(BuildContext buildContext, TextSpan textSpan,
List<dynamic> childrenSpan, ClickListener listener) {
for (var childmap in childrenSpan) {
textSpan.children.add(parse(childmap, listener));
textSpan.children.add(parse(buildContext, childmap, listener));
}
}

Expand Down
23 changes: 14 additions & 9 deletions lib/dynamic_widget/basic/text_widget_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TextWidgetParser implements WidgetParser {
var textSpan;
var textSpanParser = TextSpanParser();
if (map.containsKey("textSpan")) {
textSpan = textSpanParser.parse(map['textSpan'], listener);
textSpan = textSpanParser.parse(buildContext, map['textSpan'], listener);
}

if (textSpan == null) {
Expand All @@ -30,7 +30,9 @@ class TextWidgetParser implements WidgetParser {
semanticsLabel: semanticsLabel,
softWrap: softWrap,
textDirection: parseTextDirection(textDirectionString),
style: map.containsKey('style') ? parseTextStyle(map['style']) : null,
style: map.containsKey('style')
? parseTextStyle(buildContext, map['style'])
: null,
textScaleFactor: textScaleFactor,
);
} else {
Expand All @@ -42,7 +44,9 @@ class TextWidgetParser implements WidgetParser {
semanticsLabel: semanticsLabel,
softWrap: softWrap,
textDirection: parseTextDirection(textDirectionString),
style: map.containsKey('style') ? parseTextStyle(map['style']) : null,
style: map.containsKey('style')
? parseTextStyle(buildContext, map['style'])
: null,
textScaleFactor: textScaleFactor,
);
}
Expand Down Expand Up @@ -100,19 +104,20 @@ class TextWidgetParser implements WidgetParser {
}

class TextSpanParser {
TextSpan parse(Map<String, dynamic> map, ClickListener listener) {
TextSpan parse(BuildContext buildContext, Map<String, dynamic> map,
ClickListener listener) {
String clickEvent = map.containsKey("recognizer") ? map['recognizer'] : "";
var textSpan = TextSpan(
text: map['text'],
style: parseTextStyle(map['style']),
style: parseTextStyle(buildContext, map['style']),
recognizer: TapGestureRecognizer()
..onTap = () {
listener.onClicked(clickEvent);
},
children: []);

if (map.containsKey('children')) {
parseChildren(textSpan, map['children'], listener);
parseChildren(buildContext, textSpan, map['children'], listener);
}

return textSpan;
Expand All @@ -126,10 +131,10 @@ class TextSpanParser {
};
}

void parseChildren(
TextSpan textSpan, List<dynamic> childrenSpan, ClickListener listener) {
void parseChildren(BuildContext buildContext, TextSpan textSpan,
List<dynamic> childrenSpan, ClickListener listener) {
for (var childmap in childrenSpan) {
textSpan.children.add(parse(childmap, listener));
textSpan.children.add(parse(buildContext, childmap, listener));
}
}

Expand Down
46 changes: 44 additions & 2 deletions lib/dynamic_widget/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:ui';

import 'package:dynamic_widget/dynamic_widget.dart';
import 'package:dynamic_widget/dynamic_widget/drop_cap_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

TextAlign parseTextAlign(String textAlignString) {
Expand Down Expand Up @@ -214,6 +215,47 @@ String exportFontWeight(FontWeight fontWeight) {
return rt;
}

Color parseColor(BuildContext context, String color) {
if (color == null) {
return null;
}

return color.startsWith('::')
? parseThemeColor(Theme.of(context).colorScheme, color)
: parseHexColor(color);
}

Color parseThemeColor(ColorScheme colorScheme, String color) {
switch (color) {
case '::primary':
return colorScheme.primary;
case '::primaryVariant':
return colorScheme.primaryVariant;
case '::onPrimary':
return colorScheme.onPrimary;
case '::secondary':
return colorScheme.secondary;
case '::secondaryVariant':
return colorScheme.secondaryVariant;
case '::onSecondary':
return colorScheme.onSecondary;
case '::surface':
return colorScheme.surface;
case '::onSurface':
return colorScheme.onSurface;
case '::background':
return colorScheme.background;
case '::onBackground':
return colorScheme.onBackground;
case '::error':
return colorScheme.error;
case '::onError':
return colorScheme.onError;
default:
return null;
}
}

Color parseHexColor(String hexColorString) {
if (hexColorString == null) {
return null;
Expand All @@ -226,7 +268,7 @@ Color parseHexColor(String hexColorString) {
return Color(colorInt);
}

TextStyle parseTextStyle(Map<String, dynamic> map) {
TextStyle parseTextStyle(BuildContext context, Map<String, dynamic> map) {
if (map == null) {
return null;
}
Expand All @@ -241,7 +283,7 @@ TextStyle parseTextStyle(Map<String, dynamic> map) {
'italic' == map['fontStyle'] ? FontStyle.italic : FontStyle.normal;

return TextStyle(
color: parseHexColor(color),
color: parseColor(context, color),
debugLabel: debugLabel,
decoration: parseTextDecoration(decoration),
fontSize: fontSize,
Expand Down