问题描述
我正在使用Flutter制作一个基于标签栏的简单应用。它使用CupertinoTabScaffold
和CupertinoTabBar
。每个CupertinoTabView
都有自己的WebView
。
在第一个TabView
中,放置一个按钮,如果单击该按钮,则将更改为下一个选项卡,以加载包含CupertinoTabView
小部件的WebView
,然后导航至某些网站网址(请注意,该网址未写在WebView:initialURL
上,而是从第一个标签中检索到的)。
问题在于,当单击按钮时,在更改了所选标签的情况下,WebView
不会加载特定的URL,因为未分配WebView
控制器。
如何使应用“等待”新的CupertinoTabView
小部件完全加载?
如果我运行该应用程序,则错误如下:
Unhandled Exception: NoSuchMethodError: The method 'navigateTo' was called on null.
这似乎是因为单击按钮执行以下代码时,在行tabController.index = tabIndex
生效之前,调用了keyWebPage.curentState.navigateTo
(现在为 null )。 / p>
tabController.index = tabIndex;
keyWebPage.currentState.navigateTo(url); //executed 'before' the new TabView initialization
我怀疑是这个问题的原因,但不知道如何解决。
main.dart
final GlobalKey<PageWebState> keyWebPage = GlobalKey();
class _MyHomePageState extends State<MyHomePage> {
final CupertinoTabController tabController = CupertinoTabController();
int _currentTabIndex = 0;
WebPage pageWeb;
@override
void initState() {
super.initState();
pageWeb = new PageWeb(
this,key: keyWebPage,);
}
void navigateTab(int tabIndex,String url) {
setState(() {
_currentTabIndex = tabIndex;
tabController.index = tabIndex;
// HERE is the error occurring part
keyWebPage.currentState.navigateTo(url);
});
}
@override
Widget build(BuildContext context) {
final Widget scaffold = CupertinoTabScaffold(
controller: tabController,tabBar: CupertinoTabBar(
currentIndex: _currentTabIndex,onTap: (index) {
setState(() {
_currentTabIndex = index;
});
},items: const <BottomNavigationBarItem>[
const BottomNavigationBarItem(icon: const Icon(Icons.home),title: Text('Start')),const BottomNavigationBarItem(icon: const Icon(Icons.web),title: Text('Webpage')),],),tabBuilder: (context,index) {
CupertinoTabView _viewWidget;
switch (index) {
case 0:
_viewWidget = CupertinoTabView(
builder: (context) {
return Center(
child: CupertinoButton(
child: Text('Navigate'),onPressed: () {
navigateTab(1,'https://stackoverflow.com/');
},);
},);
break;
case 1:
_viewWidget = CupertinoTabView(
builder: (context) {
return pageWeb;
},);
break;
}
return _viewWidget;
},);
return scaffold;
}
}
WebPage.dart
final Completer<WebViewController> _controller = Completer<WebViewController>();
class PageWeb extends StatefulWidget {
final delegate;
final ObjectKey key;
PageWeb(this.delegate,this.key);
@override
PageWebState createState() {
return PageWebState();
}
}
void navigateTo(String url) { //Function that will be called on main.dart
controller.future.then((controller) => controller.loadUrl(url));
}
class PageWebState extends State<PageWeb> {
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text("Test"),child: SafeArea(
child: Container(
child: new WebView(
key: widget.key,initialUrl: "https://www.google.com/",javascriptMode: JavascriptMode.unrestricted,onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},))));
}
}
请注意,由于要更改URL,并且单击按钮可以多次,因此不考虑使用构造函数传递变量。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)