【问题标题】:Flutter - pull to refresh on a webview?Flutter - 拉动以刷新网页视图?
【发布时间】:2020-05-01 18:58:52
【问题描述】:

您好,我只是想问一下是否可以在 Flutter 中重新加载 WebView?我四处搜索,他们中的大多数都使用 FAB(浮动操作按钮)来重新加载网站,但在 WebViews 方面几乎没有,大部分都是针对 ListViews。

有没有办法在 WebView 屏幕中进行操作?

到目前为止我尝试的是:SwipeDetector 包Pull-to-refresh 包 两者都不起作用,pull to refresh 几乎可以工作,但它会关闭我的应用完全(我已遵循文档,但似乎仅在列表视图上运行良好)

我的代码是这样的,以防你想看:

class _HomePageState extends State<HomePage> {
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  WebViewController myController;
  final flutterWebviewPlugin = new WebView();

  _exitApp(BuildContext context, Future<WebViewController> controller) async {
    controller.then((data) async {
      WebViewController controller = data;
      var goBack = await controller.canGoBack();
      if (goBack == true) {
        print("onwill go back");
        controller.goBack();
      } else {
        print("onwill not go back");
        Navigator.pop(context);
      }
    });
  }

  @override
  void initState() {
    super.initState();
    firebaseCloudMessagingListeners();
  }

  void firebaseCloudMessagingListeners() {
    if (Platform.isIOS) iOSPermission();

    _firebaseMessaging.getToken().then((token) {
      print(token);
    });

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        setState(() {
          print("${message['data']['url']}");
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (BuildContext context) => NotificationClicked()));
        });
      },
      onResume: (Map<String, dynamic> message) async {
        print("${message['data']['url']}");
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("${message['data']['url']}");
      },
    );
  }

  void iOSPermission() {
    _firebaseMessaging.requestNotificationPermissions(
        IosNotificationSettings(sound: true, badge: true, alert: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
  }

  Completer<WebViewController> _controller = Completer<WebViewController>();


  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () => _exitApp(context, _controller.future),
        child: SafeArea(
    child: Scaffold(
      body: WebView(
          initialUrl: 'https://syncshop.online/en/',
          javascriptMode: JavascriptMode.unrestricted,
          onWebViewCreated: (controller) {
            _controller.complete(controller);
          },
          onPageFinished: (controller) async {
            (await _controller.future).evaluateJavascript(
        "document.getElementsByClassName('footer-container')[0].style.display='none';");
            (await _controller.future).evaluateJavascript(
        "document.getElementById('st_notification_1').style.display='none';");
            (await _controller.future).evaluateJavascript(
        "document.getElementById('sidebar_box').style.display='none';");
          },
        ),
      floatingActionButton: FutureBuilder<WebViewController>(
        future: _controller.future,
        builder: (BuildContext context,
            AsyncSnapshot<WebViewController> controller) {
          if (controller.hasData) {
            return FloatingActionButton(
              onPressed: () {
                controller.data.reload();
              },
              child: Icon(Icons.refresh),
            );
          }
          return Container();
        },
      ),
    ),
        ),
      );
  }
}

【问题讨论】:

  • 我很乐意为您提供帮助,但是我的 Android 模拟器崩溃了,只有 PullToRefresh 和 WebView 小部件位于同一棵树中。
  • 嘿,这可能是你们需要的 KDC 和 @JoãoSoares stackoverflow.com/questions/57656045/…
  • @leemuljadi 我想我会再试一次。如果我没记错的话,我上次尝试过,但我不确定在 onSwipe: () {} 中放什么,因为答案是使用来自不同 pluin 的 WebviewScaffold 而不仅仅是我使用的 WebView()(来自颤动团队)

标签: android flutter webview flutter-layout


【解决方案1】:

也许你可以尝试使用RefreshIndicator,我个人没有尝试使用WebView,但希望它可以工作。

return Scaffold(
  body: RefreshIndicator(
    onRefresh: () => _controller.reload(),
    child: Webview(),
  ),
),

【讨论】:

  • 您好,刚刚看到您的回答,我想问一下是否可以为void 函数使用不同的名称?因为我已经声明了 initState。我也更新了我的代码
  • 另外,_setupFeed() async { await controller.data.reload(); setState(() { WebView(); }); } 给出一个错误,指出控制器未定义
  • 嗨,上面的代码基本上只是为了展示它是如何工作的,然后你必须根据你的情况修改上面的代码,正如我之前提到的,我从未使用过它用于 WebView。所以想法 _setupFeed() 方法是当你向下拖动屏幕时,它会触发刷新页面的方法。也许用你用来从网络上检索数据的方法来改变 await controller.data.reload() 。为什么放置 _setupFeed(); 的想法initState 是在第一次构建小部件时调用它,这样您就可以将其放入 initState 方法中。
  • 也许可以忽略 initState 和 _setupFeed() 并尝试这样做: return Scaffold( body: RefreshIndicator( onRefresh: () => _controller.reload(), child: Webview(), ), ),
  • 您好我已经尝试并修改了您所说的内容,但我认为这是 webview 的限制,但我仍会寻找解决方案。谢谢
猜你喜欢
  • 2016-11-27
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
相关资源
最近更新 更多