Skip to content

Commit 1901352

Browse files
committed
Simulator #8
1 parent 67b2d29 commit 1901352

File tree

4 files changed

+119
-19
lines changed

4 files changed

+119
-19
lines changed

lib/src/transformers/node_transformer.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class WidgetBuildSettings extends BuildSettings {
7878
/// Defines what to do when a variable path results in a null value.
7979
final NullSubstitutionMode nullSubstitutionMode;
8080

81+
/// Determines whether the transformer manager should skip wrapping the widget
82+
/// with any additional widgets such as listeners, rotation, margin,
83+
/// constraints, etc.
84+
final bool buildRawWidget;
85+
8186
/// Creates a [WidgetBuildSettings] instance.
8287
const WidgetBuildSettings({
8388
super.withOpacity,
@@ -93,6 +98,7 @@ class WidgetBuildSettings extends BuildSettings {
9398
required this.debugLabel,
9499
this.nullSubstitutionMode = NullSubstitutionMode.noChange,
95100
this.replaceVariablesWithSymbols = false,
101+
this.buildRawWidget = false,
96102
});
97103

98104
/// Creates a copy of this [WidgetBuildSettings] instance.
@@ -110,6 +116,7 @@ class WidgetBuildSettings extends BuildSettings {
110116
String? debugLabel,
111117
NullSubstitutionMode? nullSubstitutionMode,
112118
bool? replaceVariablesWithSymbols,
119+
bool? buildRawWidget,
113120
}) {
114121
return WidgetBuildSettings(
115122
withOpacity: withOpacity ?? this.withOpacity,
@@ -126,6 +133,7 @@ class WidgetBuildSettings extends BuildSettings {
126133
nullSubstitutionMode: nullSubstitutionMode ?? this.nullSubstitutionMode,
127134
replaceVariablesWithSymbols:
128135
replaceVariablesWithSymbols ?? this.replaceVariablesWithSymbols,
136+
buildRawWidget: buildRawWidget ?? this.buildRawWidget,
129137
);
130138
}
131139

@@ -135,6 +143,7 @@ class WidgetBuildSettings extends BuildSettings {
135143
debugLabel,
136144
nullSubstitutionMode,
137145
replaceVariablesWithSymbols,
146+
buildRawWidget,
138147
];
139148
}
140149

lib/src/transformers/node_transformers/passive_app_bar_transformer.dart

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ class PassiveAppBarTransformer extends NodeWidgetTransformer<AppBarNode> {
99
PassiveAppBarTransformer(super.getNode, super.manager);
1010

1111
@override
12-
Widget buildWidget(
12+
PreferredSizeWidget buildWidget(
1313
AppBarNode node,
1414
BuildContext context,
1515
WidgetBuildSettings settings,
1616
) {
17+
if (settings.buildRawWidget) {
18+
return buildRawAppBar(context, node: node, settings: settings);
19+
}
20+
1721
return PassiveAppBarWidget(
1822
node: node,
1923
settings: settings,
@@ -22,6 +26,71 @@ class PassiveAppBarTransformer extends NodeWidgetTransformer<AppBarNode> {
2226
);
2327
}
2428

29+
PreferredSizeWidget buildRawAppBar(
30+
BuildContext context, {
31+
required AppBarNode node,
32+
WidgetBuildSettings settings = const WidgetBuildSettings(
33+
debugLabel: 'buildPreview',
34+
replaceVariablesWithSymbols: true,
35+
),
36+
List<VariableData> variablesOverrides = const [],
37+
}) {
38+
final Widget? leading = node.properties.leading.icon.show &&
39+
!node.properties.automaticallyImplyLeading
40+
? retrieveIconWidget(node.properties.leading.icon, null)
41+
: null;
42+
43+
final Widget? title = node.properties.title.trim().isEmpty
44+
? null
45+
: TextUtils.buildText(
46+
context,
47+
node.properties.title,
48+
textAlignHorizontal: null,
49+
maxLines: null,
50+
overflow: null,
51+
node: node,
52+
variablesOverrides: variablesOverrides,
53+
nullSubstitutionMode: settings.nullSubstitutionMode,
54+
replaceVariablesWithSymbol: settings.replaceVariablesWithSymbols,
55+
);
56+
return AppBar(
57+
centerTitle: node.properties.centerTitle,
58+
leading: leading != null
59+
? IconButton(
60+
onPressed: () => onTriggerAction.call(
61+
context, node.properties.leading.reactions),
62+
icon: leading,
63+
)
64+
: null,
65+
titleTextStyle: TextUtils.retrieveTextStyleFromProp(
66+
node.properties.titleStyle,
67+
effects: const [],
68+
),
69+
backgroundColor: node.properties.backgroundColor.toFlutterColor(),
70+
elevation: node.properties.elevation,
71+
automaticallyImplyLeading: node.properties.leading.icon.show
72+
? node.properties.automaticallyImplyLeading
73+
: false,
74+
title: title,
75+
foregroundColor:
76+
node.properties.titleStyle.fills.firstOrNull?.toFlutterColor(),
77+
titleSpacing: node.properties.titleSpacing,
78+
shadowColor: node.properties.shadowColor.toFlutterColor(),
79+
actions: [
80+
for (final item
81+
in node.properties.actions.whereType<IconAppBarActionItem>())
82+
IconButton(
83+
onPressed: () => onTriggerAction.call(context, item.reactions),
84+
// splashRadius: 20,
85+
iconSize: item.icon.size,
86+
tooltip: item.tooltip,
87+
icon: retrieveIconWidget(item.icon, item.icon.size) ??
88+
const SizedBox.shrink(),
89+
),
90+
],
91+
);
92+
}
93+
2594
void onTriggerAction(BuildContext context, List<Reaction> reactions) async {
2695
final bool executed = await FunctionsRepository.triggerAction(
2796
context, reactions: reactions, TriggerType.click);
@@ -83,6 +152,8 @@ class PassiveAppBarTransformer extends NodeWidgetTransformer<AppBarNode> {
83152
}
84153
}
85154

155+
typedef AppBarBuilder = PreferredSizeWidget Function(BuildContext context);
156+
86157
class PassiveAppBarWidget extends StatelessWidget
87158
implements PreferredSizeWidget {
88159
final AppBarNode node;
@@ -97,13 +168,16 @@ class PassiveAppBarWidget extends StatelessWidget
97168
const PassiveAppBarWidget({
98169
super.key,
99170
required this.node,
100-
this.elevation,
101171
required this.settings,
172+
this.elevation,
102173
this.onLeadingPressed,
103174
this.onActionPressed,
104175
this.variablesOverrides = const [],
105176
});
106177

178+
@override
179+
Size get preferredSize => node.outerBoxLocal.size.flutterSize;
180+
107181
@override
108182
Widget build(BuildContext context) {
109183
final Widget? leading = node.properties.leading.icon.show &&
@@ -164,7 +238,4 @@ class PassiveAppBarWidget extends StatelessWidget
164238
),
165239
);
166240
}
167-
168-
@override
169-
Size get preferredSize => node.basicBoxLocal.size.flutterSize;
170241
}

lib/src/transformers/node_transformers/passive_canvas_transformer.dart

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,38 @@ class PassiveCanvasTransformer extends NodeWidgetTransformer<CanvasNode> {
2828

2929
final BaseNode appBarNode = getNode(childId);
3030

31-
Widget? appBarChild = manager.buildWidgetByID(
32-
appBarPlaceholderNode.id,
33-
context,
34-
settings: settings,
35-
);
31+
final NodeWidgetTransformer<BaseNode> transformer =
32+
manager.getTransformerByNode(appBarNode);
3633

37-
if (appBarNode is! AppBarNode) {
38-
// wrap with SafeArea if not an AppBarNode.
39-
appBarChild = SafeArea(child: appBarChild);
40-
}
34+
if (transformer is PassiveAppBarTransformer) {
35+
final Widget appBarChild = manager.buildWidgetFromNode(
36+
appBarNode as AppBarNode,
37+
context,
38+
settings: settings.copyWith(
39+
// Passes through to build a PreferredSizeWidget directly.
40+
buildRawWidget: true,
41+
),
42+
);
4143

42-
return PreferredSize(
43-
preferredSize:
44-
Size.fromHeight(appBarPlaceholderNode.outerBoxLocal.height),
45-
child: SafeArea(child: appBarChild),
46-
);
44+
assert(
45+
appBarChild is PreferredSizeWidget,
46+
'PassiveAppBarTransformer must return a PreferredSizeWidget',
47+
);
48+
49+
return appBarChild as PreferredSizeWidget;
50+
} else {
51+
final Widget appBarChild = manager.buildWidgetFromNode(
52+
appBarNode,
53+
context,
54+
settings: settings,
55+
);
56+
57+
return PreferredSize(
58+
preferredSize:
59+
Size.fromHeight(appBarPlaceholderNode.outerBoxLocal.height),
60+
child: SafeArea(child: appBarChild),
61+
);
62+
}
4763
}
4864

4965
Widget? getNavigationBar(

lib/src/transformers/passive_transformer_manager.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ class PassiveNodeTransformerManager extends WidgetNodeTransformerManager {
6565
BuildContext context, {
6666
required WidgetBuildSettings settings,
6767
}) {
68+
if (settings.buildRawWidget) {
69+
return getTransformerByNode(node).buildWidget(node, context, settings);
70+
}
71+
6872
return _wrapWithListener(
6973
context,
7074
node: node,

0 commit comments

Comments
 (0)