我如何在默认浏览器中从BottomNavigationBarItem打开URL?

问题描述 投票:0回答:1

我已经用Flutter实现了以下BottomNavigationBarItem。onTap或onPressed我希望从左侧开始的第一个图标(情况0)使用url_launcher.dart在默认浏览器中打开URL。

我该如何编码?

BottomNavigationBarItem

 void _selectTab(int tabItem) {
    setState(() {
      widget.currentTab = tabItem;
      switch (tabItem) {
        case 0:
         widget.currentPage = ProfileWidget(parentScaffoldKey: widget.scaffoldKey);
          break;
        case 1:
          widget.currentPage = OrdersWidget(parentScaffoldKey: widget.scaffoldKey);
          break;
        case 2:
          widget.currentPage = OrdersHistoryWidget(parentScaffoldKey: widget.scaffoldKey);
          break;
      }
    });
  }

          items: [
            BottomNavigationBarItem( 
              icon: Icon(Icons.person),
              title: new Container(height: 0.0),
            ),
            BottomNavigationBarItem(
                title: new Container(height: 5.0),
                icon: Container(
                  width: 42,
                  height: 42,
                  decoration: BoxDecoration(
                    color: Theme.of(context).accentColor,
                    borderRadius: BorderRadius.all(
                      Radius.circular(50),
                    ),
                    boxShadow: [
                      BoxShadow(
                          color: Theme.of(context).accentColor.withOpacity(0.4), blurRadius: 40, offset: Offset(0, 15)),
                      BoxShadow(
                          color: Theme.of(context).accentColor.withOpacity(0.4), blurRadius: 13, offset: Offset(0, 3))
                    ],
                  ),
                  child: new Icon(Icons.shopping_basket, color: Theme.of(context).primaryColor),
                )),
            BottomNavigationBarItem(
              icon: new Icon(Icons.history),
              title: new Container(height: 0.0),
            ),
          ],

在另一页上,我在ListTile上使用了以下代码,并且效果很好。

                    onTap: () async {
                    const url = 'http://google.com';
                    if (await canLaunch(url)) {
                      await launch(url, forceSafariVC: false);
                    } else {
                      throw 'Could not launch $url';
                    }
                  },
flutter flutter-layout
1个回答
1
投票

您是否不能将以下内容添加到_onTap函数中?>

void _selectTab(int tabItem) async { //async is new here!
   if(tabItem == 0){
      const url = 'http://test.url';
      if (await canLaunch(url)) {
         await launch(url);
      } else {
         throw 'Could not launch $url';
      }
      return;
   }
   .
   .
   .
}
© www.soinside.com 2019 - 2024. All rights reserved.