Stop passing State objects between widgets in LayoutExplorer - #10006
Stop passing State objects between widgets in LayoutExplorer#10006srawlins wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the flex layout explorer by introducing FlexLayoutExplorerScope, an InheritedWidget used to share properties and callbacks down the widget tree. This successfully removes the need to pass the state object directly to child widgets. The review feedback recommends adding defensive null checks for the nullable objectGroup in FlexChildVisualizer to avoid potential runtime crashes from using the null-assertion operator.
| void _onChangeFlexFactor(int? newFlexFactor, VoidCallback markAsDirty) async { | ||
| markAsDirty(); | ||
| await objectGroup!.invokeSetFlexFactor( | ||
| properties.node.valueRef, | ||
| newFlexFactor, | ||
| ); | ||
| } | ||
|
|
||
| void onChangeFlexFit(FlexFit? newFlexFit) async { | ||
| state.markAsDirty(); | ||
| void _onChangeFlexFit(FlexFit? newFlexFit, VoidCallback markAsDirty) async { | ||
| markAsDirty(); | ||
| await objectGroup!.invokeSetFlexFit(properties.node.valueRef, newFlexFit!); | ||
| } |
There was a problem hiding this comment.
[MUST-FIX] Defensive Null Check for objectGroup
The objectGroup getter returns a nullable ObjectGroup?. Using the null-assertion operator (!) on it can lead to runtime crashes if the object group is null (e.g., during transitions or if the inspector service is disconnected).
We should safely bind objectGroup to a local variable, perform a null check, and return early if it is null.
void _onChangeFlexFactor(int? newFlexFactor, VoidCallback markAsDirty) async {
final group = objectGroup;
if (group == null) return;
markAsDirty();
await group.invokeSetFlexFactor(
properties.node.valueRef,
newFlexFactor,
);
}
void _onChangeFlexFit(FlexFit? newFlexFit, VoidCallback markAsDirty) async {
final group = objectGroup;
if (group == null) return;
markAsDirty();
await group.invokeSetFlexFit(properties.node.valueRef, newFlexFit!);
}Replace passing FlexLayoutExplorerWidgetState to VisualizeFlexChildren and FlexChildVisualizer with _FlexLayoutExplorerScope, an InheritedWidget providing rootProperties, animation controllers, and mutation/selection callbacks. Fixes flutter#2701
0e24e2e to
bff9f44
Compare
Replaces passing
FlexLayoutExplorerWidgetStatedown toVisualizeFlexChildrenandFlexChildVisualizerwithFlexLayoutExplorerScope, anInheritedWidget.FlexLayoutExplorerScopeprovides descendants with scoped access torootProperties, the entrance animation (entranceControllerandentranceCurve), and the necessary interaction callbacks (markAsDirty,onTap, andonDoubleTap). This eliminates passing mutableStateobjects down the widget tree while avoiding prop drilling and preserving all existing layout visualizer behaviors.Fixes #2701