Is returning a widget in a switch case just as efficient as encapsulating that code in an other builder widget?
switchcase:
@override
Widget build(BuildContext context) {
switch (predicate()) {
case Trinary.first:
return firstChildBuilder(context);
case Trinary.second:
return secondChildBuilder(context);
case Trinary.third:
return thirdChildBuilder(context);
Vs abstracting the switch out to a new widget with it's own build method,
@override
Widget build(BuildContext context) {
return TrinaryBuilderWidget(
predicate: viewModel.statusPredicate(),
firstChildBuilder: (context) {
return FirstWidget();
},
secondChildBuilder: (context) {
return SecondWidget();
},
thirdChildBuilder: (context) {
return ThirdWidget();
},
);
}
==========new class==========
class TrinaryBuilderWidget extends StatelessWidget {
final Trinary Function() predicate;
final WidgetBuilder firstChildBuilder;
final WidgetBuilder secondChildBuilder;
final WidgetBuilder thirdChildBuilder;
/// Will build the widget associated with the [Trinary] specified by the [predicate]
const TrinaryBuilderWidget({
Key? key,
required this.predicate,
required this.firstChildBuilder,
required this.secondChildBuilder,
required this.thirdChildBuilder,
}) : super(key: key);
@override
Widget build(BuildContext context) {
switch (predicate()) {
case Trinary.first:
return firstChildBuilder(context);
case Trinary.second:
return secondChildBuilder(context);
case Trinary.third:
return thirdChildBuilder(context);
}
}
}