There are several ways this can be done.
The main thing is to explicitly set the locale
prop of the MaterialApp
widget on language change.
Below, you can find an example of how to do that using the provider package.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => LocaleModel(),
child: Consumer<LocaleModel>(
builder: (context, localeModel, child) => MaterialApp(
title: 'Your app title',
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
locale: localeModel.locale,
/* ... */
),
),
);
}
}
class LocaleModel extends ChangeNotifier {
Locale? _locale;
Locale? get locale => _locale;
void set(Locale locale) {
_locale = locale;
notifyListeners();
}
}
Note: For more details, please check out an example on GitHub.