Skip to content

Stop passing State objects between widgets in LayoutExplorer - #10006

Open
srawlins wants to merge 1 commit into
flutter:masterfrom
srawlins:issue-2701
Open

Stop passing State objects between widgets in LayoutExplorer#10006
srawlins wants to merge 1 commit into
flutter:masterfrom
srawlins:issue-2701

Conversation

@srawlins

@srawlins srawlins commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Replaces passing FlexLayoutExplorerWidgetState down to VisualizeFlexChildren and FlexChildVisualizer with FlexLayoutExplorerScope, an InheritedWidget.

FlexLayoutExplorerScope provides descendants with scoped access to rootProperties, the entrance animation (entranceController and entranceCurve), and the necessary interaction callbacks (markAsDirty, onTap, and onDoubleTap). This eliminates passing mutable State objects down the widget tree while avoiding prop drilling and preserving all existing layout visualizer behaviors.

Fixes #2701

build.yaml badge

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +596 to 607
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!);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

[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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stop passing State objects between widgets in the LayoutExplorer

1 participant