How To Webview Run Javascript Using Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 27-11-2020

Have you ever consider how to run or execute Javascript code in flutter webview? Imagine using a flutter webview in your application and you want a situation where an alert dialog can be triggered understand certain condition.

How To Webview Run Javascript  Using Flutter Android App

 Webview Run Javascript
Step 1 
We cannot directly remove the time stamp from  Webview Run Javascript  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
  flutter_webview_plugin: 0.3.0+2

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



Complete Code For  Webview Run Javascript In Flutter
main.dart
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}
class MyHomePage extends StatefulWidget {
  final String title;
  MyHomePage({this.title});
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>{
  final flutterWebviewPlugin = new FlutterWebviewPlugin();

  @override
  void initState(){
    super.initState();
    flutterWebviewPlugin.evalJavascript("alert('Bajarangisoft.com')");


  }
  @override
  void dispose() {
    flutterWebviewPlugin.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WebviewScaffold(
      url: 'https://Bajarangisoft.com',
      hidden: true,
      appBar: AppBar(title: Text("Run JS in Webview")),
    );
  }
}

 

Related Post