【问题标题】:How to change language of Show date picker in flutter如何在颤动中更改显示日期选择器的语言
【发布时间】:2020-08-15 04:00:57
【问题描述】:

我想在显示日期选择器中将语言从英语更改为法语,请在下面找到我正在使用的代码以及应该解决该问题的代码,但它使代码不适用于这部分:

              new Step(
              title: new Text("Quelle est la date de 1er immatriculation?"),
              content: Column(
                children: <Widget>[
                 Text(_datetime == null ? "Vous n'avez pas encore choisi de date" : _datetime.toString().substring(0, 10)),

                  RaisedButton(
                      child: Text('choisissez une date'),
                    onPressed: () {
                    showDatePicker(context: context,
                      locale : const Locale("fr","FR"),//this line making the code not working too
                      builder: (BuildContext context, Widget child) {
                        return Theme(
                          data: ThemeData.fallback(),
                          child: child,
                        );
                      },
                     // locale: const Locale('eu', 'FR'),
                      initialDate: DateTime.now(),
                        firstDate: DateTime(1920),
                          lastDate: DateTime(2100),
                      ).then((date) {
                      setState(() {
                        _datetime =  date;
                      });
                      });
                    }
                 ),
                ],
              ),
              isActive: _currentStep >= 0,
              state:
              _currentStep >= 2 ? StepState.complete : StepState.disabled,
            ),

【问题讨论】:

    标签: flutter dart flutter-layout flutter-dependencies flutter-animation


    【解决方案1】:

    为了以本地语言显示日期选择器,您需要使用flutter_localizations 插件并在主代码中的MaterialApp 中指定localizationDelegatessupportedLocales。下面是在French 中显示日期选择器的示例工作代码:

    1. pubspec.yaml 中添加flutter_localizations 插件并运行pub get

    1. 在 dart 文件中导入插件。

    2. MaterialApp内,添加以下内容:

       return MaterialApp(
             localizationsDelegates: [
               GlobalMaterialLocalizations.delegate
             ],
             supportedLocales: [
               const Locale('en'),
               const Locale('fr')
             ],
      

      ....

       body: Center(
                 child: RaisedButton(
                   child: Text('Tap'),
                   onPressed: () {
                     showDatePicker(
                       context: context,
                         locale : const Locale("fr","FR"),
                       initialDate: DateTime.now(),
                       firstDate: DateTime(2018),
                       lastDate: DateTime(2030),
                       builder: (BuildContext context, Widget child) {
                         return Theme(
                           data: ThemeData.dark(),
                           child: child,
                         );
                       }
                     );
                   },
                 )
               )
      
    3. 再次运行应用程序(热重启)并看到 datepicker 出现在 French 中。

    【讨论】:

    • 导入为 import 'package:flutter_localizations/flutter_localizations.dart';
    【解决方案2】:

    我关注了@Darshan's answer,但出现以下错误:

    Unsupported operation: Cannot set value in unmodifiable Map
    

    在我从main.dart 中删除await initializeDateFormatting('fr_FR'); 之后,它起作用了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      • 2017-12-22
      • 1970-01-01
      相关资源
      最近更新 更多