从 StatefulWidget 获取图片并通过分享对话框分享
Get image from StatefulWidget and share it through sharing dialog
在我的项目中,我有 wc_flutter_share library for sharing content. I also have qr_flutter 用于生成二维码。
在QRView中是一个StatefulWidget。我可以通过我正在使用的库共享 QRCode 图像吗,因为在示例中它们仅显示从光盘共享图像:
final ByteData bytes = await rootBundle.load('assets/wisecrab.png');
await WcFlutterShare.share(
sharePopupTitle: 'share',
fileName: 'share.png',
mimeType: 'image/png',
bytesOfFile: bytes.buffer.asUint8List());
主要问题是从库提供的 StatefulWidget 获取图像。
您可以复制粘贴运行下面的完整代码
我已经为图像提供 base64 以证明它可以正常工作
您可以使用 RepaintBoundary
包装 QRView
并获得 Widget
图像和 return Uint8List
代码片段
RepaintBoundary(key: _globalKey, child: QRView())
...
Future<Uint8List> _capturePng() async {
try {
print('inside');
RenderRepaintBoundary boundary =
_globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
var pngBytes = byteData.buffer.asUint8List();
var bs64 = base64Encode(pngBytes);
print(pngBytes);
print(bs64);
setState(() {});
return pngBytes;
} catch (e) {
print(e);
}
}
工作演示
完整代码
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:flutter/rendering.dart';
import 'package:wc_flutter_share/wc_flutter_share.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
GlobalKey _globalKey = new GlobalKey();
Future<Uint8List> _capturePng() async {
try {
print('inside');
RenderRepaintBoundary boundary =
_globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
var pngBytes = byteData.buffer.asUint8List();
var bs64 = base64Encode(pngBytes);
print(pngBytes);
print(bs64);
setState(() {});
return pngBytes;
} catch (e) {
print(e);
}
}
void _incrementCounter() async {
var imageUint8List = await _capturePng();
await WcFlutterShare.share(
sharePopupTitle: 'share',
fileName: 'share.png',
mimeType: 'image/png',
bytesOfFile: imageUint8List);
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(child: RepaintBoundary(key: _globalKey, child: QRView())),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class QRView extends StatefulWidget {
@override
_QRViewState createState() => _QRViewState();
}
class _QRViewState extends State<QRView> {
@override
Widget build(BuildContext context) {
final message =
// ignore: lines_longer_than_80_chars
'Hey this is a QR code. Change this value in the main_screen.dart file.';
final qrFutureBuilder = FutureBuilder(
future: _loadOverlayImage(),
builder: (ctx, snapshot) {
final size = 280.0;
if (!snapshot.hasData) {
return Container(width: size, height: size);
}
return CustomPaint(
size: Size.square(size),
painter: QrPainter(
data: message,
version: QrVersions.auto,
color: Color(0xff1a5441),
emptyColor: Color(0xffeafcf6),
// size: 320.0,
embeddedImage: snapshot.data,
embeddedImageStyle: QrEmbeddedImageStyle(
size: Size.square(60),
),
),
);
},
);
return Material(
color: Colors.white,
child: SafeArea(
top: true,
bottom: true,
child: Container(
child: Column(
children: <Widget>[
Expanded(
child: Center(
child: Container(
width: 280,
child: qrFutureBuilder,
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 20, horizontal: 40)
.copyWith(bottom: 40),
child: Text(message),
),
],
),
),
),
);
}
Future<ui.Image> _loadOverlayImage() async {
final completer = Completer<ui.Image>();
final byteData = await rootBundle.load('assets/images/logo_yakka.png');
ui.decodeImageFromList(byteData.buffer.asUint8List(), completer.complete);
return completer.future;
}
}
在我的项目中,我有 wc_flutter_share library for sharing content. I also have qr_flutter 用于生成二维码。
在QRView中是一个StatefulWidget。我可以通过我正在使用的库共享 QRCode 图像吗,因为在示例中它们仅显示从光盘共享图像:
final ByteData bytes = await rootBundle.load('assets/wisecrab.png');
await WcFlutterShare.share(
sharePopupTitle: 'share',
fileName: 'share.png',
mimeType: 'image/png',
bytesOfFile: bytes.buffer.asUint8List());
主要问题是从库提供的 StatefulWidget 获取图像。
您可以复制粘贴运行下面的完整代码
我已经为图像提供 base64 以证明它可以正常工作
您可以使用 RepaintBoundary
包装 QRView
并获得 Widget
图像和 return Uint8List
代码片段
RepaintBoundary(key: _globalKey, child: QRView())
...
Future<Uint8List> _capturePng() async {
try {
print('inside');
RenderRepaintBoundary boundary =
_globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
var pngBytes = byteData.buffer.asUint8List();
var bs64 = base64Encode(pngBytes);
print(pngBytes);
print(bs64);
setState(() {});
return pngBytes;
} catch (e) {
print(e);
}
}
工作演示
完整代码
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:flutter/rendering.dart';
import 'package:wc_flutter_share/wc_flutter_share.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
GlobalKey _globalKey = new GlobalKey();
Future<Uint8List> _capturePng() async {
try {
print('inside');
RenderRepaintBoundary boundary =
_globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
var pngBytes = byteData.buffer.asUint8List();
var bs64 = base64Encode(pngBytes);
print(pngBytes);
print(bs64);
setState(() {});
return pngBytes;
} catch (e) {
print(e);
}
}
void _incrementCounter() async {
var imageUint8List = await _capturePng();
await WcFlutterShare.share(
sharePopupTitle: 'share',
fileName: 'share.png',
mimeType: 'image/png',
bytesOfFile: imageUint8List);
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(child: RepaintBoundary(key: _globalKey, child: QRView())),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class QRView extends StatefulWidget {
@override
_QRViewState createState() => _QRViewState();
}
class _QRViewState extends State<QRView> {
@override
Widget build(BuildContext context) {
final message =
// ignore: lines_longer_than_80_chars
'Hey this is a QR code. Change this value in the main_screen.dart file.';
final qrFutureBuilder = FutureBuilder(
future: _loadOverlayImage(),
builder: (ctx, snapshot) {
final size = 280.0;
if (!snapshot.hasData) {
return Container(width: size, height: size);
}
return CustomPaint(
size: Size.square(size),
painter: QrPainter(
data: message,
version: QrVersions.auto,
color: Color(0xff1a5441),
emptyColor: Color(0xffeafcf6),
// size: 320.0,
embeddedImage: snapshot.data,
embeddedImageStyle: QrEmbeddedImageStyle(
size: Size.square(60),
),
),
);
},
);
return Material(
color: Colors.white,
child: SafeArea(
top: true,
bottom: true,
child: Container(
child: Column(
children: <Widget>[
Expanded(
child: Center(
child: Container(
width: 280,
child: qrFutureBuilder,
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 20, horizontal: 40)
.copyWith(bottom: 40),
child: Text(message),
),
],
),
),
),
);
}
Future<ui.Image> _loadOverlayImage() async {
final completer = Completer<ui.Image>();
final byteData = await rootBundle.load('assets/images/logo_yakka.png');
ui.decodeImageFromList(byteData.buffer.asUint8List(), completer.complete);
return completer.future;
}
}