There are many packages to implement such functionality, but in this example we use flutter_inappwebview package.

Step 1 : Add dependency to pubspec.yaml

flutter_inappwebview: ^6.0.0

Step 2 : Create an assets folder in project root directory and create index.html file.

Add HTML content inside file.

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>InAppWebView Example</title>
   <style>
       body {
           font-family: Arial, sans-serif;
           background-color: #f4f4f4;
           margin: 0;
           padding: 0;
           display: flex;
           justify-content: center;
           align-items: center;
           height: 100vh;
       }
       .container {
           background-color: #fff;
           padding: 20px;
           box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
           border-radius: 8px;
           max-width: 400px;
           width: 100%;
           text-align: center;
       }
       h1 {
           color: #333;
           font-size: 24px;
           margin-bottom: 20px;
       }
       input[type="number"] {
           width: 100%;
           padding: 10px;
           margin: 10px 0;
           border: 1px solid #ccc;
           border-radius: 4px;
           font-size: 16px;
       }
       button {
           background-color: #007bff;
           color: white;
           padding: 12px 20px;
           margin-top: 10px;
           border: none;
           border-radius: 4px;
           font-size: 16px;
           cursor: pointer;
           width: 100%;
       }
       button:hover {
           background-color: #0056b3;
       }
       p {
           font-size: 18px;
           margin-top: 20px;
           color: #333;
       }
       .result {
           font-weight: bold;
           color: #007bff;
       }
   </style>
   <script type="text/javascript">
       function callFlutterSum() {
           var num1 = parseInt(document.getElementById("num1").value);
           var num2 = parseInt(document.getElementById("num2").value);
           window.flutter_inappwebview.callHandler('mySum', num1, num2).then(function(result) {
               document.getElementById("result").innerHTML = "Sum from Flutter: <span class='result'>" + result + "</span>";
           });
       }
   </script>
</head>
<body>
<div class="container">
   <h1>InAppWebView Example</h1>
   <input type="number" id="num1" placeholder="Enter first number">
   <input type="number" id="num2" placeholder="Enter second number">
   <button onclick="callFlutterSum()">Get Sum from Flutter</button>
   <p id="result"></p>
</div>
</body>
</html>
  • Here we added the initial file.
  • It provides various callbacks onLoadStarted, onLoadStop, OnConsoleMessage and onWebViewCreated.
  • We are added handler inside onWebViewCreated callback.
  • We added mySum handler and return value to JavaScript side.

Output:

Support On Demand!

Flutter