Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change language promatically #15

Closed
falll2000 opened this issue Oct 8, 2018 · 7 comments
Closed

How to change language promatically #15

falll2000 opened this issue Oct 8, 2018 · 7 comments

Comments

@falll2000
Copy link

I use this plug-in can change language when app start-up.

But if I want to change language programmatically, who to do so.

ex: change language when I click a button.

thanks a lot :)

@NMateu
Copy link

NMateu commented Oct 16, 2018

hi,
this should help you https://gist.github.com/ilikerobots/474b414138f3f99150dbb3d0cc4cc721

@long1eu
Copy link
Owner

long1eu commented Nov 19, 2018

Thanks @NMateu

@long1eu long1eu closed this as completed Nov 19, 2018
@etet2007
Copy link

But it seems that you have to change the state of MaterialApp, and this will reload the entire app. Is there any ways to change language in a setting page but not reload the app? Thanks!

@long1eu
Copy link
Owner

long1eu commented Jan 11, 2019

I don't see the problem rebuilding the MaterialApp widget.

@etet2007
Copy link

I don't see the problem rebuilding the MaterialApp widget.

It's usable but other widgets will lose their states. I think it's best to stay in the same page after you change language.
https://github.com/flutterchina/gitme
This app is an example.

@long1eu
Copy link
Owner

long1eu commented Jan 11, 2019

No they won't lose their state. They will be rebuilt with the new Localization parent. Can you provide a MWE ? Maybe I don't get it right.

@etet2007
Copy link

etet2007 commented Jan 11, 2019

No they won't lose their state. They will be rebuilt with the new Localization parent. Can you provide a MWE ? Maybe I don't get it right.

You are right. It's my mistake. There is another problem to make my app reload.

What's more. I find that we can change locale field in MaterialApp to "change language promatically ".

class _AppState extends State<App> {

  var locale;
  changeLocale(Locale locale) {
    setState(() {
      this.locale = locale;
    });
  }

  @override
  Widget build(BuildContext context) {
    return LocaleWidget(
      child: MaterialApp(
        title: UIData.appName,
        locale: locale,     // THIS
        home: MainPage(),
        localizationsDelegates: [
          S.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        supportedLocales: S.delegate.supportedLocales,
      ),
      changeLocale: changeLocale,
    );
  }

Your plugin is so great. I am deeply grateful for you help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants