Home > Mobile >  How can i pull to refresh webview in flutter
How can i pull to refresh webview in flutter

Time:04-08

I want to refresh webview page just when i swipe down the page. I found this code.

import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:rivlus_webview_app/common_widgets/alert_dialog.dart';

class NewWebView extends StatefulWidget {
const NewWebView({Key? key}) : super(key: key);

@override
State<NewWebView> createState() => _NewWebViewState();
}

class _NewWebViewState extends State<NewWebView> {
WebViewController? _controller;
double progress = 0;

@override
Widget build(BuildContext context) {
return SafeArea(
  child: WillPopScope(
    onWillPop: () async {
      final result = await const CommonAlertDialog(
        title: 'Çıkış Yap',
        content: 'Uygulamadan çıkış yapmak istediğinize emin misiniz?',
        mainButtonText: 'Evet',
        cancelButtonText: 'Vazgeç',
      ).show(context);
      if (result == true) {
        return exit(0);
      } else {
        return false;
      }
    },
    child: WebView(
      key: UniqueKey(),
      initialUrl: 'https://google.com',
      javascriptMode: JavascriptMode.unrestricted,
      onWebViewCreated: (WebViewController webViewController) {
        _controller = webViewController;
      },
      gestureRecognizers: Set()
        ..add(Factory<VerticalDragGestureRecognizer>(
                () => VerticalDragGestureRecognizer()
              ..onDown = (DragDownDetails dragDownDetails) {
                _controller!.getScrollY().then((value) {
                  if (value == 0 &&
                      dragDownDetails.globalPosition.direction < 1) {
                    _controller!.reload();
                  }
                });
              })),

    ),
  ),
);
}
}

This is work. But when I click on the top of the screen, it refreshes the page. I dont want this. I just want it to refresh the page when I pull the page down. How can I do that. Thanks.

CodePudding user response:

yes man

wrap your parent Widget with RefreshIndicator

RefreshIndicator(
        onRefresh: () async {
           //reload your url here
          return Future.delayed(const Duration(seconds: 1));
        }

CodePudding user response:

you can use this plugin pull_to_refresh for swipe refresh.

onRefresh method of SmartRefresher

  • Related