Sub topic 12
Letting Users Change the Language at Runtime
This chapter assumes that you know basic Dart, null safety, Flutter widgets, MaterialApp, and the ARB and gen-l10n setup from earlier chapters. It also assumes that your generated AppLocalizations class already contains translations for at least English and Spanish.
A Flutter app chooses its language from the locale property of MaterialApp. If locale is null, Flutter uses the platform locale and resolves it against supportedLocales. To let a user choose a language, store the selected Locale in state and pass it to MaterialApp.
Store the selected locale above the app
The widget that owns the locale must rebuild MaterialApp when the user makes a choice. A small StatefulWidget is enough for an in-memory choice:
import 'package:flutter/material.dart';
import 'l10n/app_localizations.dart';
void main() {
runApp(const LocalizedApp());
}
class LocalizedApp extends StatefulWidget {
const LocalizedApp({super.key});
@override
State<LocalizedApp> createState() => _LocalizedAppState();
}
class _LocalizedAppState extends State<LocalizedApp> {
Locale? _selectedLocale;
static const supportedLocales = [
Locale('en'),
Locale('es'),
];
void _changeLocale(Locale? locale) {
setState(() {
_selectedLocale = locale;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
locale: _selectedLocale,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: supportedLocales,
onGenerateTitle: (context) =>
AppLocalizations.of(context)!.appTitle,
home: TripHome(
selectedLocale: _selectedLocale,
onLocaleChanged: _changeLocale,
),
);
}
}
class TripHome extends StatelessWidget {
const TripHome({
required this.selectedLocale,
required this.onLocaleChanged,
super.key,
});
final Locale? selectedLocale;
final ValueChanged<Locale?> onLocaleChanged;
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
return Scaffold(
appBar: AppBar(
title: Text(l10n.appTitle),
actions: [
DropdownButton<Locale?>(
value: selectedLocale,
hint: const Text('System'),
onChanged: onLocaleChanged,
items: const [
DropdownMenuItem<Locale?>(
value: null,
child: Text('System'),
),
DropdownMenuItem<Locale?>(
value: Locale('en'),
child: Text('English'),
),
DropdownMenuItem<Locale?>(
value: Locale('es'),
child: Text('Español'),
),
],
),
],
),
body: Center(
child: Text(l10n.greeting('Ada')),
),
);
}
}_selectedLocale starts as null, so the app follows the operating system. Selecting English or Spanish gives MaterialApp an explicit locale. Flutter then rebuilds the localization scope, and AppLocalizations.of(context) returns messages for the new locale.
The language names in this example are fixed labels. In a production app, you may translate them or display each language's native name. Avoid using a localized message as the DropdownButton value. The value should remain a stable Locale.
Preserve the choice
The state above lasts only while the process is running. If users should keep their choice after restarting the app, save a language tag such as en or es with a persistence package. Read the saved value before building the main screen, then convert it back to a Locale.
Keep the saved value limited to locales that your app supports. If a translation is removed in a later release, discard the old value and let the system choose a supported locale.
> callout note > Keep locale state near the root of the widget tree. Passing a locale through many screens makes switching harder to maintain.
Exercise
Exercise
Why must the selected Locale be stored in state above MaterialApp?
Show answer
Changing the state rebuilds MaterialApp with the new locale, which updates the localization scope and causes descendant widgets to read the new translations.