Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
S
screenshot
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
openSourceLibrary
screenshot
Commits
d03e0095
提交
d03e0095
authored
2月 10, 2021
作者:
ritheshSalyan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support web and windows platfom
上级
89b80987
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
60 行增加
和
75 行删除
+60
-75
build.gradle
example/android/app/build.gradle
+1
-1
main.dart
example/lib/main.dart
+8
-18
pubspec.yaml
example/pubspec.yaml
+3
-12
screenshot.dart
lib/screenshot.dart
+44
-40
pubspec.yaml
pubspec.yaml
+4
-4
没有找到文件。
example/android/app/build.gradle
浏览文件 @
d03e0095
...
...
@@ -35,7 +35,7 @@ android {
defaultConfig
{
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId
"com.screenshot.example"
minSdkVersion
1
6
minSdkVersion
1
7
targetSdkVersion
30
versionCode
flutterVersionCode
.
toInteger
()
versionName
flutterVersionName
...
...
example/lib/main.dart
浏览文件 @
d03e0095
// import 'dart:io';
import
'dart:typed_data'
;
import
'package:flutter/material.dart'
;
import
'package:screenshot/screenshot.dart'
;
// import 'package:webview_flutter/webview_flutter.dart';
// import 'package:image_gallery_saver/image_gallery_saver.dart';
void
main
(
)
=>
runApp
(
MyApp
());
...
...
@@ -57,6 +58,8 @@ class _MyHomePageState extends State<MyHomePage> {
@override
void
initState
()
{
// if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
super
.
initState
();
}
...
...
@@ -87,23 +90,10 @@ class _MyHomePageState extends State<MyHomePage> {
),
body:
Container
(
child:
new
Center
(
child:
Column
(
mainAxisAlignment:
MainAxisAlignment
.
center
,
children:
<
Widget
>[
Screenshot
(
controller:
screenshotController
,
child:
Column
(
children:
<
Widget
>[
Text
(
'You have pushed the button this many times:'
+
_counter
.
toString
(),
),
FlutterLogo
(),
],
),
),
_imageFile
!=
null
?
Image
.
memory
(
_imageFile
)
:
Container
(),
],
child:
Screenshot
(
controller:
screenshotController
,
child:
Text
(
"HEllo"
),
),
),
),
...
...
example/pubspec.yaml
浏览文件 @
d03e0095
...
...
@@ -14,24 +14,19 @@ environment:
# sdk: '>=2.12.0-259.8.beta <3.0.0'
dependencies
:
image_gallery_saver
:
^1.1.0
screenshot
:
path
:
../
flutter
:
sdk
:
flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
screenshot
:
path
:
../
cupertino_icons
:
^0.1.2
image_gallery_saver
:
^1.1.0
dev_dependencies
:
flutter_test
:
sdk
:
flutter
# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec
# The following section is specific to Flutter.
flutter
:
...
...
@@ -39,18 +34,14 @@ flutter:
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design
:
true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.io/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.io/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
...
...
lib/screenshot.dart
浏览文件 @
d03e0095
...
...
@@ -10,6 +10,11 @@ import 'package:flutter/widgets.dart';
// import 'package:path_provider/path_provider.dart';
import
'dart:ui'
as
ui
;
///
///
///Cannot capture Platformview due to issue https://github.com/flutter/flutter/issues/25306
///
///
class
ScreenshotController
{
GlobalKey
_containerKey
;
ScreenshotController
()
{
...
...
@@ -19,58 +24,56 @@ class ScreenshotController {
Future
<
Uint8List
>
capture
({
String
path
=
""
,
double
pixelRatio
,
Duration
delay:
const
Duration
(
milliseconds:
20
)
Duration
delay:
const
Duration
(
milliseconds:
20
)
,
})
{
//Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
return
new
Future
.
delayed
(
delay
,
()
async
{
try
{
RenderRepaintBoundary
boundary
=
this
.
_containerKey
.
currentContext
.
findRenderObject
()
as
RenderRepaintBoundary
;
pixelRatio
=
pixelRatio
??
MediaQuery
.
of
(
_containerKey
.
currentContext
).
devicePixelRatio
;
ui
.
Image
image
=
await
boundary
.
toImage
(
pixelRatio:
pixelRatio
);
ui
.
Image
image
=
await
captureAsUiImage
(
delay:
Duration
.
zero
,
pixelRatio:
pixelRatio
,
);
ByteData
byteData
=
await
image
.
toByteData
(
format:
ui
.
ImageByteFormat
.
png
);
Uint8List
pngBytes
=
byteData
.
buffer
.
asUint8List
();
// if (path == "") {
// final directory = (await getApplicationDocumentsDirectory()).path;
// String fileName = DateTime.now().toIso8601String();
// path = '$directory/$fileName.png';
// }
// File imgFile = new File(path);
// await imgFile.writeAsBytes(pngBytes).then((onValue) {});
return
pngBytes
;
}
catch
(
Exception
)
{
throw
(
Exception
);
}
});
}
Future
<
ui
.
Image
>
captureAsUiImage
({
double
pixelRatio:
1
,
Duration
delay:
const
Duration
(
milliseconds:
20
)
})
{
Future
<
ui
.
Image
>
captureAsUiImage
(
{
double
pixelRatio:
1
,
Duration
delay:
const
Duration
(
milliseconds:
20
)})
{
//Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
return
new
Future
.
delayed
(
delay
,
()
async
{
try
{
RenderRepaintBoundary
boundary
=
this
.
_containerKey
.
currentContext
.
findRenderObject
();
return
await
boundary
.
toImage
(
pixelRatio:
pixelRatio
);
RenderRepaintBoundary
boundary
=
this
.
_containerKey
.
currentContext
.
findRenderObject
()
as
RenderRepaintBoundary
;
pixelRatio
=
pixelRatio
??
MediaQuery
.
of
(
_containerKey
.
currentContext
).
devicePixelRatio
;
ui
.
Image
image
=
await
boundary
.
toImage
(
pixelRatio:
pixelRatio
);
return
image
;
}
catch
(
Exception
)
{
throw
(
Exception
);
}
});
}
}
class
Screenshot
<
T
>
extends
StatefulWidget
{
final
Widget
child
;
final
ScreenshotController
controller
;
final
GlobalKey
containerKey
;
const
Screenshot
({
Key
key
,
this
.
child
,
this
.
controller
,
this
.
containerKey
})
:
super
(
key:
key
);
const
Screenshot
({
Key
key
,
this
.
child
,
this
.
controller
,
})
:
super
(
key:
key
);
@override
State
<
Screenshot
>
createState
()
{
return
new
ScreenshotState
();
...
...
@@ -89,21 +92,21 @@ class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin {
_controller
=
widget
.
controller
;
}
@override
void
didUpdateWidget
(
Screenshot
oldWidget
)
{
super
.
didUpdateWidget
(
oldWidget
);
//
@override
//
void didUpdateWidget(Screenshot oldWidget) {
// //
super.didUpdateWidget(oldWidget);
if
(
widget
.
controller
!=
oldWidget
.
controller
)
{
widget
.
controller
.
_containerKey
=
oldWidget
.
controller
.
_containerKey
;
if
(
oldWidget
.
controller
!=
null
&&
widget
.
controller
==
null
)
_controller
.
_containerKey
=
oldWidget
.
controller
.
_containerKey
;
if
(
widget
.
controller
!=
null
)
{
if
(
oldWidget
.
controller
==
null
)
{
_controller
=
null
;
}
}
}
}
// //
if (widget.controller != oldWidget.controller) {
// //
widget.controller._containerKey = oldWidget.controller._containerKey;
// //
if (oldWidget.controller != null && widget.controller == null)
// //
_controller._containerKey = oldWidget.controller._containerKey;
// //
if (widget.controller != null) {
// //
if (oldWidget.controller == null) {
// //
_controller = null;
// //
}
// //
}
// //
}
//
}
@override
Widget
build
(
BuildContext
context
)
{
...
...
@@ -113,3 +116,4 @@ class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin {
);
}
}
pubspec.yaml
浏览文件 @
d03e0095
name
:
screenshot
description
:
Flutter Screenshot Package (Runtime). Capture any Widget as an image.
version
:
0.
2
.0
version
:
0.
3
.0
homepage
:
https://github.com/SachinGanesh/screenshot
environment
:
sdk
:
'
>=2.8.0
<3.0.0'
# sdk: '>=2.12.0-259.8.beta <3.0.0'
analyzer
:
enable-experiment
:
-
non-nullable
#
analyzer:
#
enable-experiment:
#
- non-nullable
dependencies
:
path_provider
:
^1.1.0
flutter
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论