原生截图
Native screeshot
找了很久关于如何截取Webview的截图,我找到了这个包- native_screenshot。
问题是,它在调试模式下工作,但在生产模式下不工作 - 即使使用 pub.dev 中发布的示例也是如此。
我已将所需权限添加到 android 清单,但仍然无法正常工作。
我已经向开发者报告了这个问题,还在等待答复。我想知道这里是否有人可以提供帮助。
我需要帮助才能完成这项工作。感谢所有帮助。谢谢
webview_flutter
插件不提供获取 WebView 屏幕截图的方式或方法。
所以,你可以试试我的插件 flutter_inappwebview,这是一个 Flutter 插件,允许你添加内联 WebViews 或打开应用内浏览器 window 并且有很多事件、方法和选项可以控制WebViews.
要截取屏幕截图,您可以使用 InAppWebViewController.takeScreenshot
方法,该方法截取 WebView 可见视口的屏幕截图(PNG 格式)和 returns Uint8List
.
下面是一个示例,它在页面停止加载时截取 WebView 的屏幕截图,并显示带有相应屏幕截图的警告对话框:
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(initialRoute: '/', routes: {
'/': (context) => InAppWebViewExampleScreen(),
});
}
}
class InAppWebViewExampleScreen extends StatefulWidget {
@override
_InAppWebViewExampleScreenState createState() =>
new _InAppWebViewExampleScreenState();
}
class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
InAppWebViewController webView;
Uint8List screenshotBytes;
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("InAppWebView")),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: InAppWebView(
initialUrl: "https://github.com/flutter",
initialHeaders: {},
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
debuggingEnabled: true),
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {},
onLoadStop: (InAppWebViewController controller, String url) async {
screenshotBytes = await controller.takeScreenshot();
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Image.memory(screenshotBytes),
);
},
);
},
))
])));
}
}
找了很久关于如何截取Webview的截图,我找到了这个包- native_screenshot。
问题是,它在调试模式下工作,但在生产模式下不工作 - 即使使用 pub.dev 中发布的示例也是如此。
我已将所需权限添加到 android 清单,但仍然无法正常工作。
我已经向开发者报告了这个问题,还在等待答复。我想知道这里是否有人可以提供帮助。
我需要帮助才能完成这项工作。感谢所有帮助。谢谢
webview_flutter
插件不提供获取 WebView 屏幕截图的方式或方法。
所以,你可以试试我的插件 flutter_inappwebview,这是一个 Flutter 插件,允许你添加内联 WebViews 或打开应用内浏览器 window 并且有很多事件、方法和选项可以控制WebViews.
要截取屏幕截图,您可以使用 InAppWebViewController.takeScreenshot
方法,该方法截取 WebView 可见视口的屏幕截图(PNG 格式)和 returns Uint8List
.
下面是一个示例,它在页面停止加载时截取 WebView 的屏幕截图,并显示带有相应屏幕截图的警告对话框:
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(initialRoute: '/', routes: {
'/': (context) => InAppWebViewExampleScreen(),
});
}
}
class InAppWebViewExampleScreen extends StatefulWidget {
@override
_InAppWebViewExampleScreenState createState() =>
new _InAppWebViewExampleScreenState();
}
class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
InAppWebViewController webView;
Uint8List screenshotBytes;
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("InAppWebView")),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: InAppWebView(
initialUrl: "https://github.com/flutter",
initialHeaders: {},
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
debuggingEnabled: true),
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {},
onLoadStop: (InAppWebViewController controller, String url) async {
screenshotBytes = await controller.takeScreenshot();
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Image.memory(screenshotBytes),
);
},
);
},
))
])));
}
}