-
-
Notifications
You must be signed in to change notification settings - Fork 82
refactor: Simplify StacRadio and StacRadioGroup implementations #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bb75623
e6700af
9ed168a
90201d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:flutter/cupertino.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:flutter/material.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:flutter/widgets.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:stac/src/parsers/core/stac_action_parser.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:stac/src/parsers/core/stac_widget_parser.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:stac/src/parsers/widgets/stac_form/stac_form_scope.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:stac/src/parsers/widgets/stac_radio_group/stac_radio_group_scope.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:stac_core/stac_core.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:stac_framework/stac_framework.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -18,63 +17,71 @@ class StacRadioGroupParser extends StacParser<StacRadioGroup> { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Widget parse(BuildContext context, StacRadioGroup model) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return _RadioGroupWidget( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model: model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| formScope: StacFormScope.of(context), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return _RadioGroupWidget(model, StacFormScope.of(context)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class _RadioGroupWidget extends StatefulWidget { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const _RadioGroupWidget({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| required this.model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| required this.formScope, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const _RadioGroupWidget(this.model, this.formScope); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final StacRadioGroup model; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final StacFormScope? formScope; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| State<_RadioGroupWidget> createState() => __RadioGroupWidgetState(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| State<_RadioGroupWidget> createState() => _RadioGroupWidgetState(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class __RadioGroupWidgetState extends State<_RadioGroupWidget> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| late ValueNotifier<dynamic> groupValue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class _RadioGroupWidgetState extends State<_RadioGroupWidget> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dynamic _groupValue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void initState() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| super.initState(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| groupValue = ValueNotifier<dynamic>(widget.model.groupValue); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _saveValueInFormData(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setState(() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _groupValue = widget.model.groupValue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Initialize form data if id is provided | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (widget.model.id != null && widget.formScope != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| widget.formScope!.formData[widget.model.id!] = widget.model.groupValue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
37
to
48
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Remove Calling Apply this diff to fix the issue: @override
void initState() {
super.initState();
- setState(() {
- _groupValue = widget.model.groupValue;
- });
+ _groupValue = widget.model.groupValue;
// Initialize form data if id is provided
if (widget.model.id != null && widget.formScope != null) {
widget.formScope!.formData[widget.model.id!] = widget.model.groupValue;
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void dispose() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| groupValue.dispose(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| super.dispose(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void didUpdateWidget(covariant _RadioGroupWidget oldWidget) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| super.didUpdateWidget(oldWidget); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (oldWidget.model.groupValue != widget.model.groupValue) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _groupValue = widget.model.groupValue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void _updateGroupValue(dynamic value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| groupValue.value = value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _saveValueInFormData(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Save to form data if id is provided | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (widget.model.id != null && widget.formScope != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| widget.formScope!.formData[widget.model.id!] = widget.model.groupValue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
50
to
61
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Wrap Line 52 updates Apply this diff to trigger a rebuild: void didUpdateWidget(covariant _RadioGroupWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.model.groupValue != widget.model.groupValue) {
- _groupValue = widget.model.groupValue;
+ setState(() {
+ _groupValue = widget.model.groupValue;
+ });
// Save to form data if id is provided
if (widget.model.id != null && widget.formScope != null) {
widget.formScope!.formData[widget.model.id!] = widget.model.groupValue;
}
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void _saveValueInFormData() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (widget.model.id != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| widget.formScope?.formData[widget.model.id!] = groupValue.value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void _onChanged(dynamic value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setState(() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _groupValue = value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Save to form data if id is provided | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (widget.model.id != null && widget.formScope != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| widget.formScope!.formData[widget.model.id!] = value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Call the onChanged action if provided | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (widget.model.onChanged != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| widget.model.onChanged!.parse(context); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Widget build(BuildContext context) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final StacRadioGroup model = widget.model; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return StacRadioGroupScope( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| radioGroupValue: groupValue, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onSelect: _updateGroupValue, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| child: Builder(builder: (context) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return model.child?.parse(context) ?? const SizedBox(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return RadioGroup<dynamic>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| groupValue: _groupValue, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onChanged: _onChanged, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| child: widget.model.child?.parse(context) ?? const SizedBox.shrink(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass the group wiring to every
Radio.CupertinoRadio,Radio.adaptive, andRadioall requiregroupValueandonChanged. After removingStacRadioGroupScope, these calls no longer compile and, even if they did, taps couldn’t update the group. Please fetch the current group wiring from the newRadioGroupwrapper and feed it into each constructor.Apply this diff to restore the required wiring:
@override Widget build(BuildContext context) { - switch (widget.model.radioType ?? StacRadioType.material) { + final radioGroup = RadioGroup.of<dynamic>(context); + switch (widget.model.radioType ?? StacRadioType.material) { case StacRadioType.cupertino: - return _buildCupertinoRadio(context); + return _buildCupertinoRadio(context, radioGroup?.groupValue, + radioGroup?.onChanged ?? (_) {}); case StacRadioType.adaptive: - return _buildAdaptiveRadio(context); + return _buildAdaptiveRadio(context, radioGroup?.groupValue, + radioGroup?.onChanged ?? (_) {}); case StacRadioType.material: - return _buildMaterialRadio(context); + return _buildMaterialRadio(context, radioGroup?.groupValue, + radioGroup?.onChanged ?? (_) {}); } } - Widget _buildCupertinoRadio(BuildContext context) { + Widget _buildCupertinoRadio( + BuildContext context, + dynamic groupValue, + ValueChanged<dynamic> onChanged, + ) { return CupertinoRadio<dynamic>( value: widget.model.value, + groupValue: groupValue, + onChanged: onChanged, mouseCursor: widget.model.mouseCursor?.parse, toggleable: widget.model.toggleable ?? false, activeColor: widget.model.activeColor?.toColor(context), inactiveColor: widget.model.inactiveColor?.toColor(context), fillColor: widget.model.fillColor?.toColor(context), focusColor: widget.model.focusColor?.toColor(context), focusNode: _focusNode, autofocus: widget.model.autofocus ?? false, useCheckmarkStyle: widget.model.useCheckmarkStyle ?? false, enabled: widget.model.enabled, ); } - Widget _buildAdaptiveRadio(BuildContext context) { + Widget _buildAdaptiveRadio( + BuildContext context, + dynamic groupValue, + ValueChanged<dynamic> onChanged, + ) { return Radio<dynamic>.adaptive( value: widget.model.value, + groupValue: groupValue, + onChanged: onChanged, mouseCursor: widget.model.mouseCursor?.parse, toggleable: widget.model.toggleable ?? false, activeColor: widget.model.activeColor?.toColor(context), fillColor: WidgetStateProperty.all( widget.model.fillColor?.toColor(context), ), focusColor: widget.model.focusColor?.toColor(context), hoverColor: widget.model.hoverColor?.toColor(context), overlayColor: WidgetStateProperty.all( widget.model.overlayColor?.toColor(context), ), splashRadius: widget.model.splashRadius, materialTapTargetSize: widget.model.materialTapTargetSize?.parse, visualDensity: widget.model.visualDensity?.parse, focusNode: _focusNode, autofocus: widget.model.autofocus ?? false, useCupertinoCheckmarkStyle: widget.model.useCupertinoCheckmarkStyle ?? false, enabled: widget.model.enabled, backgroundColor: widget.model.backgroundColor != null ? WidgetStateProperty.all( widget.model.backgroundColor!.toColor(context), ) : null, side: widget.model.side?.parse(context), innerRadius: widget.model.innerRadius != null ? WidgetStateProperty.all(widget.model.innerRadius) : null, ); } - Widget _buildMaterialRadio(BuildContext context) { + Widget _buildMaterialRadio( + BuildContext context, + dynamic groupValue, + ValueChanged<dynamic> onChanged, + ) { return Radio<dynamic>( value: widget.model.value, + groupValue: groupValue, + onChanged: onChanged, mouseCursor: widget.model.mouseCursor?.parse, toggleable: widget.model.toggleable ?? false, activeColor: widget.model.activeColor?.toColor(context), fillColor: WidgetStateProperty.all( widget.model.fillColor?.toColor(context), ), focusColor: widget.model.focusColor?.toColor(context), hoverColor: widget.model.hoverColor?.toColor(context), overlayColor: WidgetStateProperty.all( widget.model.overlayColor?.toColor(context), ), splashRadius: widget.model.splashRadius, materialTapTargetSize: widget.model.materialTapTargetSize?.parse, visualDensity: widget.model.visualDensity?.parse, focusNode: _focusNode, autofocus: widget.model.autofocus ?? false, enabled: widget.model.enabled, backgroundColor: widget.model.backgroundColor != null ? WidgetStateProperty.all( widget.model.backgroundColor!.toColor(context), ) : null, side: widget.model.side?.parse(context), innerRadius: widget.model.innerRadius != null ? WidgetStateProperty.all(widget.model.innerRadius) : null, ); }🤖 Prompt for AI Agents