Skip to content

Commit

Permalink
Add covariants to reduce subclass casts in 2D APIs (flutter#141318)
Browse files Browse the repository at this point in the history
While working in https://pub.dev/packages/two_dimensional_scrollables, I found I could eliminate some casts if we added covariants here in the framework.
Much of the 2D aPI in the framework is abstract, so I think it make sense to add these and make it easier/cleaner for subclasses using the APIs.
I made a similar change in flutter#131358, this would cover all of the cases I could find so its nice and accommodating now.
  • Loading branch information
Piinks committed Jan 10, 2024
1 parent 30b0f72 commit a94c14e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/widgets/scroll_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ abstract class TwoDimensionalChildDelegate extends ChangeNotifier {
/// widgets have changed, a new delegate must be provided, and the new
/// delegate's [shouldRebuild] method must return true. Alternatively,
/// calling [notifyListeners] will allow the same delegate to be used.
Widget? build(BuildContext context, ChildVicinity vicinity);
Widget? build(BuildContext context, covariant ChildVicinity vicinity);

/// Called whenever a new instance of the child delegate class is
/// provided.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ abstract class RenderTwoDimensionalViewport extends RenderBox implements RenderA
///
/// Returns null if there is no active child for the given [ChildVicinity].
@protected
RenderBox? getChildFor(ChildVicinity vicinity) => _children[vicinity];
RenderBox? getChildFor(covariant ChildVicinity vicinity) => _children[vicinity];

@override
void attach(PipelineOwner owner) {
Expand Down
37 changes: 37 additions & 0 deletions packages/flutter/test/widgets/two_dimensional_viewport_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2714,6 +2714,38 @@ void main() {
});
}

class _TestVicinity extends ChildVicinity {
const _TestVicinity({required super.xIndex, required super.yIndex});
}

class _TestBaseDelegate extends TwoDimensionalChildDelegate { //ignore: unused_element
// Would fail analysis without covariant
@override
Widget? build(BuildContext context, _TestVicinity vicinity) => null;

@override
bool shouldRebuild(covariant TwoDimensionalChildDelegate oldDelegate) => false;

}

class _TestBuilderDelegate extends TwoDimensionalChildBuilderDelegate { //ignore: unused_element
_TestBuilderDelegate({required super.builder});
// Would fail analysis without covariant
@override
Widget? build(BuildContext context, _TestVicinity vicinity) {
return super.build(context, vicinity);
}
}

class _TestListDelegate extends TwoDimensionalChildListDelegate { //ignore: unused_element
_TestListDelegate({required super.children});
// Would fail analysis without covariant
@override
Widget? build(BuildContext context, _TestVicinity vicinity) {
return super.build(context, vicinity);
}
}

RenderTwoDimensionalViewport getViewport(WidgetTester tester, Key childKey) {
return RenderAbstractViewport.of(
tester.renderObject(find.byKey(childKey))
Expand Down Expand Up @@ -2779,6 +2811,11 @@ class _SomeRenderTwoDimensionalViewport extends RenderTwoDimensionalViewport { /
super.delegate = value;
}

@override
RenderBox? getChildFor(_TestVicinity vicinity) { // Analysis would fail without covariant
return super.getChildFor(vicinity);
}

@override
void layoutChildSequence() {}
}

0 comments on commit a94c14e

Please sign in to comment.