I have a set of around 8 widgets that all accept a single parameter of type X and display the content of type X in a different way. What I am trying to create is an abstraction layer that defines the structure of such a widget. Besides the structure, the abstraction layer would define a factory method to decide which implementation gets used based on an ID. The different implementations are all widgets that extend either Stateless- or StatefulWidget.
The abstraction layer would look like the following:
abstract class AbstractWidget {
final X content;
factory AbstractWidget({@required int id, @required X content}) {
switch (id) {
case 1:
return Implementation1(content);
break;
default: return Implementation2(content);
}
}
}
An implementation would look like the following:
class Implementation1 extends StatelessWidget implements AbstractWidget {
final X content;
Implementation1(this.content);
@override
Widget build(BuildContext context) {
// Display content in some type of way
}
}
So what I'm trying to achieve is the following:
var widgetList = new List<Widget>();
for (var item in items) {
X content = fetchContentFromAPI();
widgetList.add(AbstractWidget(content: content, id: item.id));
}
return Column(children: widgetList);
This would not work because the AbstractWidget isn't technically a Widget-type, even though it can only return instances of either Stateless- or StatefulWidgets. If anyone knows a better way of implementing my structure, it would help me a lot!
Aucun commentaire:
Enregistrer un commentaire