Automatically adjust the language of the page when you open it using getX in flutter

Issue

Automatically adjust the language of the page when you open it using getX in flutter, I want to choose the language on a page, and when I choose it, I will move to another page and be in the language I chose

Container(
                margin: const EdgeInsets.only(top: 20.0),
                padding: const EdgeInsets.only(left: 20.0, right: 20.0),
                child: new Row(
                  children: <Widget>[
                    new Expanded(
                      child: FlatButton(
                        shape: new RoundedRectangleBorder(
                            borderRadius: new BorderRadius.circular(30.0)),
                        splashColor: this.primaryColor,
                        color: this.primaryColor,
                        child: new Row(
                          children: <Widget>[
                            new Padding(
                              padding: const EdgeInsets.only(left: 20.0),
                              child: Text(
                                "English",
                                style: TextStyle(color: Colors.white),
                              ),
                            ),
                            new Expanded(
                              child: Container(),
                            ),
                            new Transform.translate(
                              offset: Offset(15.0, 0.0),
                              child: new Container(
                                padding: const EdgeInsets.all(5.0),
                                child: FlatButton(
                                  shape: new RoundedRectangleBorder(
                                      borderRadius:
                                          new BorderRadius.circular(28.0)),
                                  splashColor: Colors.white,
                                  color: Colors.white,
                                  child: Icon(
                                    Icons.arrow_forward,
                                    color: this.primaryColor,
                                  ),
                                  onPressed: () {
                                    box.write('lang', 'en_US');
                                    Get.to(() => LoginScreen1());
                                  },
                                ),
                              ),
                            )
                          ],
                        ),
                        onPressed: () {
                          box.write('lang', 'en_US');
                          Get.to(() => LoginScreen1());
                        },
                      ),
                    ),
                  ],
                ),
              ),

the second page

Padding(
                  padding: const EdgeInsets.only(left: 40.0),
                  child: Text(
                    'serverAddress'.tr,
                    style: TextStyle(color: Colors.grey, fontSize: 16.0),
                  ),
                ),

There is more than this too

Solution

In the onPressed function, we can use the Get.updateLocale function accordingly:

onPressed: () {
  box.write('lang', 'en_US');
  Locale locale = Locale('en','en_US');
  Get.updateLocale(locale);
  Get.to(() => LoginScreen1());
}

Answered By – Stefano Amorelli

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.