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

nszi6y05  于 2023-03-13  发布在  Flutter
关注(0)|答案(3)|浏览(169)

我需要显示一个阿拉伯语日期选择器,我不能用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

j5fpnvbx

j5fpnvbx1#

谢谢,我找到了我要找的解决方案。为了解决这个问题,我做了一些步骤:
为了以本地语言显示日期选择器,您需要使用flutter_localizations插件,并在主代码中的MaterialApp内指定localizationDelegates和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),
        ),
kkbh8khc

kkbh8khc2#

你能更具体地说明你使用的是哪个软件包吗?例如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, ])
pn9klfpd

pn9klfpd3#

用这个它对我有效-

添加flutter_本地化到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,
    ),
  ),

我们已经做完了一切都会好的

相关问题