in Flutter we have class
abstract class PreferredSizeWidget implements Widget {
Size get preferredSize;
}
and
@immutable
abstract class Widget extends DiagnosticableTree {
const Widget({ this.key });
final Key? key;
@protected
@factory
Element createElement();
@override
String toStringShort() {
final String type = objectRuntimeType(this, 'Widget');
return key == null ? type : '$type-$key';
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.defaultDiagnosticsTreeStyle = DiagnosticsTreeStyle.dense;
}
@override
@nonVirtual
bool operator ==(Object other) => super == other;
@override
@nonVirtual
int get hashCode => super.hashCode;
static bool canUpdate(Widget oldWidget, Widget newWidget) {
return oldWidget.runtimeType == newWidget.runtimeType
&& oldWidget.key == newWidget.key;
}
static int _debugConcreteSubtype(Widget widget) {
return widget is StatefulWidget ? 1 :
widget is StatelessWidget ? 2 :
0;
}
}
My question is: Why if we mix in PreferredSizeWidget class we don't have to have implementation for all of the abstract class Widget methods and abstract methods?
I tried to mock up some code
class A implements B {}
abstract class B extends C {}
abstract class C {
void test() {
print("test");
}
}
and got error
Error: The non-abstract class 'A' is missing implementations for these members:
- C.test
class A implements B {
^
lib/main.dart:16:8:
Haven’t run it myself, but in your case A isn’t abstract anymore so it has to implement everything, in the case of PreferredSizeWidget - you usually extend/implement/ mixin it on a widget (which already implements everything from widget) thus leaving you with only preferredSize
@Norbert515's answer.
The main point is that even though if we mixin/extend PreferredSizeWidget we actually implement all of the Widget methods but as we usually mixin it on a Widget we don't have to make an implicit override of the methods.