简体   繁体   English

Flutter web 共享图像适用于 windows 但不适用于 android/ios

[英]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.

相关问题 Flutter beta hot reload in VSCode works for web but not iOS and Android (which works in Android studio) - Flutter beta hot reload in VSCode works for web but not iOS and Android (which works in Android studio) Flutter - FutureBuilder 在 Android 上运行良好,但在 iOS 上运行不佳 - Flutter - FutureBuilder works well on Android but not well on iOS Admob广告无法在Android上显示,但可以在iOS上使用-Flutter - Admob Ads not showing on Android but works on iOS — Flutter Sqlite DB 在 Flutter Android App 中有效,但在 iOS 中无效 - Sqlite DB works in Flutter Android App but not in iOS Flutter 项目中 Android、iOS 和 Dart 之间的共享常量 - Sharing constants between Android , iOS and Dart in a Flutter Project Flutter 音频播放器在 android 上完全没有声音,但在 web 上有效 - Flutter audioplayers no sound at all on android but works on web Android 图像共享应用程序可以在我的手机上运行,但不能在其他人的手机上运行 - Android image sharing app works on my phone but not on anybody else's Firebase updateChildValues可在IOS中使用,但不适用于Web和Android - Firebase updateChildValues works in IOS but not in Web and Android 无法在Android中共享图像 - Unable sharing image in android Android图片分享应用 - Android image sharing app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM