0

我正在处理本地化。问题是我的提供程序类中的语言环境变量实际上是在整个应用程序中更新的,但应用程序的实际语言环境没有更新。我已尝试再次关注文档。仍然无法正常工作。我已经提供了所需的代码。

提供者类

class LocaleProvider extends ChangeNotifier {
 Locale _locale=const Locale('en');
  
  Locale get locale => _locale;

  void setLocale(Locale locale) {
    if (!L10n.all.contains(locale)) return;
    
    _locale = locale;
    print(_locale.toString());
    notifyListeners();
  }
}

更新语言

Column(
          children: [
            Container(
              padding: const EdgeInsets.all(20),
              child: Row(
                children:  [
                  const ButtonBack(), //back button
                  const SizedBox(
                    width: 30,
                  ),
                  Text(
                    AppLocalizations.of(context)?.language??'Language', //appbar title
                    style: titleStyle,
                  ),
                ],
              ),
            ),
            GestureDetector(
              onTap: () async {
                 Provider.of<LocaleProvider>(context,listen: false)
                    .setLocale(const Locale('my'));
              },
              child: Container(
                margin: const EdgeInsets.all(25),
                padding: const EdgeInsets.all(5),
                decoration: const BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      width: 1,
                      color: Colors.black,
                    ),
                  ),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    const Text(
                      'မြန်မာ', //burmese language
                      style: styleText,
                    ),
                    (Provider.of<LocaleProvider>(context).locale ==
                            const Locale('my'))
                        ? const Icon(Icons.check_outlined)
                        : const SizedBox(),
                  ],
                ),
              ),
            ),
            GestureDetector(
              onTap: () async {
                Provider.of<LocaleProvider>(context,listen: false)
                    .setLocale(const Locale('en'));
              },
              child: Container(
                margin: const EdgeInsets.all(25),
                padding: const EdgeInsets.all(5),
                decoration: const BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      width: 1,
                      color: Colors.black,
                    ),
                  ),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    const Text(
                      'English', //english language
                      style: styleText,
                    ),
                    (Provider.of<LocaleProvider>(context).locale ==
                            const Locale('en'))
                        ? const Icon(Icons.check_outlined)
                        : const SizedBox(), //correct icon for selected language
                  ],
                ),
              ),
            ),
          ],
        ),

主班

builder: (context, child) {
          return GetMaterialApp(
    
            debugShowCheckedModeBanner: false,
            theme: ThemeData.light().copyWith(
              scaffoldBackgroundColor: const Color(0xffFCFCFC),
            ),
            locale: Provider.of<LocaleProvider>(context,listen: true).locale,
            supportedLocales: L10n.all,
            localizationsDelegates: const [
              AppLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
            ],
            home: const HomeMapPage(),
          );
        }
4

0 回答 0