我需要显示阿拉伯日期选择器

发布于 2025-01-15 01:03:49 字数 1769 浏览 4 评论 0原文

我需要显示一个阿拉伯日期选择器,我无法使用 Locale 构造函数控制它。 它仅显示英文视图。

选择日期方法

  DateTime selectedDate = DateTime.now();

  Future<void> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: DateTime(2000, 1),
        lastDate: DateTime(2100),
        builder: (BuildContext context, Widget child) {
          return Theme(
            data: Theme.of(context).copyWith(
                colorScheme: ColorScheme.light(primary: primaryColor)),
            child: child,
          );
        });

    if (picked != null && picked != selectedDate) {
      selectedDate = picked;
    }
  }

主屏幕

void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await GetStorage.init();
      runApp(
        MyApp(),
      );
    }

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      translations: Translation(),
      locale: Locale(AuthController().appLocal.value),
      fallbackLocale: Locale(AuthController().appLocal.value),
      title: 'Hesabate App',
      theme: ThemeData(
        canvasColor: Colors.transparent,
        fontFamily: "NotoNaskhArabic_font",
      ),
      initialRoute: AppRoutes.splashScreen,
      getPages: AppRoutes.routes,
    );
  }
}

我在此变量中有更新的语言(en&ar)

AuthController().appLocal.value

只有英文视图!

I need to display an Arabic date picker , I can't control this with Locale constructor .
It's only display an English view .

Pick date metod

  DateTime selectedDate = DateTime.now();

  Future<void> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: DateTime(2000, 1),
        lastDate: DateTime(2100),
        builder: (BuildContext context, Widget child) {
          return Theme(
            data: Theme.of(context).copyWith(
                colorScheme: ColorScheme.light(primary: primaryColor)),
            child: child,
          );
        });

    if (picked != null && picked != selectedDate) {
      selectedDate = picked;
    }
  }

Main Screen

void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await GetStorage.init();
      runApp(
        MyApp(),
      );
    }

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      translations: Translation(),
      locale: Locale(AuthController().appLocal.value),
      fallbackLocale: Locale(AuthController().appLocal.value),
      title: 'Hesabate App',
      theme: ThemeData(
        canvasColor: Colors.transparent,
        fontFamily: "NotoNaskhArabic_font",
      ),
      initialRoute: AppRoutes.splashScreen,
      getPages: AppRoutes.routes,
    );
  }
}

I have an updated language(en&ar) at this variable

AuthController().appLocal.value

An Only English View !

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

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

发布评论

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

评论(3

青丝拂面 2025-01-22 01:03:49

谢谢,我找到了我正在寻找的解决方案。
为了解决这个问题,我做了一些步骤:

为了以本地语言显示日期选择器,您需要
使用flutter_localizations插件并指定localizationDelegates
以及主代码中 MaterialApp 内的supportedLocales

1.在 pubspec.yaml 中添加 flutter_localizations 插件并运行 pub get。

flutter_localizations:
        sdk: flutter

2.在 dart 文件中导入插件

import 'package:flutter_localizations/flutter_localizations.dart';

3.在 MaterialApp 中添加以下内容

  return GetMaterialApp(
       localizationsDelegates: [
         GlobalMaterialLocalizations.delegate
       ],
       supportedLocales: [
         const Locale('en'),
         const Locale('ar')
       ],

4.然后实现默认的 datePicker,如下所示

 DateTime selectedDate = DateTime.now();

  Future<void> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: DateTime(2000, 1),
        lastDate: DateTime(2100),
        builder: (BuildContext context, Widget child) {
          return Theme(
            data: Theme.of(context).copyWith(
                colorScheme: ColorScheme.light(primary: primaryColor)),
            child: child,
          );
        });

    if (picked != null && picked != selectedDate) {
      selectedDate = picked;
    }
  }

5.最后将前面的方法调用到日期 TextField 中,如下所示

Expanded(
       child: CustomTextField(
         hint: "date".tr,
         onPress: () => _selectDate(context),
        ),

thank you , I found the solution I was looking for .
to solve the Issue , I did some steps :

In order to show the date picker in local language, you need to make
use of flutter_localizations plugin and specify localizationDelegates
and supportedLocales inside MaterialApp in your main code

1.Add flutter_localizations plugin in pubspec.yaml and run pub get.

flutter_localizations:
        sdk: flutter

2.Import the plugin in dart file

import 'package:flutter_localizations/flutter_localizations.dart';

3.Inside MaterialApp, add following

  return GetMaterialApp(
       localizationsDelegates: [
         GlobalMaterialLocalizations.delegate
       ],
       supportedLocales: [
         const Locale('en'),
         const Locale('ar')
       ],

4.then implement default datePicker as following

 DateTime selectedDate = DateTime.now();

  Future<void> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: DateTime(2000, 1),
        lastDate: DateTime(2100),
        builder: (BuildContext context, Widget child) {
          return Theme(
            data: Theme.of(context).copyWith(
                colorScheme: ColorScheme.light(primary: primaryColor)),
            child: child,
          );
        });

    if (picked != null && picked != selectedDate) {
      selectedDate = picked;
    }
  }

5.finally call the previous method into your date TextField as following

Expanded(
       child: CustomTextField(
         hint: "date".tr,
         onPress: () => _selectDate(context),
        ),
枕花眠 2025-01-22 01:03:49

您能更具体地说明您正在使用哪个软件包吗?例如 flutter_datetime_picker 有参数 locale,您可以在其中传递 LocalType.ar

  static Future<DateTime?> showPicker(
BuildContext context, {
bool showTitleActions: true,
DateChangedCallback? onChanged,
DateChangedCallback? onConfirm,
DateCancelledCallback? onCancel,
locale: LocaleType.en,
BasePickerModel? pickerModel,
DatePickerTheme? theme,

根据 Nader 澄清编辑,您可以将 locale:Locale("ar") 传递给内部 date_picker

  const Locale(
this._languageCode, [
this._countryCode, ])

Can you be more specific on which package you are using?. For example flutter_datetime_picker have parameter locale, where you can pass LocalType.ar

  static Future<DateTime?> showPicker(
BuildContext context, {
bool showTitleActions: true,
DateChangedCallback? onChanged,
DateChangedCallback? onConfirm,
DateCancelledCallback? onCancel,
locale: LocaleType.en,
BasePickerModel? pickerModel,
DatePickerTheme? theme,

Edited per Nader clarification, you can pass locale:Locale("ar") to the internal date_picker

  const Locale(
this._languageCode, [
this._countryCode, ])
若沐 2025-01-22 01:03:49

使用它对我有用 -

将 flutter_localizations 添加到 pub.yaml

将这两行添加到主要材质应用程序小部件

localizationsDelegates[GlobalMaterialLocalizations.delegate],
supportedLocales: [const Locale('en'), const Locale('ar')],

我的主要材质应用程序小部件中,我的主要外观 -

class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GetMaterialApp(
    localizationsDelegates: [GlobalMaterialLocalizations.delegate],
    supportedLocales: [const Locale('en'), const Locale('ar')],
    debugShowCheckedModeBanner: false,
    initialRoute: get_initial_route(),
    getPages: app_routes());
}
}

将其放入此材料的任何子小部件的代码中 -

IconButton(
    onPressed: () async {
      final DateTime? pickedDate = await showDatePicker(
          context: context,
          initialDate: DateTime(1999),
          firstDate: DateTime(1960),
          lastDate: DateTime(2025),
          locale: Locale('ar'));
    },
    icon: Icon(
      Icons.date_range,
      color: Colors.blue,
    ),
  ),

我们已经完成了一切都应该没问题

use this it worked for me -

add flutter_localizations to pub.yaml

add this two line to main material app widget

localizationsDelegates[GlobalMaterialLocalizations.delegate],
supportedLocales: [const Locale('en'), const Locale('ar')],

for me my main looklike -

class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GetMaterialApp(
    localizationsDelegates: [GlobalMaterialLocalizations.delegate],
    supportedLocales: [const Locale('en'), const Locale('ar')],
    debugShowCheckedModeBanner: false,
    initialRoute: get_initial_route(),
    getPages: app_routes());
}
}

place it in your code in any child widget from this material -

IconButton(
    onPressed: () async {
      final DateTime? pickedDate = await showDatePicker(
          context: context,
          initialDate: DateTime(1999),
          firstDate: DateTime(1960),
          lastDate: DateTime(2025),
          locale: Locale('ar'));
    },
    icon: Icon(
      Icons.date_range,
      color: Colors.blue,
    ),
  ),

and we are done every thing should be ok

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文