Flutter 重建父部件

Flutter rebuild parent widget

我需要帮助。我在 LanguageDropdown class 中有一个下拉小部件,用户可以在其中 select 语言。该小部件位于设置 class 中的设置页面小部件内。其他页面的语言会发生变化,但当前页面不会。我怎样才能重建那个特定的页面,这样这个页面上的语言也会改变?
见下方代码

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

我不确定这是否有效,但试试这个:

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