@@ -2,8 +2,9 @@ import 'package:flutter/material.dart';
2
2
import 'package:flutter/scheduler.dart' ;
3
3
import 'package:flutter/services.dart' ;
4
4
import 'package:intl/intl.dart' ;
5
+ import 'package:share_plus/share_plus.dart' ;
5
6
import 'package:video_player/video_player.dart' ;
6
-
7
+ import 'package:http/http.dart' as http;
7
8
import '../api/core.dart' ;
8
9
import '../api/model/model.dart' ;
9
10
import '../generated/l10n/zulip_localizations.dart' ;
@@ -89,6 +90,46 @@ class _CopyLinkButton extends StatelessWidget {
89
90
}
90
91
}
91
92
93
+ Future <XFile > _downloadImage (Uri url, Map <String , String > headers) async {
94
+ final response = await http.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
+
92
133
class _LightboxPageLayout extends StatefulWidget {
93
134
const _LightboxPageLayout ({
94
135
required this .routeEntranceAnimation,
@@ -258,7 +299,7 @@ class _ImageLightboxPageState extends State<_ImageLightboxPage> {
258
299
elevation: elevation,
259
300
child: Row (children: [
260
301
_CopyLinkButton (url: widget.src),
261
- // TODO(#43): Share image
302
+ _ShareButton (url : widget.src),
262
303
// TODO(#42): Download image
263
304
]),
264
305
);
0 commit comments