Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Please add below in ‘pubspec.yaml’:
webview_flutter: ^0.3.5+2
oktoast: ^2.0.0
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:oktoast/oktoast.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
Widget build(BuildContext context) {
return OKToast(
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
home: WebViewPage(),
class WebViewPage extends StatefulWidget {
State<StatefulWidget> createState() {
return WebViewPageState();
class WebViewPageState extends State<WebViewPage> {
JavascriptChannel _alertJavascriptChannel(BuildContext context) {
return JavascriptChannel(
name: 'Toast',
onMessageReceived: (JavascriptMessage message) {
final Completer<WebViewController> _controller =
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter WebView example'),
body: Builder(builder: (BuildContext context) {
return WebView(
initialUrl: '',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
// _controller = webViewController;
javascriptChannels: <JavascriptChannel>[
navigationDelegate: (NavigationRequest request) {
if (request.url.startsWith('js://webview')) {
showToast('JS调用了Flutter By navigationDelegate');
print('blocking navigation to $request}');
return NavigationDecision.prevent;
print('allowing navigation to $request');
return NavigationDecision.navigate;
onPageFinished: (String url) {
print('Page finished loading: $url');
floatingActionButton: jsButton(),
Widget jsButton() {
return FutureBuilder<WebViewController>(
future: _controller.future,
builder: (BuildContext context,
AsyncSnapshot<WebViewController> controller) {
if (controller.hasData) {
return FloatingActionButton(
onPressed: () async {
_controller.future.then((controller) {
.then((result) {});
child: Text('call JS'),
return Container();

This comment has been minimized.

Copy link

@jimStyle88 jimStyle88 commented Nov 19, 2019


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment