gpt4 book ai didi

Flutter 重建父部件

转载 作者:行者123 更新时间:2023-12-03 03:04:21 25 4
gpt4 key购买 nike

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

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

关于Flutter 重建父部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61179965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com