Flutter-如何保持所选语言



我按照这个视频在我的应用程序中实现本地化:-https://youtu.be/Zw4KoorVxgg

一切正常,但当应用程序重新启动时,语言将设置为默认语言。我该如何保存数据。

[注意:-我已经在使用共享偏好来在我的应用程序中保存一些其他数据,但我不知道如何保存所选的Locale]

调用下面的这个类来设置区域设置:

class LocaleProvider extends ChangeNotifier {
Locale? _locale;
Locale? get locale => _locale;
void setLocale(Locale locale) {
if (!L10n.all.contains(locale)) return;
_locale = locale;
notifyListeners();
}
void clearLocale() {
_locale = null;
notifyListeners();
}
}

这是我设置语言的屏幕:

class SelectLanguage extends StatefulWidget {
const SelectLanguage({Key? key}) : super(key: key);
@override
_SelectLanguageState createState() => _SelectLanguageState();
}
class _SelectLanguageState extends State<SelectLanguage> {
void setLocale(String selectedLocale) {
final provider = Provider.of<LocaleProvider>(context, listen: false);
provider.setLocale(
Locale(selectedLocale),
);
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
backgroundColor: Colors.black,
body: Padding(
padding:
EdgeInsets.all(MediaQuery.of(context).size.width / 41.1),
child: GridView(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 2.75,
crossAxisSpacing: MediaQuery.of(context).size.width / 41.1,
mainAxisSpacing: MediaQuery.of(context).size.width / 41.1,
),
children: [
LanguageTile(
tileTitle: 'English',
titleOnTap: () => setLocale('en'),
),
LanguageTile(
tileTitle: 'हिन्दी',
titleOnTap: () => setLocale('hi'),
),
],
),
),
),
);
}
}

使用共享偏好

static Future<Locale> setLocale(String langCode) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString(languageCode, langCode);
return locale(langCode);
}
static Locale locale(String languageCode) {
Locale _temp;
if (languageCode == 'hi') {
_temp = Locale('hi');
}
else {
_temp = Locale('en');
}
return _temp;
}
static Future<Locale> getLocale() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String code = prefs.getString(languageCode);
print('language code:$code');
return locale(code);
}

创建一个类

import 'dart:ui';
import 'package:shared_preferences/shared_preferences.dart';
const String LAGUAGE_CODE = 'languageCode';
//languages code
const String ENGLISH = 'en';
const String FRENCH = 'fr';
const String ARABIC = 'ar';
const String KINYARWANDA = 'rw';
const String SWAHILLI ="sw";
Future<Locale> storeLocale(String languageCode) async {
SharedPreferences _prefs = await SharedPreferences.getInstance();
await _prefs.setString(LAGUAGE_CODE, languageCode);
return _locale(languageCode);
}
Future<Locale> getLocale() async {
SharedPreferences _prefs = await SharedPreferences.getInstance();
String languageCode = _prefs.getString(LAGUAGE_CODE) ?? ENGLISH;
return _locale(languageCode);
}
Locale _locale(String languageCode) {
switch (languageCode) {
case ENGLISH:
return const Locale(ENGLISH, '');
case FRENCH:
return const Locale(FRENCH, "");
case ARABIC:
return const Locale(ARABIC, "");
case KINYARWANDA:
return const Locale(KINYARWANDA, "");
case SWAHILLI:
return const Locale(SWAHILLI, "");
default:
return const Locale(ENGLISH, '');
}
}

然后在您的提供商类中添加

Future<void> setLocale(Locale locale) async{
if(!L10n.all.contains(locale)) return;
_locale =locale;
storeLocale(locale.languageCode);//add this important
// storelocal(locale.languageCode);
notifyListeners();
}

您正在实现它的位置或小部件,例如下拉

Widget build(BuildContext context) {
final provider = Provider.of<LocaleProvider>(context,);
final locale = provider.locale ?? const Locale('en');
return DropdownButtonHideUnderline(
child: DropdownButton(
value: locale,
icon: const Icon(Icons.language,color:Colors.white,),
items: L10n.all.map((locale) {
final flag = L10n.getFlag(locale.languageCode);
return DropdownMenuItem(
value: locale,
onTap: ()async {
//await storeLocale(locale.languageCode);//may uncomment this
final provider =
Provider.of<LocaleProvider>(context,listen: false );
provider.setLocale(locale);
},

并且在main.dart中更改为有状态并覆盖

@override
void didChangeDependencies() {
super.didChangeDependencies();
getLocale().then(
(locale) => Provider.of<LocaleProvider>(context,listen: false).setLocale(locale));
}

希望这能帮助

相关内容

  • 没有找到相关文章

最新更新