Search code examples
flutterpull-to-refreshflutterwebviewplugin

How can i pull to refresh in flutter


webview_flutter: ^3.0.2 package using but no matter which pull to refresh package I add to the code, I could not run it, if you could help me, I would be very grateful. Thank you in advance.

  Future<void> _refreshWebView() async {
    setState(() {
      _isLoading = true;
    });
    await _webViewController.reload();
    setState(() {
      _isLoading = false;
    });
  }
return WillPopScope(
      onWillPop: _onBackPressed,
      child: Scaffold(
        appBar: AppBar(
          toolbarHeight: 0,
          backgroundColor: APP_COLOR,
        ),
        body: Stack(
          children: [
            Expanded(
              child: WebView(
                key: _webViewKey,
                initialUrl: _cachedUrl ?? WEB_VIEW_URL,
                javascriptMode: JavascriptMode.unrestricted,
                onPageFinished: (String url) {
                  setState(() {
                    _isLoading = false;
                  });
                },
                onWebViewCreated: (controller) {
                  _webViewController = controller;
                },
              ),
            ),
            if (_isLoading) LinearProgressIndicator(),
          ],
        ),
      ),
    );

Solution

  • Please See This Reposetri : https://github.com/RomitKatrodiya/in_app_webview

    Code

    import 'dart:io';
    
    import 'package:flutter/material.dart';
    import 'package:flutter_inappwebview/flutter_inappwebview.dart';
    
    void main() {
      runApp(const MaterialApp(
        debugShowCheckedModeBanner: false,
        home: HomePage(),
      ));
    }
    
    class HomePage extends StatefulWidget {
      const HomePage({Key? key}) : super(key: key);
    
      @override
      State<HomePage> createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      final GlobalKey webViewKey = GlobalKey();
    
      InAppWebViewController? webViewController;
    
      InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
          crossPlatform: InAppWebViewOptions(
            useShouldOverrideUrlLoading: true,
            mediaPlaybackRequiresUserGesture: false,
          ),
          android: AndroidInAppWebViewOptions(
            useHybridComposition: true,
          ),
          ios: IOSInAppWebViewOptions(
            allowsInlineMediaPlayback: true,
          ));
    
      late PullToRefreshController pullToRefreshController;
      String url = "";
      final urlController = TextEditingController();
    
      List bookMarksList = [];
    
      @override
      void initState() {
        super.initState();
    
        pullToRefreshController = PullToRefreshController(
          options: PullToRefreshOptions(
            color: Colors.blue,
          ),
          onRefresh: () async {
            if (Platform.isAndroid) {
              webViewController?.reload();
            } else if (Platform.isIOS) {
              webViewController?.loadUrl(
                  urlRequest: URLRequest(url: await webViewController?.getUrl()));
            }
          },
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SafeArea(
            child: Stack(
              children: [
                Column(
                  children: [
                    Expanded(
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: TextField(
                          decoration: const InputDecoration(
                            prefixIcon: Icon(Icons.search),
                            border: OutlineInputBorder(),
                          ),
                          controller: urlController,
                          keyboardType: TextInputType.url,
                          onSubmitted: (value) {
                            var url = Uri.parse(value);
                            if (url.scheme.isEmpty) {
                              url = Uri.parse(
                                  "https://www.google.com/search?q=$value");
                            }
                            webViewController?.loadUrl(
                                urlRequest: URLRequest(url: url));
                          },
                        ),
                      ),
                    ),
                    Expanded(
                      flex: 12,
                      child: InAppWebView(
                        key: webViewKey,
                        initialUrlRequest: URLRequest(
                          url: Uri.parse("https://www.google.co.in"),
                        ),
                        initialOptions: options,
                        pullToRefreshController: pullToRefreshController,
                        onWebViewCreated: (controller) {
                          webViewController = controller;
                        },
                        onLoadStop: (controller, url) async {
                          pullToRefreshController.endRefreshing();
                          setState(() {
                            this.url = url.toString();
                            urlController.text = this.url;
                          });
                        },
                      ),
                    ),
                  ],
                ),
                Container(
                  alignment: Alignment.bottomCenter,
                  padding: const EdgeInsets.all(10),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      FloatingActionButton(
                        onPressed: () {
                          webViewController?.goBack();
                        },
                        mini: true,
                        child: const Icon(Icons.arrow_back_ios_new_outlined),
                      ),
                      FloatingActionButton(
                        onPressed: () {
                          bookMarksList.add(url);
                          bookMarksList = bookMarksList.toSet().toList();
                        },
                        mini: true,
                        child: const Icon(Icons.bookmark_add_outlined),
                      ),
                      FloatingActionButton(
                        onPressed: () {
                          showDialog(
                            context: context,
                            builder: (BuildContext dialogContext) {
                              return AlertDialog(
                                title: const Center(child: Text('All BookMarks')),
                                content: SizedBox(
                                  height: MediaQuery.of(context).size.width * 0.75,
                                  width: MediaQuery.of(context).size.width * 0.75,
                                  child: ListView.separated(
                                    itemCount: bookMarksList.length,
                                    itemBuilder: (context, i) {
                                      return ListTile(
                                        onTap: () {
                                          Navigator.of(context).pop();
                                          webViewController?.loadUrl(
                                            urlRequest: URLRequest(
                                              url: Uri.parse(bookMarksList[i]),
                                            ),
                                          );
                                        },
                                        title: Text(
                                          bookMarksList[i],
                                          overflow: TextOverflow.ellipsis,
                                          style: const TextStyle(
                                              color: Colors.blueAccent),
                                        ),
                                      );
                                    },
                                    separatorBuilder: (context, i) {
                                      return const Divider(
                                        color: Colors.black,
                                        endIndent: 30,
                                        indent: 30,
                                      );
                                    },
                                  ),
                                ),
                              );
                            },
                          );
                        },
                        mini: true,
                        child: const Icon(Icons.bookmark_border),
                      ),
                      FloatingActionButton(
                        onPressed: () {
                          webViewController?.loadUrl(
                            urlRequest: URLRequest(
                              url: Uri.parse("https://www.google.co.in"),
                            ),
                          );
                        },
                        mini: true,
                        child: const Icon(Icons.home),
                      ),
                      FloatingActionButton(
                        onPressed: () {
                          webViewController?.reload();
                        },
                        mini: true,
                        child: const Icon(Icons.refresh),
                      ),
                      FloatingActionButton(
                        onPressed: () {
                          webViewController?.goForward();
                        },
                        mini: true,
                        child: const Icon(Icons.arrow_forward_ios_sharp),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }