如何使用 Flutter_localizations 和 Bloc 手动更改应用语言?

How to manually change app language with Flutter_localizations and Bloc?

void main() async {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => LanguageCubit(),
      child: BlocBuilder<LanguageCubit, Locale>(builder: (context, lang) {
        return MaterialApp(
          locale: lang,
          title: 'Localizations Sample App',
          localizationsDelegates: [
            AppLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: [
            const Locale('en', ''), // English, no country code
            const Locale('es', ''), // Spanish, no country code
            const Locale('tr', ''),
            const Locale('it', ''),
          ],
          home: Home(),
        );
      }),
    );
  }
}

class Home extends StatelessWidget {
  const Home({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          color: Colors.amberAccent,
          width: 200,
          height: 200,
          child: Column(
            children: [
              Text(AppLocalizations.of(context).helloWorld),
              Divider(),
              TextButton(
                  onPressed: () {
                    context.read<LanguageCubit>().changeLangEs(context);
                  },
                  child: Text('Change')),
            ],
          ),
        ),
      ),
    );
  }
}

class LanguageCubit extends Cubit<Locale> {
  LanguageCubit() : super(null);

  static final _languageEn = Locale('en', '');
  static final _languageEs = Locale('es', '');
  static final _languageTr = Locale('tr', '');

  void changeLangEn(context) async {
    emit(_languageEn);
  }

  void changeLangEs(context) async {
    emit(_languageEs);
  }

  void changeLangTr(context) async {
    emit(_languageTr);
  }
}

我有这样一个应用程序,问题是当我关闭应用程序并打开它时,应用程序使用默认语言again.How我可以将应用程序语言永久更改为所选语言吗?它会是很好,如果通过此 code.Do 给出了一个示例,我需要存储所选语言,然后从该数据库中使用它吗? Flutter_localizations 是否提供了一种简单的方法来做到这一点?

void main() async {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => LanguageCubit(),
      child: BlocBuilder<LanguageCubit, Locale>(builder: (context, lang) {
        return MaterialApp(
          locale: lang,
          title: 'Localizations Sample App',
          localizationsDelegates: [
            AppLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: [
            const Locale('en', ''), // English, no country code
            const Locale('es', ''), // Spanish, no country code
            const Locale('tr', ''),
            const Locale('it', ''),
          ],
          home: Home(),
        );
      }),
    );
  }
}

class Home extends StatelessWidget {
  //Here
  const Home({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    context.read<LanguageCubit>().changeStartLang();
    return Scaffold(
      body: Center(
        child: Container(
          color: Colors.amberAccent,
          width: 200,
          height: 200,
          child: Column(
            children: [
              Text(AppLocalizations.of(context).helloWorld),
              Divider(),
              TextButton(
                onPressed: () {
                  context.read<LanguageCubit>().changeLang(context, 'en');
                },
                child: Text('English'),
              ),
              TextButton(
                onPressed: () {
                  context.read<LanguageCubit>().changeLang(context, 'es');
                },
                child: Text('Espaniol'),
              ),
              TextButton(
                onPressed: () {
                  context.read<LanguageCubit>().changeLang(context, 'tr');
                },
                child: Text('Turkish'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class LanguageCubit extends Cubit<Locale> {
  LanguageCubit() : super(null);

  void changeStartLang() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String langCode = prefs.getString('lang');
    print(langCode);
    if (langCode != null) {
      emit(Locale(langCode, ''));
    }
  }

  void changeLang(context, String data) async {
    emit(Locale(data, ''));
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString('lang', data);
  }
}

我用过这样的方法,效果很好,简单实用。 context.read<LanguageCubit>().changeStartLang();build