import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin_ios_android/flutter_webview_plugin_ios_android.dart';
import 'dart:async';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
routes: {
'/': (_) => const MyHomePage(),
}
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String selectedUrl = 'https://грибовы.рф';
final flutterWebViewPlugin = FlutterWebviewPlugin();
// при изменении url
late StreamSubscription<String> _onUrlChanged;
// при ошибке
late StreamSubscription<WebViewHttpError> _onHttpError;
// изменение процента загрузки страницы
late StreamSubscription<double> _onProgressChanged;
// поскролили вверх-вниз
late StreamSubscription<double> _onScrollYChanged;
// поскролили вправо-влево
late StreamSubscription<double> _onScrollXChanged;
@override
void dispose() {
_onUrlChanged.cancel();
_onHttpError.cancel();
_onProgressChanged.cancel();
_onScrollXChanged.cancel();
_onScrollYChanged.cancel();
flutterWebViewPlugin.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
flutterWebViewPlugin.close();
// Слушатель изменения url страницы
_onUrlChanged = flutterWebViewPlugin.onUrlChanged.listen((String url) {
print("URL: $url");
});
// изменение прогресса загрузки
_onProgressChanged =
flutterWebViewPlugin.onProgressChanged.listen((double progress) {
setState(() {
print('onProgressChanged: $progress');
});
});
_onScrollYChanged =
flutterWebViewPlugin.onScrollYChanged.listen((double y) {
});
_onScrollXChanged =
flutterWebViewPlugin.onScrollXChanged.listen((double x) {
});
_onHttpError =
flutterWebViewPlugin.onHttpError.listen((WebViewHttpError error) {
print("Ошибка загрузки: ${error.code} ${error.url}");
});
}
// перехват вызовов Javascript
final Set<JavascriptChannel> jsChannels = [
JavascriptChannel(
name: 'Print',
onMessageReceived: (JavascriptMessage message) {
print(message.message);
}),
].toSet();
@override
Widget build(BuildContext context) {
return WebviewScaffold(
ignoreSSLErrors: true,
url: selectedUrl,
javascriptChannels: jsChannels,
mediaPlaybackRequiresUserGesture: false,
appBar: AppBar(
title: const Text('Widget WebView'),
),
withZoom: true,
withLocalStorage: true,
hidden: false,
initialChild: Container(
color: Colors.redAccent,
child: const Center(
child: Text('Ожидаем загрузки...'),
),
),
);
}
}