import 'dart:io'; import 'dart:typed_data'; import 'dart:ui'; import 'package:aiplot/common/app_util.dart'; import 'package:aiplot/network/NetworkConfig.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:path_provider/path_provider.dart'; import 'package:share_plus/share_plus.dart'; import '../generated/l10n.dart'; ///分享邀请弹框 class ShareDialog extends StatefulWidget { String imgUrl; String userName; ShareDialog(this.imgUrl, this.userName, {Key? key}) : super(key: key); @override State createState() => _ShareDialogState(); } class _ShareDialogState extends State { final _boundaryKey = GlobalKey(); @override Widget build(BuildContext context) { final size = MediaQuery.of(context).size; final l39 = size.width / 9.2307692307692; final w74 = size.width / 4.86486486486; final w66 = size.width / 5.454545454545; final w60 = size.width / 6; final w50 = size.width / 7.2; final w135 = size.width / 2.666666666666; final h37 = size.width / 9.729729729729; return Material( type: MaterialType.transparency, //透明类型 color: const Color(0x00000000), child: Container( alignment: Alignment.center, child: Column( mainAxisSize: MainAxisSize.min, children: [ Container( alignment: Alignment.centerLeft, margin: EdgeInsets.only(left: l39, right: l39), child: Text( S.of(context).Invite_new, style: TextStyle(color: Colors.white), ), ), ///分享的主体 Container( margin: EdgeInsets.only(left: l39, right: l39, top: 10), child: RepaintBoundary( key: _boundaryKey, child: Container( width: size.width, color: Colors.white, child: Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.end, children: [ Container( alignment: Alignment.centerLeft, margin: EdgeInsets.only(left: 12, top: 13), child: Image(width: w74, image: AssetImage("assets/images/share_logo.png")), ), Container( margin: EdgeInsets.only(right: 12), child: Row( children: [ Text( "${S.of(context).Author}:", style: TextStyle(fontSize: 12, color: Color(0xFF666666)), ), SizedBox( width: w60, child: Text( "${widget.userName}", overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 12, color: Color(0xFF666666)), ), ), ], ), ) ], ), widget.imgUrl != "" ? Container( alignment: Alignment.centerLeft, margin: EdgeInsets.only(left: 12, top: 12, right: 12), child: CachedNetworkImage(imageUrl: widget.imgUrl), ) : Container(), SizedBox( width: double.infinity, height: w66, child: Stack( children: [ Positioned( left: 12, top: 14, child: Text( "${S.of(context).New_users} ${NetworkConfig.appConfigBean!.InvitationCodeNumber} ${S.of(context).Painting_points}", style: TextStyle(fontSize: 13), )), Positioned( left: 12, bottom: 12, child: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ Container( child: Text( "${S.of(context).Invitation_code} :", style: TextStyle(fontSize: 10, color: Color(0xFF6A6A6A)), ), ), Container( margin: EdgeInsets.only(left: 9), child: Text( NetworkConfig.invitationCode!, style: TextStyle(fontSize: 16, color: Color(0xFF853FFF)), ), ), ], )), Positioned( right: 12, top: 10, child: Container( width: w50, height: w50, color: Color(0xFFEEEEFF), child: CachedNetworkImage( fit: BoxFit.fitHeight, imageUrl: NetworkConfig.userData!.InvitationCodeImageUrl!, ), )), ], ), ), ], ), ), ), ), Container( margin: EdgeInsets.only(left: l39, right: l39, top: 9), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ ///保存 GestureDetector( onTap: () async { EasyLoading.show(status: 'loading...'); final boundary = _boundaryKey.currentContext?.findRenderObject(); if (boundary != null && boundary is RenderRepaintBoundary) { final image = await boundary.toImage(pixelRatio: 3.0); ByteData? byteData = await image.toByteData(format: ImageByteFormat.png); EasyLoading.dismiss(); AppUtil.saveWidgetImage(byteData!); } }, child: Container( width: w135, height: h37, alignment: Alignment.center, color: Colors.white, child: Text( S.of(context).Save, style: TextStyle(color: Color(0xFF853FFF), fontSize: 16), ), ), ), ///分享 GestureDetector( onTap: () async { EasyLoading.show(status: 'loading...'); final boundary = _boundaryKey.currentContext?.findRenderObject(); if (boundary != null && boundary is RenderRepaintBoundary) { final image = await boundary.toImage(pixelRatio: 3.0); ByteData? byteData = await image.toByteData(format: ImageByteFormat.png); final xFile = await byteDataToXFile(ByteData.view(byteData!.buffer)); EasyLoading.dismiss(); await Share.shareXFiles([xFile]); } }, child: Container( width: w135, height: h37, alignment: Alignment.center, decoration: const BoxDecoration( gradient: LinearGradient( begin: Alignment.centerLeft, //渐变开始于上面的中间开始 end: Alignment.centerRight, //渐变结束于下面的中间 colors: [Color(0xFF843FFF), Color(0xFFF87DFF)]), ), child: Text( S.of(context).Share, style: TextStyle(color: Colors.white, fontSize: 16), ), ), ), ], ), ), ], ), ), ); } Future byteDataToXFile(ByteData bytes) async { Uint8List uint8List = bytes.buffer.asUint8List(); Directory tempDir = await getTemporaryDirectory(); String tempPath = tempDir.path; File tempFile = await File('$tempPath/image.jpg').writeAsBytes(uint8List); return XFile(tempFile.path); } }