Flutter重建父窗口小部件

时间:2020-04-13 00:28:47

标签: flutter dart widget state rebuild

我需要帮助。我在LanguageDropdown类中有一个Dropdown小部件,用户可以在其中选择语言。该小部件位于Settings类中的设置页面小部件内。语言在其他页面上更改,但在当前页面上没有更改。如何重建该特定页面,因此该页面上的语言也会更改?
参见下面的代码

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 个答案:

答案 0 :(得分:0)

我不确定是否可以,但是请尝试以下操作:

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