Home > Blockchain >  Is it possible to listen for system language change in Flutter?
Is it possible to listen for system language change in Flutter?

Time:04-07

Is it possible to listen when the user changes their system language? I need to clear some list when the user changes their language. Now I am detecting the language in the void main() function on startup.

CodePudding user response:

Yes, it is possible. Take a look on this example:

@override
  Widget build(BuildContext context) {
    Locale myLocale = Localizations.localeOf(context);

    return Scaffold(
      appBar: AppBar(
        title: 'Title'),
      ),
      body: Text(myLocale.languageCode == 'fr' ? 'Bonjour!' : 'Hello!'),
    );
  }

CodePudding user response:

You can use Getx library and use:

return GetMaterialApp(
     locale: Get.deviceLocale,   //returns Locale('<language code>', '<country code>')
);

from there you can store it in a variable and once the device language update changes so does the var and there you link your desired function.

  • Related