我这个应用程序带有 flutter 3.0 和 easy_localization 3.0.2 包。 我想在应用程序启动时设置默认语言。 我使用 startLocale: Locale('ru', 'RU') 但它似乎不起作用。 它使用先前执行应用程序时在context中设置的语言。 我该如何解决这个问题?
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'generated/locale_keys.g.dart';
import 'lang_view.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
runApp(EasyLocalization(
supportedLocales: [
Locale('en', 'US'),
Locale('ar', 'DZ'),
Locale('de', 'DE'),
Locale('ru', 'RU'),
Locale('it', 'IT')
],
startLocale: Locale('ru', 'RU'),
path: 'resources/langs',
child: MyApp(),
));
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: {
// When navigating to the "/" route, build the FirstScreen widget.
'/': (context) => MyHomePage(title: 'Easy localization'),
// When navigating to the "/second" route, build the SecondScreen widget.
'/second': (context) => const SecondPage(),
},
);
}
}```
使用 startLocale 需要设置 saveLocale: false ,否则默认 saveLocale 为 true 时不起作用,所以需要设置 saveLocale:false 。