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

时间:2020-04-30 17:29:46

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

我想在演出日期选择器中将语言从英语更改为法语,请在下面使用我正在使用的代码,以及一个应该解决的代码,但该代码对此部分无效:

              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,
            ),

2 个答案:

答案 0 :(得分:20)

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

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

enter image description here

  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. 再次运行应用程序(热重启),然后在French中看到日期选择器。

enter image description here

希望这能回答您的问题。

答案 1 :(得分:2)

我遵循@Darshan的回答,但出现以下错误:

Unsupported operation: Cannot set value in unmodifiable Map

await initializeDateFormatting('fr_FR');中删除main.dart后,对我来说效果很好,请尽情享受