一个按钮更改语言颤抖

发布于 2025-02-09 06:22:30 字数 1764 浏览 3 评论 0原文

我正在用颤音建立一个网站,并且我正在使用 easy_localization 有两个按钮,并且有两个按钮一种用于阿拉伯语的语言可以更改为阿拉伯语言环境的上下文,而英语语言可以更改为英语上下文,我想要的只是一个按钮检查当前的环境上下文,例如,如果本地环境上下文是阿拉伯语英文文字。否则将显示英语环境上下文,该按钮将显示阿拉伯文本。

这是我的代码:

> main.dart

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await EasyLocalization.ensureInitialized();
  runApp(EasyLocalization(
      child: MyApp(),
      supportedLocales: [
        Locale('en'),
        Locale('ar'),
      ],
      assetLoader: CodegenLoader(),
      fallbackLocale: Locale('en'),
      path: 'assets/translations'));
}

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  //the root of the application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      supportedLocales: context.supportedLocales,
      localizationsDelegates: context.localizationDelegates,
      locale: context.locale,
      debugShowCheckedModeBanner: false,
      title: '',
      theme: ThemeData(
          primaryColor: kPrimaryColor,
          textTheme: GoogleFonts.poppinsTextTheme(Theme.of(context).textTheme),
          visualDensity: VisualDensity.adaptivePlatformDensity),
      home: HomeScreen(),
    );
  }
}

这是我用来更改语言环境的按钮:

            ElevatedButton(
                onPressed: () async => await (context.setLocale(Locale('ar'))),
                child: Text('عربي')),
            ElevatedButton(
                onPressed: () async => await (context.setLocale(Locale('en'))),
                child: Text('English')),

I am building a website with flutter, and I am using easy_localization package, and there is two button one for Arabic Language to change to arabic locale context, And one for English Language to change to English context, what I want is only one button to check for the current locale context, for example if the locale context is Arabic then the button will display English text. else will display English locale context and the button will display Arabic Text.

here is my code:

> main.dart

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await EasyLocalization.ensureInitialized();
  runApp(EasyLocalization(
      child: MyApp(),
      supportedLocales: [
        Locale('en'),
        Locale('ar'),
      ],
      assetLoader: CodegenLoader(),
      fallbackLocale: Locale('en'),
      path: 'assets/translations'));
}

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  //the root of the application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      supportedLocales: context.supportedLocales,
      localizationsDelegates: context.localizationDelegates,
      locale: context.locale,
      debugShowCheckedModeBanner: false,
      title: '',
      theme: ThemeData(
          primaryColor: kPrimaryColor,
          textTheme: GoogleFonts.poppinsTextTheme(Theme.of(context).textTheme),
          visualDensity: VisualDensity.adaptivePlatformDensity),
      home: HomeScreen(),
    );
  }
}

and here is the buttons I'm using to change the locale context:

            ElevatedButton(
                onPressed: () async => await (context.setLocale(Locale('ar'))),
                child: Text('عربي')),
            ElevatedButton(
                onPressed: () async => await (context.setLocale(Locale('en'))),
                child: Text('English')),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有深☉意 2025-02-16 06:22:30

尝试一下:

context.locale.languageCode == 'en'
? ElevatedButton(
                onPressed: () async => await (context.setLocale(Locale('ar'))),
                child: Text('عربي')),
: ElevatedButton(
                onPressed: () async => await (context.setLocale(Locale('en'))),
                child: Text('English')),

Try this out:

context.locale.languageCode == 'en'
? ElevatedButton(
                onPressed: () async => await (context.setLocale(Locale('ar'))),
                child: Text('عربي')),
: ElevatedButton(
                onPressed: () async => await (context.setLocale(Locale('en'))),
                child: Text('English')),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文