[英]Flutter web sharing image works on windows but not on android/ios
I am trying to generate a qr and share it as png.我正在尝试生成一个 qr 并将其共享为 png。 This code works fine for windows but won't work (nothing happens) on ios(safari) and and android(chrome/samsung browser).
此代码适用于 windows 但在 ios(safari)和 android(chrome/samsung 浏览器)上不起作用(没有任何反应)。 I'm a begginer in app/web development and have no idea what may be causing this.
我是应用程序/网络开发的初学者,不知道是什么原因造成的。 I have tried to google an answer in a thousand different ways but found nothing.
我试图以一千种不同的方式搜索答案,但一无所获。
import 'package:flutter/material.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui';
import 'dart:io';
import 'package:flutter/rendering.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
import 'package:encrypt/encrypt.dart' as enc;
import 'package:firebase_auth/firebase_auth.dart';
import 'package:http/http.dart';
import 'package:universal_html/html.dart' as html;
class GenerateScreen extends StatefulWidget {
const GenerateScreen({Key? key, @required this.guestName}) : super(key: key);
final String? guestName;
@override
State<StatefulWidget> createState() => GenerateScreenState();
}
class GenerateScreenState extends State<GenerateScreen> {
GlobalKey globalKey = GlobalKey();
List qrFile = [];
String encryptedText() {
final String toEncrypt =
FirebaseAuth.instance.currentUser!.uid + '!' + widget.guestName!;
print(toEncrypt);
final key = enc.Key.fromUtf8('Ijh35hYjabf86H7k8jWgETR264Nhflo9');
final iv = enc.IV.fromLength(16);
final encrypter = enc.Encrypter(enc.AES(key));
final encrypted = encrypter.encrypt(toEncrypt, iv: iv);
return encrypted.base16;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _contentWidget(),
);
}
Future _captureAndSharePng() async {
RenderRepaintBoundary boundary =
globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
var image = await boundary.toImage();
ByteData? byteData = await image.toByteData(format: ImageByteFormat.png);
Uint8List pngBytes = byteData!.buffer.asUint8List();
qrFile = [
html.File([pngBytes], "qr.png", {"type": "image/png"})
];
await html.window.navigator.share({"files": qrFile, "title": "QR"});
}
_contentWidget() {
final bodyHeight = MediaQuery.of(context).size.height -
MediaQuery.of(context).viewInsets.bottom;
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(bottom: 100),
child: TextButton(
onPressed: () {
_captureAndSharePng();
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [Text('Compartir QR'), Icon(Icons.share)],
)),
),
Center(
child: RepaintBoundary(
key: globalKey,
child: Container(
color: Colors.white,
child: QrImage(
backgroundColor: Colors.white,
data: encryptedText(),
size: 0.3 * bodyHeight,
),
),
),
),
],
);
}
} ```
html.window.navigator.share is only for the web you have to code with conditions for android and ios or you can use https://pub.dev/packages/share_plus . html.window.navigator.share is only for the web you have to code with conditions for android and ios or you can use https://pub.dev/packages/share_plus . it supports all the platforms.
它支持所有平台。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.