Back to Blogs

How to Localize DatePicker in Flutter?

Tech
How to Localize DatePicker in Flutter?
They are used to get datetime information from user in Flutter. How to set date picker widget according to user's local language?
1. Add flutter_localizations plugin in pubspec.yaml and run pub get.
           dependencies:
           flutter:
                 sdk: flutter
              flutter_localizations: 
                 sdk: flutter     

2. Import the plugin in dart file.
import 'package:flutter_localizations/flutter_localizations.dart'; 

3. Inside MaterialApp, add the following:
  return const MaterialApp(
  localizationsDelegates: [
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
  ],
  supportedLocales: [
    Locale('en'),
    Locale('tr'),  
  ],
  home: MyHomePage(),
);

4. then add this to your datetime picker
locale: const Locale("tr"),

Example Code

showDatePicker(
            context: context,
            locale: const Locale("tr"),
            initialDate: DateTime.now(),
            firstDate: DateTime(2018),
            lastDate: DateTime(2030),
            builder: (BuildContext context, Widget? child) {
              return Theme(
                data: ThemeData.dark(),
                child: child!,
              );
            });