|
1 | | -# localization-date-range-picker-flutter |
2 | | -How to add new language in Flutter date range picker? |
| 1 | +# How to add new language in Flutter date range picker (SfDateRangePicker)? |
| 2 | + |
| 3 | +In the flutter date range picker, you can change the locale of the date range picker using the `locale` property of the MaterialApp widget. |
| 4 | + |
| 5 | +## Step 1: |
| 6 | +For localization, you need to add the supported locales in an array. And using any one of the supported locales, you can set the locale for the date picker. |
| 7 | + |
| 8 | +```xml |
| 9 | +localizationsDelegates: [ |
| 10 | + GlobalMaterialLocalizations.delegate, |
| 11 | + GlobalWidgetsLocalizations.delegate, |
| 12 | +], |
| 13 | +supportedLocales: [ |
| 14 | + const Locale('en'), |
| 15 | + const Locale('zh'), |
| 16 | + const Locale('he'), |
| 17 | + const Locale('ru'), |
| 18 | + const Locale('fr', 'BE'), |
| 19 | + const Locale('fr', 'CA'), |
| 20 | + const Locale('ja'), |
| 21 | + const Locale('de'), |
| 22 | + const Locale('hi'), |
| 23 | + const Locale('ar'), |
| 24 | +], |
| 25 | +locale: const Locale('zh'), |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | +## Step 2: |
| 30 | +Please find the complete code snippet for localization. |
| 31 | + |
| 32 | +```xml |
| 33 | +import 'package:flutter/material.dart'; |
| 34 | +import 'package:flutter_localizations/flutter_localizations.dart'; |
| 35 | +import 'package:syncfusion_flutter_datepicker/datepicker.dart'; |
| 36 | + |
| 37 | +void main() => runApp(Localization()); |
| 38 | + |
| 39 | +class Localization extends StatelessWidget { |
| 40 | + // This widget is the root of your application. |
| 41 | + @override |
| 42 | + Widget build(BuildContext context) { |
| 43 | + return MaterialApp( |
| 44 | + localizationsDelegates: [ |
| 45 | + GlobalMaterialLocalizations.delegate, |
| 46 | + GlobalWidgetsLocalizations.delegate, |
| 47 | + ], |
| 48 | + supportedLocales: [ |
| 49 | + const Locale('en'), |
| 50 | + const Locale('zh'), |
| 51 | + const Locale('he'), |
| 52 | + const Locale('ru'), |
| 53 | + const Locale('fr', 'BE'), |
| 54 | + const Locale('fr', 'CA'), |
| 55 | + const Locale('ja'), |
| 56 | + const Locale('de'), |
| 57 | + const Locale('hi'), |
| 58 | + const Locale('ar'), |
| 59 | + ], |
| 60 | + locale: const Locale('zh'), |
| 61 | + debugShowCheckedModeBanner: false, |
| 62 | + home: LocalizationInPicker(), |
| 63 | + ); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +class LocalizationInPicker extends StatefulWidget { |
| 68 | + @override |
| 69 | + _LocalizationInPickerState createState() => _LocalizationInPickerState(); |
| 70 | +} |
| 71 | + |
| 72 | +List<String> views = <String>['Month', 'Year', 'Decade', 'Century']; |
| 73 | + |
| 74 | +class _LocalizationInPickerState extends State<LocalizationInPicker> { |
| 75 | + DateRangePickerController _controller; |
| 76 | + |
| 77 | + @override |
| 78 | + void initState() { |
| 79 | + // TODO: implement initState |
| 80 | + _controller = DateRangePickerController(); |
| 81 | + super.initState(); |
| 82 | + } |
| 83 | + |
| 84 | + @override |
| 85 | + Widget build(BuildContext context) { |
| 86 | + return Scaffold( |
| 87 | + appBar: AppBar( |
| 88 | + leading: PopupMenuButton<String>( |
| 89 | + icon: Icon(Icons.calendar_today), |
| 90 | + itemBuilder: (BuildContext context) => views.map((String choice) { |
| 91 | + return PopupMenuItem<String>( |
| 92 | + value: choice, |
| 93 | + child: Text(choice), |
| 94 | + ); |
| 95 | + }).toList(), |
| 96 | + onSelected: (String value) { |
| 97 | + if (value == 'Month') { |
| 98 | + _controller.view = DateRangePickerView.month; |
| 99 | + } else if (value == 'Year') { |
| 100 | + _controller.view = DateRangePickerView.year; |
| 101 | + } else if (value == 'Decade') { |
| 102 | + _controller.view = DateRangePickerView.decade; |
| 103 | + } else if (value == 'Century') { |
| 104 | + _controller.view = DateRangePickerView.century; |
| 105 | + } |
| 106 | + }, |
| 107 | + ), |
| 108 | + ), |
| 109 | + body: Card( |
| 110 | + margin: const EdgeInsets.fromLTRB(40, 150, 40, 150), |
| 111 | + child: SfDateRangePicker( |
| 112 | + controller: _controller, |
| 113 | + view: DateRangePickerView.month, |
| 114 | + ), |
| 115 | + ) |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
0 commit comments