Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
S
screenshot
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
openSourceLibrary
screenshot
Commits
de95fbdc
提交
de95fbdc
authored
2月 10, 2021
作者:
ritheshSalyan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support nullsafety
上级
be886c89
隐藏空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
58 行增加
和
51 行删除
+58
-51
main.dart
example/lib/main.dart
+18
-16
pubspec.yaml
example/pubspec.yaml
+2
-2
screenshot.dart
lib/screenshot.dart
+32
-27
file_manager.dart
lib/src/platform_specific/file_manager/file_manager.dart
+1
-1
file_manager_io.dart
lib/src/platform_specific/file_manager/file_manager_io.dart
+1
-1
non_io.dart
lib/src/platform_specific/file_manager/non_io.dart
+1
-1
pubspec.lock
pubspec.lock
+1
-1
pubspec.yaml
pubspec.yaml
+2
-2
没有找到文件。
example/lib/main.dart
浏览文件 @
de95fbdc
...
...
@@ -31,7 +31,7 @@ class MyApp extends StatelessWidget {
}
class
MyHomePage
extends
StatefulWidget
{
MyHomePage
({
Key
key
,
this
.
title
})
:
super
(
key:
key
);
MyHomePage
({
Key
?
key
,
this
.
title
=
""
})
:
super
(
key:
key
);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
...
...
@@ -50,7 +50,7 @@ class MyHomePage extends StatefulWidget {
class
_MyHomePageState
extends
State
<
MyHomePage
>
{
int
_counter
=
0
;
Uint8List
_imageFile
;
Uint8List
?
_imageFile
;
//Create an instance of ScreenshotController
ScreenshotController
screenshotController
=
ScreenshotController
();
...
...
@@ -100,22 +100,24 @@ class _MyHomePageState extends State<MyHomePage> {
_imageFile
=
null
;
screenshotController
.
capture
(
delay:
Duration
(
milliseconds:
10
))
.
then
((
Uint8List
image
)
async
{
.
then
((
Uint8List
?
image
)
async
{
_imageFile
=
image
;
showDialog
(
context:
context
,
builder:
(
context
)
=>
Scaffold
(
appBar:
AppBar
(
title:
Text
(
"CAPURED SCREENSHOT"
),
if
(
image
!=
null
)
{
showDialog
(
context:
context
,
builder:
(
context
)
=>
Scaffold
(
appBar:
AppBar
(
title:
Text
(
"CAPURED SCREENSHOT"
),
),
body:
Center
(
child:
Column
(
children:
[
if
(
image
!=
null
)
Image
.
memory
(
image
),
],
)),
),
body:
Center
(
child:
Column
(
children:
[
_imageFile
!=
null
?
Image
.
memory
(
_imageFile
)
:
Container
(),
],
)),
),
);
);
}
}).
catchError
((
onError
)
{
print
(
onError
);
});
...
...
example/pubspec.yaml
浏览文件 @
de95fbdc
...
...
@@ -10,8 +10,8 @@ description: A new Flutter project.
version
:
1.0.0+1
environment
:
sdk
:
'
>=2.8.0
<3.0.0'
#
sdk: '>=2.12.0-259.8.beta <3.0.0'
#
sdk: '>=2.8.0 <3.0.0'
sdk
:
'
>=2.12.0-259.8.beta
<3.0.0'
dependencies
:
flutter
:
...
...
lib/screenshot.dart
浏览文件 @
de95fbdc
...
...
@@ -16,42 +16,44 @@ import 'dart:ui' as ui;
///
///
class
ScreenshotController
{
GlobalKey
_containerKey
;
late
GlobalKey
_containerKey
;
ScreenshotController
()
{
_containerKey
=
GlobalKey
();
}
/// Captures image and saves to given path
Future
<
String
>
captureAndSave
(
Future
<
String
?
>
captureAndSave
(
String
directory
,
{
String
fileName
,
double
pixelRatio
,
Duration
delay
:
const
Duration
(
milliseconds:
20
),
String
?
fileName
,
double
?
pixelRatio
,
Duration
delay
=
const
Duration
(
milliseconds:
20
),
})
async
{
Uint8List
content
=
await
capture
(
Uint8List
?
content
=
await
capture
(
pixelRatio:
pixelRatio
,
delay:
delay
,
);
if
(
content
!=
null
)
{
PlatformFileManager
fileManager
=
PlatformFileManager
();
PlatformFileManager
fileManager
=
PlatformFileManager
(
);
return
fileManager
.
saveFile
(
content
,
directory
,
name:
fileName
)
;
return
fileManager
.
saveFile
(
content
,
directory
,
name:
fileName
);
}
return
null
;
}
Future
<
Uint8List
>
capture
({
double
pixelRatio
,
Duration
delay:
const
Duration
(
milliseconds:
20
),
Future
<
Uint8List
?
>
capture
({
double
?
pixelRatio
,
Duration
?
delay
=
const
Duration
(
milliseconds:
20
),
})
{
//Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
return
new
Future
.
delayed
(
delay
,
()
async
{
return
new
Future
.
delayed
(
delay
??
Duration
(
milliseconds:
20
)
,
()
async
{
try
{
ui
.
Image
image
=
await
captureAsUiImage
(
delay:
Duration
.
zero
,
pixelRatio:
pixelRatio
,
);
ByteData
byteData
=
ByteData
?
byteData
=
await
image
.
toByteData
(
format:
ui
.
ImageByteFormat
.
png
);
Uint8List
pngBytes
=
byteData
.
buffer
.
asUint8List
();
Uint8List
?
pngBytes
=
byteData
?
.
buffer
.
asUint8List
();
return
pngBytes
;
}
catch
(
Exception
)
{
...
...
@@ -61,18 +63,20 @@ class ScreenshotController {
}
Future
<
ui
.
Image
>
captureAsUiImage
(
{
double
pixelRatio:
1
,
Duration
delay:
const
Duration
(
milliseconds:
20
)})
{
{
double
?
pixelRatio
,
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
);
?.
findRenderObject
()
as
RenderRepaintBoundary
;
BuildContext
?
context
=
_containerKey
.
currentContext
;
if
(
pixelRatio
==
null
)
{
if
(
context
!=
null
)
pixelRatio
=
pixelRatio
??
MediaQuery
.
of
(
context
).
devicePixelRatio
;
}
ui
.
Image
image
=
await
boundary
.
toImage
(
pixelRatio:
pixelRatio
??
1
);
return
image
;
}
catch
(
Exception
)
{
throw
(
Exception
);
...
...
@@ -85,9 +89,9 @@ class Screenshot<T> extends StatefulWidget {
final
Widget
child
;
final
ScreenshotController
controller
;
const
Screenshot
({
Key
key
,
this
.
child
,
this
.
controller
,
Key
?
key
,
required
this
.
child
,
required
this
.
controller
,
})
:
super
(
key:
key
);
@override
...
...
@@ -97,14 +101,15 @@ class Screenshot<T> extends StatefulWidget {
}
class
ScreenshotState
extends
State
<
Screenshot
>
with
TickerProviderStateMixin
{
late
ScreenshotController
_controller
;
@override
void
initState
()
{
super
.
initState
();
if
(
widget
.
controller
==
null
)
{
_controller
=
ScreenshotController
();
}
else
//
if (widget.controller == null) {
//
_controller = ScreenshotController();
//
} else
_controller
=
widget
.
controller
;
}
...
...
lib/src/platform_specific/file_manager/file_manager.dart
浏览文件 @
de95fbdc
...
...
@@ -7,5 +7,5 @@ import 'file_manager_stub.dart'
abstract
class
PlatformFileManager
{
factory
PlatformFileManager
()
=>
getFileManager
();
Future
<
String
>
saveFile
(
Uint8List
fileContent
,
String
path
,
{
String
name
});
Future
<
String
>
saveFile
(
Uint8List
fileContent
,
String
path
,
{
String
?
name
});
}
lib/src/platform_specific/file_manager/file_manager_io.dart
浏览文件 @
de95fbdc
...
...
@@ -8,7 +8,7 @@ PlatformFileManager getFileManager() => PlatformFilePickerWindows();
class
PlatformFilePickerWindows
implements
PlatformFileManager
{
@override
Future
<
String
>
saveFile
(
Uint8List
fileContent
,
String
path
,
{
String
name
})
async
{
{
String
?
name
})
async
{
name
=
name
??
"
${DateTime.now().microsecondsSinceEpoch}
.png"
;
File
file
=
await
File
(
"
$path
/
$name
"
).
create
(
recursive:
true
);
file
.
writeAsBytesSync
(
fileContent
);
...
...
lib/src/platform_specific/file_manager/non_io.dart
浏览文件 @
de95fbdc
...
...
@@ -8,7 +8,7 @@ PlatformFileManager getFileManager() => PlatformFileManagerWeb();
class
PlatformFileManagerWeb
implements
PlatformFileManager
{
@override
Future
<
String
>
saveFile
(
Uint8List
fileContent
,
String
path
,
{
String
name
})
async
{
{
String
?
name
})
async
{
throw
UnsupportedError
(
"File cannot be saved in current platform"
);
// name = name ?? "${DateTime.now().microsecondsSinceEpoch}.png";
// File file = await File("$path/$name").create(recursive: true);
...
...
pubspec.lock
浏览文件 @
de95fbdc
...
...
@@ -143,4 +143,4 @@ packages:
source: hosted
version: "2.1.0-nullsafety.5"
sdks:
dart: ">=2.12.0-
0.0
<3.0.0"
dart: ">=2.12.0-
259.8.beta
<3.0.0"
pubspec.yaml
浏览文件 @
de95fbdc
...
...
@@ -4,8 +4,8 @@ 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'
#
sdk: '>=2.8.0 <3.0.0'
sdk
:
'
>=2.12.0-259.8.beta
<3.0.0'
# analyzer:
# enable-experiment:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论