Home > Back-end >  How to change the language of the DatePicker in Flutter?
How to change the language of the DatePicker in Flutter?

Time:01-23

I'm working on a form with a DatePicker. Now I want to change the language from English to German but that didn't worked as axpected.

At the moment my App looks like this: englisch DatePicker

So i tried to change the locale property to German like this: locale: "de_DE".

void _showDatePicker(){
    showDatePicker(
      locale: "de_DE",
        context: context,
        initialDate: DateTime.now(),
        firstDate: DateTime(2000),
        lastDate: DateTime(2050),
      cancelText: 'Abbrechen',
    ).then((value) {
      setState(() {
        String _dateTime = DateFormat('dd.MM.yyy').format(value!);
        selectedDay = _dateTime;
      });
    } );
  }

But that didn't work, i get this error: lib/content/anmeldungen.dart:33:15: Error: The argument type 'String' can't be assigned to the parameter type 'Locale?'.

  • 'Locale' is from 'dart:ui'. locale: "de_DE", ^

So thanks for helping!

CodePudding user response:

The locale property of the showDatePicker function expects a Locale object, not a string. To set the locale to German, you can use the Locale constructor to create a new Locale object and pass it to the locale property. Here is an example:

void _showDatePicker(){
    showDatePicker(
      locale: Locale('de', 'DE'),
        context: context,
        initialDate: DateTime.now(),
        firstDate: DateTime(2000),
        lastDate: DateTime(2050),
      cancelText: 'Abbrechen',
    ).then((value) {
      setState(() {
        String _dateTime = DateFormat('dd.MM.yyy', 'de_DE').format(value!);
        selectedDay = _dateTime;
      });
    } );
  }

In this case, the first parameter of the Locale constructor is the language code and the second parameter is the country code. Also, you need to make sure that you pass the correct Locale code to the DateFormat function so it can format the date in the correct format.

CodePudding user response:

Thanks for Help but at the moment i'm getting this error:

Performing hot reload...
Syncing files to device sdk gphone64 x86 64...

======== Exception caught by widgets library =======================================================
The following assertion was thrown building DatePickerDialog(dirty, dependencies: [UnmanagedRestorationScope, _InheritedTheme, _LocalizationsScope-[GlobalKey#00104]], state: _DatePickerDialogState#516be):
No MaterialLocalizations found.

DatePickerDialog widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.
The material library uses Localizations to generate messages, labels, and abbreviations.

To introduce a MaterialLocalizations, either use a MaterialApp at the root of your application to include them automatically, or add a Localization widget with a MaterialLocalizations delegate.

The specific widget that could not find a MaterialLocalizations ancestor was: DatePickerDialog
  dirty
  dependencies: [UnmanagedRestorationScope, _InheritedTheme, _LocalizationsScope-[GlobalKey#00104]]
  state: _DatePickerDialogState#516be
The ancestors of this widget were: 
  : MaterialApp
    state: _MaterialAppState#b2803
  : MyApp
  ...
The relevant error-causing widget was: 
  MaterialApp MaterialApp:file:///C:/Users/domi0/Desktop/Schule/Diplomarbeit/terminverwaltung/lib/main.dart:15:13
When the exception was thrown, this was the stack: 
#0      debugCheckHasMaterialLocalizations.<anonymous closure> (package:flutter/src/material/debug.dart:76:7)
#1      debugCheckHasMaterialLocalizations (package:flutter/src/material/debug.dart:96:4)
#2      MaterialLocalizations.of (package:flutter/src/material/material_localizations.dart:657:12)
#3      _DatePickerDialogState.build (package:flutter/src/material/date_picker.dart:458:71)
#4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4992:27)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4878:15)
#6      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#7      Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#8      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2667:19)
#9      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#10     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:378:5)
#11     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1175:15)
#12     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1104:9)
#13     SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:881:7)
(elided 4 frames from class _RawReceivePortImpl, class _Timer, and dart:async-patch)
====================================================================================================
Reloaded 1 of 653 libraries in 2.306ms (compile: 71 ms, reload: 559 ms, reassemble: 1267 ms).
D/EGL_emulation( 7518): app_time_stats: avg=125539.43ms min=75.92ms max=251002.92ms count=2

  • Related