Search code examples
flutterdartwidgetstaterebuild

Flutter rebuild parent widget


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(),
     ),
   );
 }
}

Solution

  • 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,
        );
      }
    }