Flutter: Listen to Locale changes using EasyLocalizations

185 Views Asked by At

I'm currently using the EasyLocalization package in my Flutter app. This works great with the predefined translation files, but I want to request some data from a server again, if the locale has changed.

Is there a way to listen to locale changes in the app? I have tried to do so using WidgetsBindingObserver and PlatformDispatcher.instance.onLocaleChanged, but this does not seem to work with in app locale changes.

My code currently looks like this:

AppBar(
    title: Padding(
      padding: const EdgeInsets.symmetric(horizontal: 8.0),
      child:
      SpeedDial(
        backgroundColor: Colors.transparent,
        direction: SpeedDialDirection.down,
        switchLabelPosition: true,
        children: [
          SpeedDialChild(
            label: 'deutsch',
            labelStyle: getTextStyle(const Locale('de', 'DE')),
            onTap: () => context.setLocale(const Locale('de', 'DE')),
          ),
          // more languages here
          SpeedDialChild(
            label: 'español',
            labelStyle: getTextStyle(const Locale('es', 'ES')),
            onTap: () => context.setLocale(const Locale('es', 'ES')),
          ),
        ],
        child: Icon(
          Icons.language,
          size: SizeConfig.blockSizeHorizontal * 8,
        ),
      ),
    ),
  )

For state management, I use Provider

0

There are 0 best solutions below