Skip to content

Commit d1db6e2

Browse files
committed
Clean up transformers for checkbox and switch transformers.
1 parent 0bd3d3a commit d1db6e2

File tree

2 files changed

+137
-154
lines changed

2 files changed

+137
-154
lines changed

lib/src/transformers/node_transformers/passive_checkbox_transformer.dart

Lines changed: 78 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -13,123 +13,120 @@ class PassiveCheckboxTransformer extends NodeWidgetTransformer<CheckboxNode> {
1313
BuildContext context,
1414
WidgetBuildSettings settings,
1515
) {
16-
return buildFromNode(context, node, settings: settings);
17-
}
18-
19-
Widget buildFromProps(
20-
BuildContext context, {
21-
required CheckboxProperties props,
22-
required double height,
23-
required double width,
24-
bool? value,
25-
required WidgetBuildSettings settings,
26-
}) {
27-
final node = CheckboxNode(
28-
id: '',
29-
name: 'Checkbox',
30-
basicBoxLocal: NodeBox(0, 0, width, height),
31-
alignment: AlignmentModel.none,
32-
properties: props,
33-
)..value = value;
34-
return buildFromNode(context, node, settings: settings);
16+
if (settings.isPreview) {
17+
bool? effectiveValue = node.value;
18+
return StatefulBuilder(
19+
builder: (context, setState) => TransformerCheckbox(
20+
node: node,
21+
settings: settings,
22+
onChanged: (context, value) => setState(() {
23+
effectiveValue = value;
24+
}),
25+
value: effectiveValue,
26+
),
27+
);
28+
} else {
29+
return PassiveCheckboxWidget(
30+
node: node,
31+
settings: settings,
32+
);
33+
}
3534
}
35+
}
3636

37-
Widget buildPreview({
38-
CheckboxProperties? properties,
39-
CheckboxNode? node,
40-
double? height,
41-
double? width,
42-
bool? value,
43-
ValueChanged<bool?>? onChanged,
44-
WidgetBuildSettings settings =
45-
const WidgetBuildSettings(debugLabel: 'buildPreview'),
46-
}) {
47-
final previewNode = CheckboxNode(
48-
properties: properties ?? node?.properties ?? CheckboxProperties(),
49-
id: '',
50-
name: 'Checkbox',
51-
basicBoxLocal: NodeBox(0, 0, width ?? 32, height ?? 32),
52-
retainedOuterBoxLocal: NodeBox(0, 0, width ?? 32, height ?? 32),
53-
);
54-
previewNode.value = value;
55-
return PassiveCheckboxWidget(
56-
node: previewNode,
57-
onChanged: (context, value) => onChanged?.call(value),
58-
settings: settings,
59-
);
60-
}
37+
class PassiveCheckboxWidget extends StatelessWidget {
38+
final CheckboxNode node;
39+
final WidgetBuildSettings settings;
40+
final List<VariableData> variables;
6141

62-
Widget buildFromNode(
63-
BuildContext context,
64-
CheckboxNode node, {
65-
required WidgetBuildSettings settings,
66-
}) {
67-
return PassiveCheckboxWidget(
68-
node: node,
69-
settings: settings,
70-
onChanged: (context, value) => onChanged(context, node, value),
71-
);
72-
}
42+
const PassiveCheckboxWidget({
43+
super.key,
44+
required this.node,
45+
required this.settings,
46+
this.variables = const [],
47+
});
7348

74-
void onChanged(BuildContext context, CheckboxNode node, bool? internalValue) {
49+
void onChanged(BuildContext context, bool? internalValue) {
7550
NodeStateProvider.setState(context, internalValue);
51+
7652
FunctionsRepository.setPropertyValue(context,
7753
node: node, property: 'value', value: internalValue);
78-
7954
FunctionsRepository.triggerAction(
8055
context, node: node, TriggerType.changed, value: internalValue);
8156
}
57+
58+
@override
59+
Widget build(BuildContext context) {
60+
final bool? value = PropertyValueDelegate.getPropertyValue<bool>(
61+
node,
62+
'value',
63+
scopedValues: ScopedValues.of(context, variablesOverrides: variables),
64+
) ??
65+
node.value;
66+
67+
return TransformerCheckbox(
68+
node: node,
69+
settings: settings,
70+
value: value,
71+
onChanged: onChanged,
72+
variables: variables,
73+
);
74+
}
8275
}
8376

84-
class PassiveCheckboxWidget extends StatelessWidget {
77+
class TransformerCheckbox extends StatefulWidget {
8578
final CheckboxNode node;
8679
final WidgetBuildSettings settings;
8780
final List<VariableData> variables;
8881
final void Function(BuildContext context, bool? value)? onChanged;
82+
final bool? value;
8983

90-
const PassiveCheckboxWidget({
84+
const TransformerCheckbox({
9185
super.key,
9286
required this.node,
9387
required this.settings,
9488
required this.onChanged,
89+
required this.value,
9590
this.variables = const [],
9691
});
9792

9893
@override
99-
Widget build(BuildContext context) {
100-
final double scale = node.basicBoxLocal.width /
101-
(node.properties.compact ? Checkbox.width : kCheckboxDefaultSize);
94+
State<TransformerCheckbox> createState() => _TransformerCheckboxState();
95+
}
10296

103-
final bool? value = PropertyValueDelegate.getPropertyValue<bool>(
104-
node,
105-
'value',
106-
scopedValues: ScopedValues.of(context, variablesOverrides: variables),
107-
) ??
108-
node.value;
97+
class _TransformerCheckboxState extends State<TransformerCheckbox> {
98+
@override
99+
Widget build(BuildContext context) {
100+
final double scale = widget.node.basicBoxLocal.width /
101+
(widget.node.properties.compact
102+
? Checkbox.width
103+
: kCheckboxDefaultSize);
109104

110105
return SizedBox.fromSize(
111-
size: node.basicBoxLocal.size.flutterSize,
106+
size: widget.node.basicBoxLocal.size.flutterSize,
112107
child: Transform.scale(
113108
scale: scale,
114109
child: Checkbox(
115110
key: ValueKey(
116-
'${node.id}-${IndexedItemProvider.maybeOf(context)?.index ?? ''}'),
117-
value: node.properties.tristate ? value : (value ?? false),
118-
tristate: node.properties.tristate,
119-
autofocus: node.properties.autofocus,
120-
checkColor: node.properties.checkColor.toFlutterColor(),
121-
activeColor: node.properties.activeColor.toFlutterColor(),
122-
hoverColor: node.properties.hoverColor.toFlutterColor(),
123-
focusColor: node.properties.focusColor.toFlutterColor(),
124-
onChanged: (value) => onChanged?.call(context, value),
111+
'${widget.node.id}-${IndexedItemProvider.maybeOf(context)?.index ?? ''}'),
112+
value: widget.node.properties.tristate
113+
? widget.value
114+
: (widget.value ?? false),
115+
tristate: widget.node.properties.tristate,
116+
autofocus: widget.node.properties.autofocus,
117+
checkColor: widget.node.properties.checkColor.toFlutterColor(),
118+
activeColor: widget.node.properties.activeColor.toFlutterColor(),
119+
hoverColor: widget.node.properties.hoverColor.toFlutterColor(),
120+
focusColor: widget.node.properties.focusColor.toFlutterColor(),
121+
onChanged: (value) => widget.onChanged?.call(context, value),
125122
visualDensity: VisualDensity.standard,
126-
splashRadius: node.properties.splashRadius,
123+
splashRadius: widget.node.properties.splashRadius,
127124
shape: RoundedRectangleBorder(
128-
borderRadius: node.properties.cornerRadius.borderRadius,
125+
borderRadius: widget.node.properties.cornerRadius.borderRadius,
129126
),
130127
side: BorderSide(
131-
color: node.properties.borderColor.toFlutterColor(),
132-
width: node.properties.borderWidth,
128+
color: widget.node.properties.borderColor.toFlutterColor(),
129+
width: widget.node.properties.borderWidth,
133130
),
134131
),
135132
),

lib/src/transformers/node_transformers/passive_switch_transformer.dart

Lines changed: 59 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -13,89 +13,48 @@ class PassiveSwitchTransformer extends NodeWidgetTransformer<SwitchNode> {
1313
BuildContext context,
1414
WidgetBuildSettings settings,
1515
) {
16-
return buildFromNode(context, node, settings);
17-
}
18-
19-
Widget buildFromProps(
20-
BuildContext context, {
21-
required SwitchProperties props,
22-
required double height,
23-
required double width,
24-
bool value = false,
25-
required WidgetBuildSettings settings,
26-
}) {
27-
final node = SwitchNode(
28-
id: '',
29-
name: 'Switch',
30-
basicBoxLocal: NodeBox(0, 0, width, height),
31-
retainedOuterBoxLocal: NodeBox(0, 0, width, height),
32-
properties: props,
33-
value: value,
34-
);
35-
return buildFromNode(context, node, settings);
36-
}
37-
38-
Widget buildPreview({
39-
SwitchProperties? properties,
40-
SwitchNode? node,
41-
double? height,
42-
double? width,
43-
bool value = false,
44-
ValueChanged<bool>? onChanged,
45-
WidgetBuildSettings settings =
46-
const WidgetBuildSettings(debugLabel: 'buildPreview'),
47-
}) {
48-
final previewNode = SwitchNode(
49-
properties: properties ?? node?.properties ?? SwitchProperties(),
50-
id: '',
51-
name: 'Switch',
52-
basicBoxLocal: NodeBox(0, 0, width ?? 32, height ?? 32),
53-
retainedOuterBoxLocal: NodeBox(0, 0, width ?? 32, height ?? 32),
54-
);
55-
previewNode.value = value;
56-
return PassiveSwitchWidget(
57-
node: previewNode,
58-
onChanged: (context, value) => onChanged?.call(value),
59-
settings: settings,
60-
);
61-
}
62-
63-
Widget buildFromNode(
64-
BuildContext context,
65-
SwitchNode node,
66-
WidgetBuildSettings settings,
67-
) {
68-
return PassiveSwitchWidget(
69-
node: node,
70-
settings: settings,
71-
onChanged: (context, value) => onChanged(context, node, value),
72-
);
73-
}
74-
75-
void onChanged(BuildContext context, SwitchNode node, bool internalValue) {
76-
NodeStateProvider.setState(context, internalValue);
77-
FunctionsRepository.setPropertyValue(context,
78-
node: node, property: 'value', value: internalValue);
79-
80-
FunctionsRepository.triggerAction(
81-
context, node: node, TriggerType.changed, value: internalValue);
16+
if (settings.isPreview) {
17+
bool effectiveValue = node.value;
18+
return StatefulBuilder(
19+
builder: (context, setState) => TransformerSwitch(
20+
node: node,
21+
settings: settings,
22+
onChanged: (context, value) => setState(() {
23+
effectiveValue = value;
24+
}),
25+
value: effectiveValue,
26+
),
27+
);
28+
} else {
29+
return PassiveSwitchWidget(
30+
node: node,
31+
settings: settings,
32+
);
33+
}
8234
}
8335
}
8436

8537
class PassiveSwitchWidget extends StatelessWidget {
8638
final SwitchNode node;
8739
final WidgetBuildSettings settings;
8840
final List<VariableData> variablesOverrides;
89-
final void Function(BuildContext context, bool value)? onChanged;
9041

9142
const PassiveSwitchWidget({
9243
super.key,
9344
required this.node,
9445
required this.settings,
95-
this.onChanged,
9646
this.variablesOverrides = const [],
9747
});
9848

49+
void onChanged(BuildContext context, bool internalValue) {
50+
NodeStateProvider.setState(context, internalValue);
51+
52+
FunctionsRepository.setPropertyValue(context,
53+
node: node, property: 'value', value: internalValue);
54+
FunctionsRepository.triggerAction(
55+
context, node: node, TriggerType.changed, value: internalValue);
56+
}
57+
9958
@override
10059
Widget build(BuildContext context) {
10160
final ScopedValues scopedValues = ScopedValues.of(
@@ -108,12 +67,39 @@ class PassiveSwitchWidget extends StatelessWidget {
10867
scopedValues: scopedValues,
10968
) ??
11069
node.value;
111-
// final bool value = variables.getBooleanById(node.variables['value'] ?? '',
112-
// defaultValue: node.value);
70+
71+
return TransformerSwitch(
72+
node: node,
73+
settings: settings,
74+
variablesOverrides: variablesOverrides,
75+
value: value,
76+
onChanged: onChanged,
77+
);
78+
}
79+
}
80+
81+
class TransformerSwitch extends StatelessWidget {
82+
final SwitchNode node;
83+
final WidgetBuildSettings settings;
84+
final List<VariableData> variablesOverrides;
85+
final void Function(BuildContext context, bool value)? onChanged;
86+
final bool value;
87+
88+
const TransformerSwitch({
89+
super.key,
90+
required this.node,
91+
required this.settings,
92+
required this.onChanged,
93+
required this.value,
94+
this.variablesOverrides = const [],
95+
});
96+
97+
@override
98+
Widget build(BuildContext context) {
11399
final scale = node.basicBoxLocal.width / kSwitchDefaultWidth;
114-
return SizedBox(
115-
width: node.basicBoxLocal.width,
116-
height: node.basicBoxLocal.height,
100+
101+
return AdaptiveNodeBox(
102+
node: node,
117103
child: Transform.scale(
118104
scale: scale,
119105
child: Theme(

0 commit comments

Comments
 (0)