Skip to content

Commit cd2dcee

Browse files
committed
add super parameter
1 parent 27bbad0 commit cd2dcee

4 files changed

Lines changed: 33 additions & 11 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class HomeController extends HomeControllerBase {
127127
128128
//
129129
// here you can add you own logic and call the variables and methods
130-
// from the XML file. e.g. <Text text="ctrl.myText" />
130+
// within the XML file. e.g. <Text text="ctrl.myText" />
131131
//
132132
133133
@override
@@ -155,6 +155,7 @@ class HomeController extends HomeControllerBase {
155155
### 2. [Pipes](./docs/pipes.md)
156156
### 3. [Custom properties](./docs/custom-properties.md)
157157
### 4. [Injecting providers](./docs/providers.md)
158+
### 4. [Parameters](./docs/parameters.md)
158159
### 5. [Adding controllers to widgets](./docs/controllers.md)
159160
### 6. [Adding mixin to widget's states](./docs/mixins.md)
160161
### 7. [Localization](./docs/localization.md)

docs/parameters.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11

22

33
# Passing parameters to widgets
4-
Parameters are used to pass information from one widget to another, and since you can't modify the widget code (generated file), the XML structure enables you to declare the parameter(s) as follow:
4+
Parameters are used to pass data from one widget to another, and since you can't modify the widget code (generated file), the XML structure enables you to declare the parameter(s) as follow:
55
```XML
66
<MyPage controller="MyController">
77
<param type="CategoryModel" name="category" />
8+
9+
<param type="int" name="index" required="true" />
10+
<!--
11+
final int index;
12+
MyPage({@required this.index})
13+
-->
14+
15+
<param type="UniqueKey" name="key" superParamName="key" />
16+
<!--
17+
final UniqueKey key;
18+
MyPage({this.key}) : super(key: key);
19+
-->
20+
21+
<param type="UniqueKey" superParamName="key" />
22+
<!-- MyPage({UniqueKey key}) : super(key: key) -->
823
...
924
</MyPage>
1025
```
1126

12-
And you can access it directly inside XML:
27+
And you can access it directly in the XML file:
1328
```XML
1429
<Text text="category.name" />
1530
```

src/generators/class-generator.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class ClassCodeGenerator {
125125
...controllers.filter(a => !a.isPrivate && !a.skipGenerate).map(a => this.createControllerVar(a)),
126126
...vars.map(a => a.type ? `final ${a.name} = new ${a.type}();` : a.name),
127127
...rootWidget.vars.map(a => this.createControllerVar(a)),
128-
...rootWidget.params.map(a => this.createControllerVar(a)),
128+
...rootWidget.params.filter(a => !!a.name).map(a => this.createControllerVar(a)),
129129
...rootWidget.providers.map(a => this.createControllerVar(a))
130130
];
131131
const disposeLines = [
@@ -187,9 +187,9 @@ class ${rootWidget.controller}Base {
187187
return `
188188
189189
class ${widgetName} extends StatelessWidget${mixinsCode} {
190-
${rootWidget.params.map(a => `final ${a.type ? a.type + ' ' : ''}${a.name}${a.value !== undefined ? ' = ' + a.value : ''};`).join('\n ')}
190+
${rootWidget.params.filter(a => !!a.name).map(a => `final ${a.type ? a.type + ' ' : ''}${a.name}${a.value !== undefined ? ' = ' + a.value : ''};`).join('\n ')}
191191
${widgetName}(${rootWidget.params.length ? '{': ''}
192-
${rootWidget.params.map(a => `${a.required ? '@required ' : ''}this.${a.name}`).join(',\n ')}
192+
${rootWidget.params.map(a => `${a.required ? '@required ' : ''}${a.name ? `this.${a.name}` : `${(a.type ? a.type + ' ' : '')}${a.superParamName}`}`).join(',\n ')}
193193
${rootWidget.params.length ? '}': ''});
194194
${buildMethodContent}
195195
}
@@ -206,19 +206,24 @@ class ${widgetName} extends StatelessWidget${mixinsCode} {
206206
];
207207
const stateVarsInit: string[] = [
208208
...(hasController ? [`ctrl = new ${rootWidget.controller}();`] : []),
209-
...rootWidget.params.map(a => `ctrl._${a.name} = widget.${a.name};`),
209+
...rootWidget.params.filter(a => !!a.name).map(a => `ctrl._${a.name} = widget.${a.name};`),
210210
...controllers.filter(a => !a.isPrivate && !a.skipGenerate).map(a => `${hasController ? `ctrl._${a.name} = `: ''}${a.name} = ${a.value ? a.value : `new ${a.type}()`};`),
211211
...rootWidget.vars.map(a => `${hasController ? `ctrl._${a.name} = `: ''}${a.name} = ${a.value};`),
212212
...(hasController ? [`WidgetsBinding.instance.addPostFrameCallback((_) => ctrl.afterFirstBuild(context));`] : [])
213213
];
214+
const superParams = rootWidget.params
215+
.filter(a => a.superParamName)
216+
.map(a => `${a.superParamName}: ${a.name || a.superParamName}`)
217+
.join(', ');
218+
const superCtor = superParams ? ` : super(${superParams})` : '';
214219

215220
return `
216221
217222
class ${widgetName} extends StatefulWidget {
218-
${rootWidget.params.map(a => `final ${a.type ? a.type + ' ' : ''}${a.name}${a.value !== undefined ? ' = ' + a.value : ''};`).join('\n ')}
223+
${rootWidget.params.filter(a => !!a.name).map(a => `final ${a.type ? a.type + ' ' : ''}${a.name}${a.value !== undefined ? ' = ' + a.value : ''};`).join('\n ')}
219224
${widgetName}(${rootWidget.params.length ? '{': ''}
220-
${rootWidget.params.map(a => `${a.required ? '@required ' : ''}this.${a.name}`).join(',\n ')}
221-
${rootWidget.params.length ? '}': ''});
225+
${rootWidget.params.map(a => `${a.required ? '@required ' : ''}${a.name ? `this.${a.name}` : `${(a.type ? a.type + ' ' : '')}${a.superParamName}`}`).join(',\n ')}
226+
${rootWidget.params.length ? '}': ''})${superCtor};
222227
223228
@override
224229
_${widgetName}State createState() => _${widgetName}State();

src/resolvers/widget-resolver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ export class WidgetResolver {
119119
type: n.attributes['type'] || 'dynamic',
120120
name: n.attributes['name'],
121121
value: n.attributes['value'],
122+
superParamName: n.attributes['superParamName'],
122123
required: 'required' in n.attributes
123124
};
124-
}).filter(a => !!a.name);
125+
}).filter(a => !!a.name || !!a.superParamName);
125126

126127
return params;
127128
}

0 commit comments

Comments
 (0)