0

我使用包 CubertoBottomBar 添加了底部导航栏。我想通过 url_launcher 包在其他浏览器中启动新的 url,当我实现它时它会抛出一个错误,因为类型 'Future' 不是类型 'Widget' 的子类型,请帮我解决这个问题。

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentIndex = 0;
  Color inactiveColor = Colors.white;
  String currentTitle = "Home";
  Color currentColor = Colors.white;

  final List<Widget> _children = [
    HomePage(),
    Contact(),
    _urlLauncher()
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: _children[_currentIndex],
      bottomNavigationBar: CubertoBottomBar(
        barBackgroundColor: Colors.orange,
        inactiveIconColor: inactiveColor,
        tabStyle: CubertoTabStyle.STYLE_FADED_BACKGROUND,
        selectedTab: _currentIndex,
        tabs: [
          TabData(iconData: Icons.home, title: "Home", tabColor: Colors.white),
          TabData(iconData: Icons.phone, title: "Contact", tabColor: Colors.white),
          TabData(iconData: Icons.person_outline, title: "Register", tabColor: Colors.white),
        ],
        onTabChangedListener: (position, title, color) {
          setState(() {
            _currentIndex = position;
            currentTitle = title;
            currentColor = color;
          });
        },
      ),
    );
  }
  void onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  static _urlLauncher() async{
    const url = 'https://flutter.dev';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }
}
4

2 回答 2

2

_urlLauncher()是一个未来,因此您不能将其添加到小部件列表中,List<Widget>也不能像小部件一样显示它

final List<Widget> _children = [
    HomePage(),
    Contact(),
    _urlLauncher()
  ];

你可以这样做:

_urlLauncher()将小部件列表中的更改 为然后像这样Container调用函数onTabChangedListener:

final List<Widget> _children = [
    HomePage(),
    Contact(),
    Container()
  ];
onTabChangedListener: (position, title, color) {
   if(position == 2){
     _urlLauncher(); // open in browser if the position is 2
   }else{
     setState(() {
       _currentIndex = position;
       currentTitle = title;
       currentColor = color;
     });
  }   
},
于 2020-08-06T10:21:20.083 回答
0

问题是,它会打开一个url. 但仍然需要一个Widget来包含它。您bottomNavigationBar期望的页面。

解决方案:url_launcher将内部包裹Container()起来,另外,不要忘记设置forceWebView: true内部_urlLauncher()

现在让我们跳转到代码,看看我们如何实现这一点

final List<Widget> _children = [
    HomePage(),
    Contact(),
    Container()
];

你会很高兴去的。另外,现在做forceWebView

static _urlLauncher() async{
    const url = 'https://flutter.dev';
    if (await canLaunch(url)) {
      await launch(url, forceWebView: true);
    } else {
      throw 'Could not launch $url';
    }
  }

并让我们onTabTapped()知道,当按下位置 2 时,我们必须调用该_urlLauncher()方法

void onTabTapped(int index) {
    setState(() {
      _currentIndex = index;

      // here you do the thing
      if (_currentIndex == 2) _urlLauncher();
    });
  }
于 2020-08-06T10:23:11.297 回答