Skip to content
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

Expose the Axis of MultiChildRenderObjectWidget's to Their Children #133394

Open
2 tasks done
caseycrogers opened this issue Aug 26, 2023 · 0 comments
Open
2 tasks done

Expose the Axis of MultiChildRenderObjectWidget's to Their Children #133394

caseycrogers opened this issue Aug 26, 2023 · 0 comments
Labels
c: new feature Nothing broken; request for a new capability c: proposal A detailed proposal for a change to Flutter framework flutter/packages/flutter repository. See also f: labels. P2 Important issues not at the top of the work list team-framework Owned by Framework team triaged-framework Triaged by Framework team

Comments

@caseycrogers
Copy link
Contributor

caseycrogers commented Aug 26, 2023

Is there an existing issue for this?

Use case

There are situations where a child widget will want to know the axis of the nearest enclosing axis-aligned widget (Row, Column, Scrollable, etc.).

The example most relevant to me is in my flutter_gutter package. It has a Gap widget that is just a SizedBox with a non-zero height if the nearest relevant enclosing widget is vertically aligned and non-zero width if the nearest is horizontally aligned. The way it does this is super hacky:
It visits ancestor widgets and checks them against a list of known axis-aligned widget types: Row, Column, Scrollable, etc.
The biggest downside is that Gap will not notice the axis of widgets not explicitly included in my checks, either because I forgot to add them or because they're in an external package (ie BoxyRow/BoxyColumn).

Proposal

Have all axis-aligned widgets expose their axis in a consistent manner. Not entirely sure on the appropriate implementation, but here are two possible approaches:

  1. Create abstract class AxisAligned { Axis get axis; } and have the relevant widgets implement this class and override the getter appropriately. Child widgets can crawl their ancestors:
Axis? getAxis(BuildContext context) {
    Axis? axis;
    context.visitAncestorElements((element) {
      final Widget widget = element.widget;
      if (widget is AxisAligned) {
        axis = widget.axis;
        return false;
      }
      return true;
    });
    return axis;
}

1a. Same as above, but abstract class AxisAlignedMultiChildRenderObject extends MultiChildRenderObject...
2. Create an inherited widget called AxisScope equivalent to _ScrollableScope (in this case _ScrollableScope would probably get refactored to be a sub class of AxisScope) that exposes Axis via an inherited Widget and have all the relevant widgets embed the scope in their widget trees:

Axis? getAxis(BuildContext context) {
    // `maybeOf` is just a convenience wrapper around `dependOnInheritedWidget...`
    return AxisScope.maybeOf(context).axis;
}

While the Flutter team of course isn't responsible for external packages, this would at least set a consistent standard and API within the Flutter/flutter repo. Then external packages could update to use this API so that my package and others could use it and know that all Flutter and participating external packages would be properly supported.

@caseycrogers caseycrogers changed the title Expose The Axis of Relevant MultiChildLayout's to Their Children Expose the Axis of Relevant MultiChildLayout's to Their Children Aug 27, 2023
@caseycrogers caseycrogers changed the title Expose the Axis of Relevant MultiChildLayout's to Their Children Expose the Axis of MultiChildLayout's to Their Children Aug 27, 2023
@caseycrogers caseycrogers changed the title Expose the Axis of MultiChildLayout's to Their Children Expose the Axis of MultiChildRenderObjectWidget's to Their Children Aug 27, 2023
@danagbemava-nc danagbemava-nc added in triage Presently being triaged by the triage team c: new feature Nothing broken; request for a new capability framework flutter/packages/flutter repository. See also f: labels. c: proposal A detailed proposal for a change to Flutter team-framework Owned by Framework team and removed in triage Presently being triaged by the triage team labels Aug 28, 2023
@Piinks Piinks added P2 Important issues not at the top of the work list triaged-framework Triaged by Framework team labels Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: new feature Nothing broken; request for a new capability c: proposal A detailed proposal for a change to Flutter framework flutter/packages/flutter repository. See also f: labels. P2 Important issues not at the top of the work list team-framework Owned by Framework team triaged-framework Triaged by Framework team
Projects
None yet
Development

No branches or pull requests

3 participants