rand dart fix
This commit is contained in:
@@ -58,18 +58,16 @@ class WidgetGenerator extends Generator {
|
||||
if (isPropertyClass(baseClass)) {
|
||||
_base = 'PropertyBase';
|
||||
}
|
||||
if (_base != null) {
|
||||
final _template = MixinStoreTemplate(_base)
|
||||
..width = width
|
||||
..height = height;
|
||||
yield _generateCodeFromTemplate(
|
||||
baseClass.name,
|
||||
baseClass,
|
||||
_template,
|
||||
typeNameFinder,
|
||||
);
|
||||
final _template = MixinStoreTemplate(_base)
|
||||
..width = width
|
||||
..height = height;
|
||||
yield _generateCodeFromTemplate(
|
||||
baseClass.name,
|
||||
baseClass,
|
||||
_template,
|
||||
typeNameFinder,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _generateCodeFromTemplate(
|
||||
String publicTypeName,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'util.dart';
|
||||
|
||||
class CommaList<T> {
|
||||
CommaList(this.templates) : assert(templates != null);
|
||||
CommaList(this.templates);
|
||||
|
||||
final List<T> templates;
|
||||
|
||||
@@ -11,10 +11,7 @@ class CommaList<T> {
|
||||
}
|
||||
|
||||
class SurroundedCommaList<T> {
|
||||
SurroundedCommaList(this.prefix, this.suffix, this.templates)
|
||||
: assert(prefix != null),
|
||||
assert(suffix != null),
|
||||
assert(templates != null);
|
||||
SurroundedCommaList(this.prefix, this.suffix, this.templates);
|
||||
|
||||
final String prefix;
|
||||
final String suffix;
|
||||
|
||||
@@ -11,9 +11,7 @@ class ParamTemplate {
|
||||
String get metadata => hasRequiredAnnotation ? '@required ' : '';
|
||||
|
||||
@override
|
||||
String toString() => defaultValue == null
|
||||
? '$metadata$type $name'
|
||||
: '$type $name = $defaultValue';
|
||||
String toString() => '$type $name = $defaultValue';
|
||||
}
|
||||
|
||||
class TypeParamTemplate {
|
||||
@@ -23,7 +21,7 @@ class TypeParamTemplate {
|
||||
String get asArgument => name;
|
||||
|
||||
@override
|
||||
String toString() => bound == null ? name : '$name extends $bound';
|
||||
String toString() => '$name extends $bound';
|
||||
}
|
||||
|
||||
class NamedArgTemplate {
|
||||
|
||||
@@ -23,24 +23,18 @@ class BaseOptionTemplate extends SettingsImpl {
|
||||
sb.write('return ');
|
||||
if (tryParse) {
|
||||
sb.write("$propertyType.tryParse(params[${name}Key].toString())");
|
||||
if (defaultValue != null) {
|
||||
sb.write(' ?? $defaultValue');
|
||||
}
|
||||
} else {
|
||||
sb.write(' ?? $defaultValue');
|
||||
} else {
|
||||
sb.write('params[${name}Key] as $propertyType');
|
||||
}
|
||||
sb.writeln(';');
|
||||
sb.writeln('}');
|
||||
if (defaultValue != null) {
|
||||
if (propertyType == 'String') {
|
||||
sb.writeln("return '$defaultValue';");
|
||||
} else {
|
||||
sb.writeln("return $defaultValue;");
|
||||
}
|
||||
if (propertyType == 'String') {
|
||||
sb.writeln("return '$defaultValue';");
|
||||
} else {
|
||||
sb.writeln("return null;");
|
||||
sb.writeln("return $defaultValue;");
|
||||
}
|
||||
sb.writeln('}');
|
||||
sb.writeln('}');
|
||||
sb.writeln('set ${name}Val($propertyType val) {');
|
||||
sb.writeln('params[${name}Key] = val;');
|
||||
sb.writeln('widgetContext.onUpdate(id, widgetData);');
|
||||
|
||||
@@ -38,12 +38,8 @@ class ColorOptionTemplate extends SettingsImpl {
|
||||
sb.write('return $propertyType(_value);');
|
||||
sb.writeln('}');
|
||||
sb.writeln('}');
|
||||
if (defaultValue != null) {
|
||||
sb.writeln("return $propertyType($defaultValue);");
|
||||
} else {
|
||||
sb.writeln("return null;");
|
||||
}
|
||||
sb.writeln('}');
|
||||
sb.writeln("return $propertyType($defaultValue);");
|
||||
sb.writeln('}');
|
||||
// Setter
|
||||
sb.writeln('set ${name}Val($propertyType val) {');
|
||||
sb.write('params[${name}Key] = "');
|
||||
|
||||
@@ -29,7 +29,7 @@ class EnumOptionTemplate extends SettingsImpl {
|
||||
sb.write('final _value = ');
|
||||
sb.writeln("params[${name}Key].toString().replaceAll('#', '');");
|
||||
final _fallback =
|
||||
defaultValue == null ? null : _getEnumValueFromString(defaultValue);
|
||||
_getEnumValueFromString(defaultValue);
|
||||
sb.writeln("""
|
||||
return ${name}Values.firstWhere(
|
||||
(element) => element.toString() == _value,
|
||||
@@ -58,7 +58,6 @@ class EnumOptionTemplate extends SettingsImpl {
|
||||
}
|
||||
|
||||
T getEnum<T>(String val, {T fallback, List<T> values}) {
|
||||
if (val == null) return fallback;
|
||||
final _value = val.replaceAll('#', '');
|
||||
return values.firstWhere(
|
||||
(element) => element.toString() == _value,
|
||||
|
||||
@@ -23,12 +23,8 @@ class FunctionOptionTemplate extends SettingsImpl {
|
||||
sb.write('params[${name}Key] as String');
|
||||
sb.writeln(';');
|
||||
sb.writeln('}');
|
||||
if (defaultValue != null) {
|
||||
sb.writeln("return $defaultValue;");
|
||||
} else {
|
||||
sb.writeln("return null;");
|
||||
}
|
||||
sb.writeln('}');
|
||||
sb.writeln("return $defaultValue;");
|
||||
sb.writeln('}');
|
||||
sb.writeln('set ${name}Val(String val) {');
|
||||
sb.writeln('params[${name}Key] = val;');
|
||||
sb.writeln('widgetContext.onUpdate(id, widgetData);');
|
||||
@@ -40,13 +36,10 @@ class FunctionOptionTemplate extends SettingsImpl {
|
||||
String constructor() {
|
||||
final sb = StringBuffer();
|
||||
sb.write(' ');
|
||||
if (key != null && int.tryParse(key) != null) {
|
||||
if (int.tryParse(key) != null) {
|
||||
sb.write('');
|
||||
} else if (key != null) {
|
||||
sb.write("$key: ");
|
||||
} else {
|
||||
sb.write("$name: ");
|
||||
}
|
||||
} else sb.write("$key: ");
|
||||
|
||||
sb.writeln("() => onAction(context, ${name}Val)");
|
||||
sb.writeln(',');
|
||||
return sb.toString();
|
||||
|
||||
@@ -11,13 +11,10 @@ abstract class SettingsImpl {
|
||||
String constructor() {
|
||||
final sb = StringBuffer();
|
||||
sb.write(' ');
|
||||
if (key != null && int.tryParse(key) != null) {
|
||||
if (int.tryParse(key) != null) {
|
||||
sb.write('${name}Val');
|
||||
} else if (key != null) {
|
||||
sb.write("$key: ${name}Val");
|
||||
} else {
|
||||
sb.write("$name: ${name}Val");
|
||||
}
|
||||
} else sb.write("$key: ${name}Val");
|
||||
|
||||
sb.writeln(',');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@@ -34,12 +34,8 @@ class KeyOptionTemplate extends SettingsImpl {
|
||||
""");
|
||||
sb.writeln(';');
|
||||
sb.writeln('}');
|
||||
if (defaultValue != null) {
|
||||
sb.writeln("return ValueKey<String>('$defaultValue');");
|
||||
} else {
|
||||
sb.writeln("return null;");
|
||||
}
|
||||
sb.writeln('}');
|
||||
sb.writeln("return ValueKey<String>('$defaultValue');");
|
||||
sb.writeln('}');
|
||||
sb.writeln('set ${name}Val($propertyType val) {');
|
||||
sb.write("""
|
||||
if (val == null) {
|
||||
|
||||
@@ -33,21 +33,17 @@ class ListWidgetOptionTemplate extends SettingsImpl {
|
||||
return _children;
|
||||
""");
|
||||
sb.writeln('}');
|
||||
if (fallback != null) {
|
||||
final random = DateTime.now().millisecondsSinceEpoch.toString();
|
||||
sb.writeln("""
|
||||
return [
|
||||
widgetRender({
|
||||
'id': '$random',
|
||||
'name': '$fallback',
|
||||
'params': {},
|
||||
})
|
||||
];
|
||||
""");
|
||||
} else {
|
||||
sb.writeln("return null;");
|
||||
}
|
||||
sb.writeln('}');
|
||||
final random = DateTime.now().millisecondsSinceEpoch.toString();
|
||||
sb.writeln("""
|
||||
return [
|
||||
widgetRender({
|
||||
'id': '$random',
|
||||
'name': '$fallback',
|
||||
'params': {},
|
||||
})
|
||||
];
|
||||
""");
|
||||
sb.writeln('}');
|
||||
sb.writeln('void ${name}ValUpdate(Map<String, dynamic> val) {');
|
||||
sb.write("""
|
||||
if (params[${name}Key] == null) {
|
||||
@@ -64,19 +60,16 @@ class ListWidgetOptionTemplate extends SettingsImpl {
|
||||
String constructor() {
|
||||
final sb = StringBuffer();
|
||||
sb.write(' ');
|
||||
if (key != null && int.tryParse(key) != null) {
|
||||
if (int.tryParse(key) != null) {
|
||||
sb.write('');
|
||||
} else if (key != null) {
|
||||
sb.write("$key: ");
|
||||
} else {
|
||||
sb.write("$name: ");
|
||||
}
|
||||
} else sb.write("$key: ");
|
||||
|
||||
sb.write("""
|
||||
${name}Val == null && !widgetContext.isDragging ? ${empty ? '[]' : 'null'} : [
|
||||
if (${name}Val != null)
|
||||
for (final item in ${name}Val) item.build(context),
|
||||
""");
|
||||
if (acceptType != null && acceptType.isNotEmpty) {
|
||||
if (acceptType.isNotEmpty) {
|
||||
sb.write("""
|
||||
if (widgetContext.isDragging)
|
||||
DragTarget<$acceptType>(
|
||||
|
||||
@@ -35,13 +35,10 @@ class SupportedOptionTemplate extends SettingsImpl {
|
||||
String constructor() {
|
||||
final sb = StringBuffer();
|
||||
sb.write(' ');
|
||||
if (key != null && int.tryParse(key) != null) {
|
||||
if (int.tryParse(key) != null) {
|
||||
sb.write('');
|
||||
} else if (key != null) {
|
||||
sb.write("$key: ");
|
||||
} else {
|
||||
sb.write("$name: ");
|
||||
}
|
||||
} else sb.write("$key: ");
|
||||
|
||||
sb.writeln("${name}Val?.build(context)");
|
||||
sb.writeln(',');
|
||||
return sb.toString();
|
||||
|
||||
@@ -21,10 +21,8 @@ class WidgetOptionTemplate extends SettingsImpl {
|
||||
@override
|
||||
String access() {
|
||||
final sb = StringBuffer();
|
||||
if (acceptType != null) {
|
||||
sb.writeln('final _${name}Listen = ValueNotifier<bool>(false);');
|
||||
}
|
||||
sb.writeln('WidgetBase get ${name}Val {');
|
||||
sb.writeln('final _${name}Listen = ValueNotifier<bool>(false);');
|
||||
sb.writeln('WidgetBase get ${name}Val {');
|
||||
sb.write("if (params[${name}Key] != null) ");
|
||||
sb.writeln('{');
|
||||
sb.write('return ');
|
||||
@@ -54,69 +52,60 @@ class WidgetOptionTemplate extends SettingsImpl {
|
||||
String constructor() {
|
||||
final sb = StringBuffer();
|
||||
sb.write(' ');
|
||||
if (key != null && int.tryParse(key) != null) {
|
||||
if (int.tryParse(key) != null) {
|
||||
sb.write('');
|
||||
} else if (key != null) {
|
||||
sb.write("$key: ");
|
||||
} else {
|
||||
sb.write("$name: ");
|
||||
}
|
||||
if (acceptType == null) {
|
||||
sb.writeln('${name}Val?.build(context)');
|
||||
} else {
|
||||
} else sb.write("$key: ");
|
||||
|
||||
sb.write("""
|
||||
!widgetContext.isDragging || (widgetContext.isDragging && ${name}Val?.build(context) != null) ?
|
||||
(
|
||||
${name}Val?.build(context)
|
||||
|
||||
""");
|
||||
sb.write("""
|
||||
?? (widgetRender(widgetContext, json.decode(json.encode({
|
||||
'id': '${shortid.generate()}',
|
||||
'name': '$fallback',
|
||||
'params': {},
|
||||
})))).build(context)
|
||||
""");
|
||||
sb.write("""
|
||||
!widgetContext.isDragging || (widgetContext.isDragging && ${name}Val?.build(context) != null) ?
|
||||
(
|
||||
${name}Val?.build(context)
|
||||
|
||||
""");
|
||||
if (fallback != null) {
|
||||
sb.write("""
|
||||
?? (widgetRender(widgetContext, json.decode(json.encode({
|
||||
'id': '${shortid.generate()}',
|
||||
'name': '$fallback',
|
||||
'params': {},
|
||||
})))).build(context)
|
||||
""");
|
||||
}
|
||||
sb.write("""
|
||||
)
|
||||
""");
|
||||
sb.write("""
|
||||
:
|
||||
PreferredSize(
|
||||
preferredSize: Size(${acceptWidth ?? 30}, ${acceptHeight ?? 30}),
|
||||
child: DragTarget<$acceptType>(
|
||||
onAccept: (val) {
|
||||
_${name}Listen.value = false;
|
||||
if (val != null) {
|
||||
${name}ValUpdate(val?.data);
|
||||
}
|
||||
},
|
||||
onLeave: (val) {
|
||||
_${name}Listen.value = false;
|
||||
},
|
||||
onWillAccept: (val) {
|
||||
_${name}Listen.value = true;
|
||||
return _${name}Listen.value;
|
||||
},
|
||||
builder: (context, accepted, rejected) {
|
||||
return ValueListenableBuilder<bool>(
|
||||
valueListenable: _${name}Listen,
|
||||
builder: (context, _accepting, child) => SizedBox.fromSize(
|
||||
size: Size(${acceptWidth ?? 30}, ${acceptHeight ?? 30}),
|
||||
child: Placeholder(
|
||||
color: !_accepting ?
|
||||
Colors.grey :
|
||||
Theme.of(context).accentColor,
|
||||
),
|
||||
));
|
||||
},
|
||||
),
|
||||
)
|
||||
""");
|
||||
}
|
||||
sb.writeln(',');
|
||||
)
|
||||
""");
|
||||
sb.write("""
|
||||
:
|
||||
PreferredSize(
|
||||
preferredSize: Size(${acceptWidth ?? 30}, ${acceptHeight ?? 30}),
|
||||
child: DragTarget<$acceptType>(
|
||||
onAccept: (val) {
|
||||
_${name}Listen.value = false;
|
||||
if (val != null) {
|
||||
${name}ValUpdate(val?.data);
|
||||
}
|
||||
},
|
||||
onLeave: (val) {
|
||||
_${name}Listen.value = false;
|
||||
},
|
||||
onWillAccept: (val) {
|
||||
_${name}Listen.value = true;
|
||||
return _${name}Listen.value;
|
||||
},
|
||||
builder: (context, accepted, rejected) {
|
||||
return ValueListenableBuilder<bool>(
|
||||
valueListenable: _${name}Listen,
|
||||
builder: (context, _accepting, child) => SizedBox.fromSize(
|
||||
size: Size(${acceptWidth ?? 30}, ${acceptHeight ?? 30}),
|
||||
child: Placeholder(
|
||||
color: !_accepting ?
|
||||
Colors.grey :
|
||||
Theme.of(context).accentColor,
|
||||
),
|
||||
));
|
||||
},
|
||||
),
|
||||
)
|
||||
""");
|
||||
sb.writeln(',');
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,20 +21,15 @@ class MixinStoreTemplate extends StoreTemplate {
|
||||
if (_hasPreferredSize) {
|
||||
sb.writeln('@override');
|
||||
sb.write('Size get preferredSize => ');
|
||||
if (width != null && height != null) {
|
||||
sb.write('Size($width, $height)');
|
||||
} else if (width != null) {
|
||||
sb.write('Size.fromWidth($width)');
|
||||
} else if (height != null) {
|
||||
sb.write('Size.fromHeight($height)');
|
||||
}
|
||||
sb.write('Size($width, $height)');
|
||||
|
||||
sb.writeln(';');
|
||||
}
|
||||
sb.writeln('');
|
||||
sb.writeln('@override');
|
||||
sb.writeln('Map<String, String> get properties => {');
|
||||
for (final setting in settings) {
|
||||
sb.write("'${setting?.key ?? setting.name}'");
|
||||
sb.write("'${setting.key ?? setting.name}'");
|
||||
sb.write(':');
|
||||
sb.write("'${setting.propertyType}'");
|
||||
sb.writeln(',');
|
||||
@@ -62,7 +57,7 @@ class MixinStoreTemplate extends StoreTemplate {
|
||||
sb.write('Animated');
|
||||
}
|
||||
sb.writeln('$widgetName(');
|
||||
settings.sort((a, b) => (a?.key ?? a.name).compareTo((b?.key ?? b.name)));
|
||||
settings.sort((a, b) => (a.key ?? a.name).compareTo((b.key ?? b.name)));
|
||||
if (isAnimated) {
|
||||
sb.writeln(
|
||||
'duration: const Duration(milliseconds: $animatedDurationMilliseconds),');
|
||||
|
||||
@@ -24,10 +24,8 @@ class LibraryScopedNameFinder {
|
||||
|
||||
Map<Element, String> _namesByElement;
|
||||
Map<Element, String> get namesByElement {
|
||||
if (_namesByElement != null) {
|
||||
return _namesByElement;
|
||||
}
|
||||
|
||||
return _namesByElement;
|
||||
|
||||
_namesByElement = {};
|
||||
|
||||
// Add all of this library's type-defining elements to the name map
|
||||
|
||||
Reference in New Issue
Block a user