flutter - Flutter - 我想在屏幕截图后立即将其显示在屏幕上而不是文件上
问题描述
通过使用 RepaintBoundary,您可以仅捕获所需区域。
我想让屏幕截图在您拍摄后立即出现在屏幕上。
解决方案
您可以将字节存储在变量中,并使用Image.memory()
.
演示:
按中间的“下载”按钮,截屏并显示。
完整源代码:
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
GlobalKey _globalKey = GlobalKey();
var _bytes;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Screenshot Example")),
body: Column(
children: [
/// Area to be captured
RepaintBoundary(
key: _globalKey,
child: Container(
width: 300,
height: 300,
decoration: BoxDecoration(
gradient: RadialGradient(
colors: [Colors.white, Colors.grey],
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FlutterLogo(),
Text("screenshot me"),
],
),
),
),
/// Button
IconButton(
icon: Icon(Icons.file_download),
onPressed: () async {
final render = (_globalKey.currentContext!.findRenderObject()
as RenderRepaintBoundary);
final imageBytes = (await (await render.toImage())
.toByteData(format: ImageByteFormat.png))!
.buffer
.asUint8List();
setState(() {
_bytes = imageBytes;
});
},
),
/// Display
if (_bytes != null) Image.memory(_bytes, width: 200),
],
),
);
}
}
推荐阅读
- c# - 找不到实体框架插入字段时出错
- java - BCFIPS - org.bouncycastle.crypto.fips.FipsOperationError:模块校验和失败:预期这得到了
- javascript - 使用 CSS-in-JS 时,我应该为 CSS 类使用哪种大小写?
- python - 如何将参数传递给 scipy.optimize 中的非线性约束?
- javascript - 从 Firestore 获取值并使用异步将输出存储为全局
- c# - Linq to SQL/Entities:最大的 N-Per 组问题/性能提升
- javascript - 限制用户在输入字段 angular 12 中输入特定值
- graphql - 使用 graphql 查询变量来搜索匹配两个不同搜索的值的正确方法是什么?
- php - 表单验证 CodeIgniter 4
- php - 如何在特色的标准(基本)模块上显示产品选项?