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

标签 flutter dart datepicker flutter-getx

我需要显示一个阿拉伯日期选择器,我无法使用 Locale 构造函数控制它。 它仅显示英文 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 !

最佳答案

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

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.在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.最后将前面的方法调用到日期文本字段中,如下所示

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

关于flutter - 我需要显示阿拉伯日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71519674/

相关文章:

Dart 可观察列表,不会在更改时删除并重新插入所有元素

javascript - JQuery 日期选择器设置默认值

jquery - 如何添加动态 Bootstrap 日期选择器字段?

jquery - 如何从日期选择器设置区域设置格式?

flutter - 如何控制 FLutter 包 audio_service 中通知栏中显示的内容

flutter - 缩放嵌套 RichText 小部件以实现可访问性

dart - 绘制从远程服务器下载的图像

flutter - 查找比较两个持续时间的剩余百分比

flutter - 波纹动画 flutter

Flutter:如何为 DropdownItems 和 DropdownButton 所选项目设置不同的颜色?