I am trying to figure out how to create a subclass of a class without specifying all the optional parameters of the parent but still have access to them from the Constructor of the child subclass. This is especially important when subclassing Flutter Widgets with the myriad of attributes.
E.g. DoorWidget is parent class with many optional parameters.
ChildDoorWidget inherits DoorWidget to add extra logic, but still wants all the optional parent parameters without having to specify every parameter in the super() method of the subclass.
Is there some way of doing this?
Here is an example.
// Maine widget
class DoorWidget {
String color = 'red';
int width;
int height;
int p1;
int p2;
int p3;
Function onKicked = () => print('Kicked');
DoorWidget(this.color, {this.onKicked, this.width, this.height, this.p1, this.p2, this.p3});
}
class ChildDoorWidget extends DoorWidget {
@override
String color = 'green';
ChildDoorWidget(String color, {Function onKicked, int width, int height})
// Do I need to specify every parent optional parameter in the super class?
// Is there a way to avoid this.
: color = color,
super(color, onKicked: onKicked, width: width);
}
main() {
DoorWidget d = DoorWidget('green', width: 10, onKicked: () => print('DoorWidget Kicked') );
print('Text Class');
print(d.color);
print(d.width);
d.onKicked();
ChildDoorWidget c = ChildDoorWidget('blue',
width: 12, onKicked: () => print('ChildDoorWidget tapped called'));
// Ideally I would like to do this:
// ChildDoorWidget m = ChildDoorWidget('blue', width: 12, onKicked: () => print('tapped called'), p1: 1, p2: 2, and any other optional params);
print('\nMyText Class');
print(c.color);
print(c.width);
c.onKicked();
}