The other day at work we got into this discussion when Copilot was giving different answers for similar questions.
The discussion was: if I have multiple children that use AppLocalizations
, should I read it from context
in a common parent’s build
method and send that instance to each one of the children or get it from context
inside each widget?
In other words, between the two options:
class ParentWidget extends StatelessWidget {
const ParentWidget({super.key});
@override
Widget build(BuildContext context) {
final localizations = AppLocalizations.of(context)!;
return Column(children: [
Text(localizations.someLabel),
ChildWidget(localizations: localizations),
]);
}
}
class ChildWidget extends StatelessWidget {
final AppLocalizations localizations;
const ChildWidget({required this.localizations, super.key});
@override
Widget build(BuildContext context) {
return Text(localizations.someOtherLabel);
}
}
and
class ParentWidget extends StatelessWidget {
const ParentWidget({super.key});
@override
Widget build(BuildContext context) {
final localizations = AppLocalizations.of(context)!;
return Column(children: [
Text(localizations.someLabel),
const ChildWidget(),
]);
}
}
class ChildWidget extends StatelessWidget {
const ChildWidget({super.key});
@override
Widget build(BuildContext context) {
final localizations = AppLocalizations.of(context)!;
return Text(localizations.someOtherLabel);
}
}
which one is more acceptable?
(Probably a matter of opinion, but it doesn’t hurt to ask)