Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter global material localizations and initialize date formatting not working together

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?

like image 505
Andrew P Avatar asked Sep 10 '18 01:09

Andrew P


People also ask

How do you format a date in flutter?

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.

What is Flutter_ localizations?

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 .

How do you use Intl flutter?

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 .


2 Answers

I solved the problem by adding in pubspec.yaml the following dependency:

dependencies:
  ...
  flutter_localizations:
    sdk: flutter
  ...

be careful with the indentation.

like image 99
PAKO Avatar answered Oct 23 '22 05:10

PAKO


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.

like image 1
SoftWyer Avatar answered Oct 23 '22 05:10

SoftWyer