Add covariants to reduce subclass casts in 2D APIs (#141318)
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 https://github.com/flutter/flutter/pull/131358, this would cover all of the cases I could find so its nice and accommodating now.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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))
|
||||
@@ -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() {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user