title: Button to change the language Flutter
body: <p>I am building an app in Flutter, so far I am using the Internationalization with JSON where the language of the app is based on the language that the user has as default in his phone its working pretty well, but I would like to give the user a chance to change the language without changing phone the system language settings, by only clicking in a button and then the application change the language without going through the settings.</p>
  <p>Here is the code:</p>
  <p>The Main:</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:flutter_app_darkmode/app_localizations.dart';
  import 'package:flutter_localizations/flutter_localizations.dart';
  
    void main() =&gt; runApp(MyApp());
  
    class MyApp extends StatelessWidget {
        @override
        Widget build(BuildContext context) =&gt; MaterialApp(
        supportedLocales: [
        Locale('en', &quot;ZA&quot;),
        Locale('pt', &quot;MZ&quot;),
        ],
        localizationsDelegates: [
        AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
        ],
        localeResolutionCallback: (locale, supportedLocales) {
         for (var supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale.languageCode &amp;&amp;
              supportedLocale.countryCode == locale.countryCode) {
            return supportedLocale;
            } else {
            if (MyHomePage.local != null) {
              for (int i = 0; i &lt; supportedLocales.length; i++) {
                if (MyHomePage.local == supportedLocales.elementAt(i)) {
                  return supportedLocales.elementAt(i);
                }}}}}
              return supportedLocales.first;
              },
             home: MyHomePage(),
            );}
  
           class MyHomePage extends StatefulWidget {
           @override
           _MyHomePageState createState() =&gt; _MyHomePageState();
  
             class _MyHomePageState extends State&lt;MyHomePage&gt; {
             getLocale() {
             Locale myLocale = Localizations.localeOf(context);
              print(myLocale);}
  
              @override
               Widget build(BuildContext context) {
                getLocale();
                 return Scaffold(
                  body: Center(
                  child: Padding(
                   padding: const EdgeInsets.all(8.0),
                    child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: &lt;Widget&gt;[
                     Text(
                     AppLocalizations.of(context).translate('first_string'),
                     style: TextStyle(fontSize: 25),
                     textAlign: TextAlign.center,),
                    Text(
                    AppLocalizations.of(context).translate('second_string'),
                      style: TextStyle(fontSize: 25),
                      textAlign: TextAlign.center,),
                      RaisedButton(
                        child: Text('PT'),
                      onPressed: () {},
                       ),],),),),);}}
  </code></pre>
  <p>The app_locations class:</p>
  <pre><code>import 'dart:async';
  import 'dart:convert';
  import 'package:flutter/material.dart';
  import 'package:flutter/services.dart';
  
  class AppLocalizations {
  final Locale locale;
  
    AppLocalizations(this.locale);
  
    static AppLocalizations of(BuildContext context) {
     return Localizations.of&lt;AppLocalizations&gt;(context, AppLocalizations);
    }
  
      static const LocalizationsDelegate&lt;AppLocalizations&gt; delegate =
      _AppLocalizationsDelegate();
  
      Map&lt;String, String&gt; _localizedStrings;
  
         Future&lt;bool&gt; load() async {
         String jsonString =
          await rootBundle.loadString('lang/${locale.languageCode}.json');
  
          Map&lt;String, dynamic&gt; jsonMap = json.decode(jsonString);
  
         _localizedStrings = jsonMap.map((key, value) {
         return MapEntry(key, value.toString());
          });
  
          return true;}
  
         String translate(String key) {
        return _localizedStrings[key];}}
  
       class _AppLocalizationsDelegate
       extends LocalizationsDelegate&lt;AppLocalizations&gt; {
       const _AppLocalizationsDelegate();
  
         @override
         bool isSupported(Locale locale) {
         return ['en', 'pt'].contains(locale.languageCode);}
  
         @override
         Future&lt;AppLocalizations&gt; load(Locale locale) async {
         AppLocalizations localizations = new AppLocalizations(locale);
         await localizations.load();
         return localizations;}
  
          @override
          bool shouldReload(_AppLocalizationsDelegate old) =&gt; false;}
  </code></pre>