Circular Progress Indicator While Loading WebView In Flutter

admin_img Posted By Bajarangi soft , Posted On 10-09-2020

WebView widget gives us two call back methods onPageFinished() and onPageStarted(). The onPageFinished() method invokes every time when web page done loading its inner contents. The onPageStarted() function invokes every time when WebView start loading web page and until all the web contents will downloaded. We would use these both functions in our tutorial to make the Progress bar visible and invisible from screen using State.

Circular Progress Indicator While Loading WebView In Flutter

Step 1
We cannot directly remove the time stamp from Circular Progress Indicator While Loading WebView  but using the intl.dart package we can easily filter the date stamp from time stamp. So open your flutter project’s pubspec.yaml in code .

dependencies:
  flutter:
    sdk: flutter
  webview_flutter: ^0.3.19+7

 


Step 2
After done saving the pubspec.yaml file, Open your flutter project root folder in Command Prompt or Terminal and execute flutter pub get command. 
flutter pub get

Step 3
 Open your project’s main.dart file and import material.dart and   webview_flutter: ^0.3.19+7. dart package.\
import 'package:webview_flutter/webview_flutter.dart';


Complete Code For Circular Progress Indicator While Loading WebView In Flutter
main.dart
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
        home: Scaffold(
            body: WebViewClass()
        )
    );
  }
}

class WebViewClass extends StatefulWidget {

  WebViewState createState() => WebViewState();

}

class WebViewState extends State<WebViewClass>{

  num position = 1 ;

  final key = UniqueKey();

  doneLoading(String A) {
    setState(() {
      position = 0;
    });
  }

  startLoading(String A){
    setState(() {
      position = 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
            backgroundColor: Colors.green,
            title: Text(' ProgressBar While Loading Webview')),
        body: IndexedStack(
            index: position,
            children: <Widget>[

              WebView(
                initialUrl: 'https://Google.com',
                javascriptMode: JavascriptMode.unrestricted,
                key: key ,
                onPageFinished: doneLoading,
                onPageStarted: startLoading,
              ),

              Container(
                color: Colors.white,
                child: Center(
                    child: CircularProgressIndicator()),
              ),

            ])
    );
  }
}

 

Related Post