@@ -2,8 +2,9 @@ import 'package:flutter/material.dart';
22import 'package:flutter/scheduler.dart' ;
33import 'package:flutter/services.dart' ;
44import 'package:intl/intl.dart' ;
5+ import 'package:share_plus/share_plus.dart' ;
56import 'package:video_player/video_player.dart' ;
6-
7+ import 'package:http/http.dart' as httpClient;
78import '../api/core.dart' ;
89import '../api/model/model.dart' ;
910import '../generated/l10n/zulip_localizations.dart' ;
@@ -89,6 +90,46 @@ class _CopyLinkButton extends StatelessWidget {
8990 }
9091}
9192
93+ Future <XFile > _downloadImage (Uri url, Map <String , String > headers) async {
94+ final response = await httpClient.get (url, headers: headers);
95+ final bytes = response.bodyBytes;
96+ return XFile .fromData (bytes,
97+ name: url.pathSegments.last,
98+ mimeType: response.headers['content-type' ]);
99+ }
100+
101+ class _ShareButton extends StatelessWidget {
102+ const _ShareButton ({required this .url});
103+
104+ final Uri url;
105+
106+ @override
107+ Widget build (BuildContext context) {
108+ final zulipLocalizations = ZulipLocalizations .of (context);
109+ return IconButton (
110+ tooltip: zulipLocalizations.lightboxShareImageTooltip,
111+ icon: const Icon (Icons .share),
112+ onPressed: () async {
113+ try {
114+ final store = PerAccountStoreWidget .of (context);
115+ final headers = {
116+ if (url.origin == store.account.realmUrl.origin)
117+ ...authHeader (email: store.account.email, apiKey: store.account.apiKey),
118+ ...userAgentHeader ()
119+ };
120+ final xFile = await _downloadImage (url, headers);
121+ await Share .shareXFiles ([xFile]);
122+ } catch (error) {
123+ if (! context.mounted) return ;
124+ showErrorDialog (
125+ context: context,
126+ title: zulipLocalizations.errorDialogTitle,
127+ message: zulipLocalizations.errorShareFailed);
128+ }
129+ });
130+ }
131+ }
132+
92133class _LightboxPageLayout extends StatefulWidget {
93134 const _LightboxPageLayout ({
94135 required this .routeEntranceAnimation,
@@ -258,7 +299,7 @@ class _ImageLightboxPageState extends State<_ImageLightboxPage> {
258299 elevation: elevation,
259300 child: Row (children: [
260301 _CopyLinkButton (url: widget.src),
261- // TODO(#43): Share image
302+ _ShareButton (url : widget.src),
262303 // TODO(#42): Download image
263304 ]),
264305 );
0 commit comments