【发布时间】:2019-06-24 15:03:01
【问题描述】:
我正在尝试将 webview_flutter WebView 连接到 BottomNavigationBar。
我希望在点击选项卡时使用新 URL 重新加载视图。在
onTap 回调我将 _currentUrl 更新为新的 url,并使用新的标签索引调用 set state。
为什么标签栏更新了,但网页视图没有重建? 我错过了什么?
class WebViewApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return WebViewState();
}
}
class WebViewState extends State<WebViewApp> {
int _currentTabIndex = 0;
String _currentUrl = URL.home;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter WebView Demo',
home: Scaffold(
body: WebView(initialUrl: _currentUrl),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentTabIndex,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: Text('Search'),
),
BottomNavigationBarItem(
icon: Icon(Icons.calendar_today),
title: Text('Profile'),
),
],
onTap: (index) {
switch (index) {
case 0:
_currentUrl = URL.home;
break;
case 1:
_currentUrl = URL.search;
break;
case 2:
_currentUrl = URL.calendar;
break;
}
setState(() {
_currentTabIndex = index;
});
},
),
),
);
}
}
【问题讨论】:
标签: flutter flutter-layout setstate