我需要显示一个阿拉伯日期选择器,我无法使用 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
最佳答案
谢谢,我找到了我正在寻找的解决方案。 为了解决这个问题,我做了一些步骤:
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/