我正在创建一个本地化的应用程序,在主页面上有一个BottomNavigationBar.如果我改变本地化,BottomNavigationBar的项目名称不更新,直到点击其中一个项目。我如何更新BottomNavigationBar?
编码。
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
onTap: onTabTapped,
type: BottomNavigationBarType.fixed,
// new
currentIndex: _currentIndex,
selectedFontSize: 12,
unselectedFontSize: 12,
elevation: 10,
showSelectedLabels: true,
showUnselectedLabels: true,
items: [
new BottomNavigationBarItem(
icon: Icon(MyFlutterAppIOS.share_100, color: Colors.blueGrey),
title: Text(
allTranslations.text("sharetitle"),
style: TextStyle(
color: Colors.blueGrey,
),
),
),
new BottomNavigationBarItem(
icon: Icon(MyFlutterAppIOS.protect_100, color: Colors.blueGrey),
title: Text(
allTranslations.text("lblprivacypolicy"),
style: TextStyle(
color: Colors.blueGrey,
),
),
),
new BottomNavigationBarItem(
icon: Icon(MyFlutterAppIOS.protect_100, color: Colors.blueGrey),
title: Text(
allTranslations.text("lblterms"),
style: TextStyle(
color: Colors.blueGrey,
),
),
),
new BottomNavigationBarItem(
icon: Icon(MyFlutterAppIOS.info_100, color: Colors.blueGrey),
title: Text(
allTranslations.text("lblinfo"),
style: TextStyle(color: Colors.blueGrey),
),
)
],
);
}
那是因为你没有以任何方式重新构建这个小组件。一旦你改变了语言,你需要重新构建这个小组件。你可以使用任何状态管理库,如BLOC或provider,一旦你更新语言,就会通知这个widget的变化.所以当你点击任何项目时,它就会重建,然后你就会看到变化。
ChangeNotifierProvider(
create: (context) => AppLevelProvider(),
child: BottomNavigationBar(),
),
您也可以在这里使用现有的提供者并通知更改。请阅读更多关于状态管理 此处