Вы можете скопировать и вставить полный код ниже
Я предоставил base64 для изображения, чтобы доказать, что он работает правильно
Вы можете использовать RepaintBoundary
, чтобы обернуть QRView
и получить Widget
Изображение и вернуть 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;
}
}