我对 Flutter BottomNavigationBar 有疑问。我用 3 个项目构建了一个 BottomNavigationBar,完整的代码是
class TabPage extends StatefulWidget {
@override
_TabPageState createState() => _TabPageState();
}
class _TabPageState extends State<TabPage> {
int _currentIndex = 0;
final Map<int, Widget> _routes = <int, Widget>{
0: MainTab(),
1: NotificationTab(),
2: BlocProvider(
create: (context) => ProfileBloc(DataProvider()),
child: ProfileTab(),
),
};
void _onTabTapped(int index) {
setState(() {
_currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
showUnselectedLabels: false,
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
onTap: _onTabTapped,
items: [
BottomNavigationBarItem(
icon: Icon(
Icons.language,
),
label: AppLocalizations.of(context).translate("home"),
),
BottomNavigationBarItem(
icon: Icon(
Icons.notifications,
),
label: AppLocalizations.of(context).translate("notification"),
),
BottomNavigationBarItem(
icon: Icon(
Icons.person,
),
label: AppLocalizations.of(context).translate("profile"),
)
],
),
body: _routes[_currentIndex],
);
}
}
如果我从一个选项卡切换到另一个选项卡,一切正常,问题是如果用户点击当前选定的选项卡,该选项卡不会重新加载。第一个选项卡是一个带有 WebView 的小部件,所以我希望用户返回到 initialUrl,即使他已经在该选项卡上。似乎点击当前选择的选项卡没有任何效果。有没有办法重建后代小部件?
class MainTab extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: WebView(
initialUrl: "https://www.google.com/",
javascriptMode: JavascriptMode.unrestricted,
),
);
}
}