build method
Widget
build(
- BuildContext context,
- WidgetRef ref,
- Locale state,
- LanguageSwitcherNotifier notifier,
- List<
LocaleInfo> supportedLocales,
override
Build method that concrete implementations must override.
This method receives all necessary data for building language switcher UI:
context
: The build context for accessing theme and navigationref
: WidgetRef for accessing Riverpod providersstate
: Current locale state from the language switcher providernotifier
: LanguageSwitcherNotifier for changing localessupportedLocales
: List of all supported locales from app configuration
Example implementation:
@override
Widget build(
BuildContext context,
WidgetRef ref,
Locale state,
LanguageSwitcherNotifier notifier,
List<LocaleInfo> supportedLocales,
) {
return Column(
children: supportedLocales.map((localeInfo) {
return RadioListTile<Locale>(
value: localeInfo.locale,
groupValue: state,
title: Text(localeInfo.displayName),
onChanged: (locale) {
if (locale != null) notifier.changeLocale(locale);
},
);
}).toList(),
);
}
Implementation
@override
Widget build(
BuildContext context,
WidgetRef ref,
Locale state,
LanguageSwitcherNotifier notifier,
List<LocaleInfo> supportedLocales,
) {
final localeInfo = supportedLocales.firstWhere(
(info) => info.locale.languageCode == state.languageCode,
);
return IconButton(
onPressed: () {
HapticFeedback.lightImpact();
LanguageSwitcher.show(context);
},
icon: Stack(
children: [
const Icon(Icons.language),
Positioned(
right: 0,
bottom: 0,
child: Container(
padding: const EdgeInsets.all(2),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary,
shape: BoxShape.circle,
),
child: Text(
localeInfo.locale.languageCode.toUpperCase(),
style: TextStyle(
color: Theme.of(context).colorScheme.onPrimary,
fontSize: 8,
fontWeight: FontWeight.bold,
),
),
),
),
],
),
tooltip: context.jetI10n.changeLanguage,
);
}