【问题标题】:closeWebView after 5 seconds - flutter5秒后closeWebView - 颤动
【发布时间】:2021-09-17 03:32:53
【问题描述】:

我正在尝试制作一个颤动的按钮以在应用程序内打开一个 URL 并在 5 秒后将其关闭,然后返回应用程序。

const url = 'https://pub.dev/packages/url_launcher';
floatingActionButton: FloatingActionButton(
        onPressed: () {
          _launchInBrowser(url);
          Timer(const Duration(seconds: 5), () {
            closeWebView();
          });
        },
        child: Icon(Icons.add),
        backgroundColor: Colors.grey[850],
      ),

这是在应用内打开 URL 的函数:

Future<void> _launchInBrowser(String url) async {
  if (await canLaunch(url)) {
    await launch(
      url,
      forceSafariVC: true,
      forceWebView: false,
      headers: <String, String>{'header_key': 'header_value'},
    );
  } else {
    throw 'Could not launch $url';
  }
}

URL 可以正常打开,但是 5 秒后它没有关闭。我应该使用什么功能来做到这一点? 谢谢!

【问题讨论】:

    标签: android flutter flutter-layout flutter-web


    【解决方案1】:

    计时器在一段时间内反复运行,这可能是它没有关闭的原因。首次运行后尝试使用 cancel() 方法关闭计时器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-16
      • 2010-10-15
      • 2014-03-26
      • 2015-02-21
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      相关资源
      最近更新 更多