dartwriter
dartwriter
provides api to generate dart source code. it can make your job easier while developing flutter/dart tools. you can also generate flutter ui code.
hello world example
var context = editorcontext(enabledartformatter: true);
var code = method(
name: 'main',
returntype: 'void',
statements: [
call('print',
argument: argument([
argumentitem("'hello world!'")
])
),
return('0')
]
);
print(context.build([code]));
generated as below:
void main() {
print('hello world!');
return 0;
}
flutter stateless widget example
input
{
"as": "scaffold",
"appbar": {
"as": "appbar",
"title": {
"as": "text",
"params": [
"'ahmet'"
]
},
"centertitle": "false"
},
"body": {
"as": "center",
"child": {
"as": "row",
"children": [
{
"as": "icon",
"params": ["icons.add"],
"color": "colors.red"
},
{
"as": "text",
"params": ["'ahmet'"],
"textalign": "textalign.center"
}
]
}
}
}
code:
var context = editorcontext(enabledartformatter: true);
var darthelper = darthelper.instance;
map map = jsondecode(json);
var homepage = class('homepage',
baseclass: 'statelesswidget',
methods: [
annotation('override'),
method(
name: 'build',
returntype: 'widget',
param: parameter([
parameteritem('buildcontext context'),
]),
statements: [ return(darthelper.getcodefrommap(map)) ]
)
]
);
print(context.build([
import('package:flutter/material.dart'),
homepage
]));
generated ui code:
import 'package:flutter/material.dart';
class homepage extends statelesswidget {
@override
widget build(buildcontext context) {
return scaffold(
appbar: appbar(title: text('ahmet'), centertitle: false),
body: center(
child: row(children: [
icon(icons.add, color: colors.red),
text('ahmet', textalign: textalign.center)
])));
}
}
installation
in the pubspec.yaml
of your flutter / dart project, add the following dependency:
dependencies:
...
dart_writer: any
in your library file add the following import:
import 'package:dart_writer/dart_writer.dart';
api documentation
conditions
method(
name: 'getmin',
returntype: 'int',
statements: [
assign('var num1', '5'),
assign('var num2', '10'),
if(condition: 'num1 < num2', statements: [return('num1')]),
elseif(condition: 'num1 == num2', statements: [return('num1')]),
else(statements: [return('num2')])
]
)
generated code:
int getmin() {
var num1 = 5;
var num2 = 10;
if (num1 < num2) {
return num1;
} else if (num1 == num2) {
return num1;
} else {
return num2;
}
}
loops
method(
name: 'loops',
returntype: 'void',
statements: [
for('i = 0', 'i < 5', 'i++',
statements: [rawcode('print(i);')]
),
foreach('item', 'userlist',
statements: [
return('usercard(item)')
]
),
while('i < 5',
statements: [ rawcode('print(i);'), assign('i', 'i + 1')]
)
]
)
generated code:
void loops() {
for (var i = 0; i < 5; i++) {
print(i);
}
for (var item in userlist) {
return usercard(item);
}
while (i < 5) {
print(i);
i = i + 1;
}
}
statements
method(name: 'do', returntype: 'int',
statements: [
assign('var i', '5'),
assign('var name', call('getname')),
return('i')
]
)
generated code:
int do() {
var i = 5;
var name = getname();
return i;
}
oop concepts
class
parameter | description | output |
---|---|---|
string classname | class name | class bird |
bool isabstract? | generating abstract class if value is true | abstract class animal or class animal |
list<constructors>? | more than one constructor can be defined | singleton._init() , singleton({this.a}) : super(a) |
string? baseclass | extends to base class | class bird extends animal |
list<string>? mixins | indicates the use of mixins | class bird with feather, walk |
list<string>? interfaces | implements interface | class bird implements flyable, crowable |
list<attribute>? attributes; | attributes of class | final string name; |
list<iexpression>? methods; | all methods of class such as method, getters, settters | final string name; |
constructor
parameter | description | output |
---|---|---|
string classname | class name | class singleton |
string consturctorname? | if value is null default constructor. if not value, named constructor. | singleton._init() , singleton({this.a}) |
parameter? param | constructor parameters | singleton({required this.a}) , singleton(this.a, {this.b}) |
string? superargument | call constructor of base class | singleton(this.a) : super(a) |
string? modifier | modifier of constructor such as factory |
factory singleton() |
attribute
parameter | description | output |
---|---|---|
string name | attribute name | name |
string type | attribute type | string name |
string? modifiers | attribute modifiers | final string name |
string? value | initialize value to attribute | final string name = 'ahmet' |
methods
method
parameter | description | output |
---|---|---|
string name | method name | walk |
string returntype? | return type | void walk |
parameter? param | method parameters | void walk({required int step}) |
bool? isasync | is async method? | void walk({required int step}) async {} |
string? modifier | modifier of method such as static |
static void walk |
list<iexpression>? statements | body of method. | code here... |
getter
parameter | description | output |
---|---|---|
string name | getter name | get walk |
string returntype? | return type | void get walk |
string? modifier | modifier of method such as static |
static void get name |
list<iexpression>? statements | body of method. | code here... |
setter
parameter | description | output |
---|---|---|
string name | getter name | set name |
string param? | return type | set name(string name) |
list<iexpression>? statements | body of method. | code here... |
example class code:
class(
'bird',
baseclass: 'animal',
interfaces: ['flyable', 'crowable'],
mixins: ['feather', 'walk'],
attributes: <attribute> [
attribute(modifiers: 'final', type: 'string', name: 'name'),
],
constructors: <constructor> [
constructor(
classname: 'bird',
constructorname: 'fromname',
param: parameter([parameteritem('this.name', isrequired: true, isnamed: true)]),
superargument: argument([argumentitem('name')])
),
],
methods: [
method(
name: 'onfly',
returntype: 'double',
param: parameter([parameteritem('double height')]),
statements: [return('height * 2')]
),
]
);
generated code:
class bird extends animal with feather, walk implements flyable, crowable {
final string name;
bird.fromname({required this.name}) : super(name);
double onfly(double height) {
return height * 2;
}
}
interface
parameter | description | output |
---|---|---|
string name | interface name | interface flyable |
string? baseclass | extends class | interface flyable extends breathable |
list<iexpression>? prototypes | abstract methods of interface | void dofly(); |
example interface
interface('flyable',
baseclass: 'breathable',
prototypes: [
method(name: 'dofly', returntype: 'void')
]
)
generated code:
abstract class flyable extends breathable {
void dofly();
}
other
expression | example code | output |
---|---|---|
annotation | annotation(‘override’) | @override |
import | import(‘package:dart_writer/dart_writer.dart’, as: ‘writer’) | import 'package:dart_writer/dart_writer.dart' as writer; |
enum | enum(‘roles’, enums: [‘user’, ‘admin’, ‘developer’]) | enum roles { user, admin, developer } |
paramter | parameter([parameteritem(‘string name’, isnamed: true, isrequired: true)]) | {required string name} |
argument | argument([argumentitem(“‘star'”, name:’surname’]) | surname: 'star' |
rawcode | rawcode(‘var name = user?.name ?? “‘ahmet'”‘) | [output]: var name = user?.name ?? 'ahmet' |
task list
- [ ] unit tests
Comments are closed.