android - Flutter - 在 webview 上拉动刷新?

标签 android flutter webview flutter-layout

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

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

What I tried so far is: SwipeDetector package and Pull-to-refresh package both which doesn't work, pull to refresh almost works but it closes my app entirely (I've followed the documentation but it seems it works fine on listviews only)

我的代码是这样的,如果你想看的话:

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();
        },
      ),
    ),
        ),
      );
  }
}

最佳答案

也许你可以尝试使用 RefreshIndicator,我个人还没有在 WebView 中尝试过,但希望它能起作用。

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

关于android - Flutter - 在 webview 上拉动刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59744142/

相关文章:

dart - 如何设置Flutter的初始值autocomplete_textfield

android - flutter nfc 插件只扫描一次

dart - 缩短我的文本并在一个小弹出窗口中显示原始版本(Text Overflow.ellipsis 给出错误)

android - 在 Android 中将 res/font 中的自定义字体与 WebView 结合使用

react-native - Expo Webview 加载本地 HTML 文件

android - dependency commons-logging :commons-logging:1. 2 被调试忽略,因为它可能与 Android 提供的内部版本冲突

android - 带有 RxJava 的 MVVM,没有数据绑定(bind)

java - 根据 EditText 值更改 SeekBar 进度

android - TextView 和 EditText 在 LinearLayout 中被截断

macos - MacOS 中的 WebView : How to request file system permissions correctly