Skip to content

Commit abe25b6

Browse files
committed
Fixed Widget's onLoad not able to access inputs.
1 parent 1e8829c commit abe25b6

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

integration_test/local/defaultApp/Widget Bindings.yaml renamed to integration_test/local/defaultApp/Custom Widget.yaml

+12-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ View:
2222

2323
Custom:
2424
inputs: [txt]
25+
onLoad:
26+
showToast:
27+
message: Hello ${txt}
2528
body:
2629
Column:
2730
children:
@@ -35,6 +38,13 @@ Custom:
3538

3639
Custom2:
3740
inputs: [text]
41+
onLoad: |-
42+
setByJS.text = "Hi " + text;
43+
3844
body:
39-
Text:
40-
text: "Custom Custom Widget: ${text}"
45+
Column:
46+
children:
47+
- Text:
48+
text: "Custom Custom Widget: ${text}"
49+
- Text:
50+
id: setByJS

integration_test/local/defaultApp_test.dart

+12-2
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,22 @@ void main() {
2424
/// test that binding to a TextInput works properly in the same scope
2525
/// and also in a custom widget's scope
2626
testWidgets("Bindings to widget's value", (tester) async {
27-
await TestHelper.loadScreen(tester, 'Widget Bindings', config);
28-
// await tester.pumpAndSettle();
27+
await TestHelper.loadScreen(tester, 'Custom Widget', config);
2928

3029
// TextInput has initial value of 'first'
3130
// so first make sure our EnsembleText is correctly bind to that
3231
Finder text = find.descendant(
3332
of: find.byType(EnsembleText), matching: find.text('first'));
3433
expect(text, findsOneWidget);
3534

35+
// Custom Widget's onLoad can access inputs
36+
// search for toast message
37+
await tester.pumpAndSettle();
38+
expect(find.text('Hello first'), findsOneWidget);
39+
40+
// ensure Nested widget's onLoad can access inputs via JS too
41+
expect(find.text('Hi first'), findsOneWidget);
42+
3643
// Custom Widget's text should also bind to the same value
3744
Finder customText = find.descendant(
3845
of: find.byType(EnsembleText),
@@ -68,6 +75,9 @@ void main() {
6875
of: find.byType(EnsembleText),
6976
matching: find.text('Custom Custom Widget: second'));
7077
expect(customCustomText, findsOneWidget);
78+
79+
// ensure onLoad never run again and still retain original value
80+
expect(find.text('Hi first'), findsOneWidget);
7181
});
7282

7383
/// test bindings to API is working properly

lib/framework/widget/view_util.dart

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:ensemble/framework/error_handling.dart';
66
import 'package:ensemble/framework/event.dart';
77
import 'package:ensemble/framework/scope.dart';
88
import 'package:ensemble/framework/theme_manager.dart';
9+
import 'package:ensemble/framework/view/data_scope_widget.dart';
910
import 'package:ensemble/widget/custom_widget/custom_widget.dart';
1011
import 'package:ensemble/framework/widget/widget.dart';
1112
import 'package:ensemble/page_model.dart';
@@ -274,7 +275,12 @@ class ViewUtil {
274275
scopeNode.scope.dataContext
275276
.addInvokableContext(id, customWidget.controller);
276277
}
277-
return customWidget;
278+
279+
// wrap it inside a DataScopeWidget
280+
return DataScopeWidget(
281+
debugLabel: "CustomWidget",
282+
scopeManager: customScope,
283+
child: customWidget);
278284
}
279285

280286
static Widget buildBareWidget(ScopeNode scopeNode, WidgetModel model,

lib/layout/box/flex_box_layout.dart

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:ensemble/framework/ensemble_widget.dart';
33
import 'package:ensemble/framework/error_handling.dart';
44
import 'package:ensemble/framework/model.dart';
55
import 'package:ensemble/framework/studio/studio_debugger.dart';
6+
import 'package:ensemble/framework/view/data_scope_widget.dart';
67
import 'package:ensemble/framework/widget/has_children.dart';
78
import 'package:ensemble/framework/widget/widget.dart';
89
import 'package:ensemble/layout/box/base_box_layout.dart';
@@ -133,6 +134,10 @@ class FlexBoxLayoutState extends WidgetState<FlexBoxLayout>
133134
/// check if a widget have flex or flexMode set. This essentially means the
134135
/// widget already handles this themselves already
135136
bool hasFlex(Widget widget) {
137+
if (widget is DataScopeWidget) {
138+
widget = widget.child;
139+
}
140+
136141
// legacy widgets
137142
if (widget is HasController && widget.controller is WidgetController) {
138143
return (widget.controller as WidgetController).flex != null ||

lib/widget/custom_widget/custom_widget.dart

+3-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class CustomWidget extends EnsembleWidget<CustomWidgetController> {
1515
{super.key, required this.model, required this.scopeManager}) {
1616
log("Custom Widget $hashCode created");
1717
}
18+
1819
final CustomWidgetModel model;
1920
final ScopeManager scopeManager;
2021

@@ -83,14 +84,6 @@ class _CustomWidgetState extends EnsembleWidgetState<CustomWidget> {
8384
}
8485

8586
@override
86-
Widget buildWidget(BuildContext context) {
87-
// Note that we wrap the custom widget inside DataScopeWidget here instead
88-
// of outside because custom widget can now have flex, and the parent FlexBox
89-
// need a way to look up the flex value. CustomWidget has to the the root
90-
// for this to work (and DataScopeWidget will be inside)
91-
return DataScopeWidget(
92-
debugLabel: 'customWidget',
93-
scopeManager: widget.scopeManager,
94-
child: widget.scopeManager.buildWidget(widget.model.getModel()));
95-
}
87+
Widget buildWidget(BuildContext context) =>
88+
widget.scopeManager.buildWidget(widget.model.getModel());
9689
}

0 commit comments

Comments
 (0)