如何在显示中更改显示日期选择器的语言

问题描述 投票:1回答:1

我想在演出日期选择器中将语言从英语更改为法语,请在下面使用我正在使用的代码,以及应该解决的类似代码,但这会使该代码不适用于此部分:

              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个回答
0
投票
为了以本地语言显示日期选择器,您需要使用flutter_localizations插件,并在主代码的localizationDelegates中指定supportedLocalesMaterialApp。以下是在French中显示日期选择器的示例工作代码:

    flutter_localizations中添加pubspec.yaml插件并运行pub get
  • enter image description here

      将插件导入到dart文件中。
  • 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, ); } ); }, ) )

  • 再次运行应用程序(热重启),并在French中看到日期选择器。
  • enter image description here

    希望这能回答您的问题。

    © www.soinside.com 2019 - 2024. All rights reserved.