3

I need help. I have a Dropdown widget in LanguageDropdown class, where the user can select the language. And the widget is inside a settings page widget in Settings class. The language changes on other pages, but not on current one. How can I rebuild that specific page, so the language changes on this one also?
See the code below

import 'package:jptapp/features/settings/change_language/app_localization.dart';

class LanguageDropDown extends StatefulWidget {
  @override
  _LanguageDropDownState createState() {
    return _LanguageDropDownState();
  }
}

class _LanguageDropDownState extends State<LanguageDropDown> {
  String _value = allTranslations.currentLanguage;
  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      items: [
        DropdownMenuItem<String>(
          child: Text('English'),
          value: 'en',
        ),
        DropdownMenuItem<String>(
          child: Text('Magyar'),
          value: 'hu',
        ),
        DropdownMenuItem<String>(
          child: Text('Srpski'),
          value: 'rs',
        ),
      ],
      onChanged: (String value) {
        setState(() async{
          _value = value;
          await allTranslations.setNewLanguage(_value);
        });
      },
      hint: Text(_value),
      value: _value,
    );
  }
}
import 'package:jptapp/core/constants/colors.dart';
import 'package:jptapp/features/settings/change_language/app_localization.dart';
import 'package:jptapp/features/settings/widgets/widgets.dart';

class Settings extends StatefulWidget {
 @override
 _SettingsState createState() => _SettingsState();
}

class _SettingsState extends State<Settings> {
 @override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       centerTitle: true,
       backgroundColor: MyColors.appBarColor,
       title: Text(
         allTranslations.text('settings'),
       ),
     ),
     body: ListView(
       children: ListTile.divideTiles(
         context: context,
         tiles: [
           ListTile(
             trailing: ThemeChangerAnimationButton(),
             title: Text(
               allTranslations.text('darkmode'),
             ),
           ),
           ListTile(
             trailing: LanguageDropDown(),
             title: Text(
               allTranslations.text('language'),
             ),
           ),
         ],
       ).toList(),
     ),
   );
 }
}

1 Answer 1

1

I'm not sure this will work but try this:

import 'package:flutter/material.dart';
import 'package:jptapp/features/settings/change_language/app_localization.dart';

class LanguageDropDown extends StatefulWidget {
  @override
  _LanguageDropDownState createState() {
    return _LanguageDropDownState();
  }
}

class _LanguageDropDownState extends State<LanguageDropDown> {
  String _value = allTranslations.currentLanguage;

  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      items: [
        DropdownMenuItem<String>(
          child: Text('English'),
          value: 'en',
        ),
        DropdownMenuItem<String>(
          child: Text('Magyar'),
          value: 'hu',
        ),
        DropdownMenuItem<String>(
          child: Text('Srpski'),
          value: 'rs',
        ),
      ],
      onChanged: (String value) {
        setState(() async {
          _value = value;
          await allTranslations.setNewLanguage(_value);
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => Settings()
              ));
        });
      },
      hint: Text(_value),
      value: _value,
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.