I am using localisation in a flutter app but also want to localise the date format using initialise date formatting. My main looks like this ...
void main() {
runApp(new MaterialApp(
supportedLocales:
[const Locale('en', 'US'),
const Locale('en', 'AU')],
localizationsDelegates: [
const DemoLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
home: new ThirdPageWidget(),
navigatorObservers: [routeObserver],
));
}
Also I have a initializeDateFormatting in a stateful widget like this ...
@override
void initState() {
super.initState();
initializeDateFormatting().then((_) {
dateFormat = new DateFormat.yMd('en_AU');
print(dateFormat.format(DateTime.now()));
});
Now when the locale is en_AU the format of the date is month/day/year american style but when I remove this line of code
GlobalMaterialLocalizations.delegate,
The date correctly displays day/month/year. Does any one know what I can do to fix this? How important is it to have the GlobalMaterialLocalizations.delegate?
To format DateTime in Flutter using a standard format, you need to use the intl library and then use the named constructors from the DateFormat class. Simply write the named constructor and then call the format() method with providing the DateTime.
Setting up an internationalized app: the Flutter_localizations package. By default, Flutter only provides US English localizations. To add support for other languages, an application must specify additional MaterialApp (or CupertinoApp ) properties, and include a package called flutter_localizations .
Select the starter folder in the project directory then, on the menu bar, select Tools ▸ Flutter Intl ▸ Initialize for the Project. The command above added a flutter_intl section to your pubspec. yaml. Add main_locale: en_US to it, below and in the same indentation level of enabled: true .
I solved the problem by adding in pubspec.yaml the following dependency:
dependencies:
...
flutter_localizations:
sdk: flutter
...
be careful with the indentation.
I too had this issue and, after playing around a little, found that when using Material localisations it appears default to US if you do not specify the supported locales.
Adding the following supported locales allowed the UK date format to be shown rather than the US one.
localizationsDelegates: [
const DemoLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'), // English US
const Locale('en', 'GB'), // English UK
// ... other locales the app supports
],
I didn't have to explicitly initialise the DateFormat class as Material appears to handle this too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With