【问题标题】:How do I open a URL in default browser from the BottomNavigationBarItem?如何从 BottomNavigationBarItem 在默认浏览器中打开 URL?
【发布时间】:2020-07-29 01:54:28
【问题描述】:

我已经用颤振实现了以下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】:

    难道你不能在你的 _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;
       }
       .
       .
       .
    }
    

    【讨论】:

    • 非常感谢,非常有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 2018-08-21
    • 2014-12-29
    • 2014-12-06
    • 2013-09-19
    • 2021-10-09
    • 2011-07-01
    相关资源
    最近更新 更多