【Flutter】share_plusで共有する

Flutter

コンテンツを他のアプリやサービスと共有するためのプラグイン。
テキスト、ファイル、URLなどを簡単に共有できる。

share_plus | Flutter package
Flutter plugin for sharing content via the platform share UI, using the ACTION_SEND intent on Android and UIActivityView...

使い方

テキストを共有

Share.share('check out my website https://example.com');

オプションで、共有時に使用される subject を指定可能。

Share.share('check out my website https://example.com', subject: 'Look what I made!');

共有結果の確認

final result = await Share.share('check out my website https://example.com');

if (result.status == ShareResultStatus.success) {
    print('Thank you for sharing my website!');
}

ファイルを共有

final result = await Share.shareXFiles([XFile('${directory.path}/image.jpg')], text: 'Great picture');

Webでの使用

Share.shareXFiles([XFile('assets/hello.txt')], text: 'Great picture');

URIの共有

Share.shareUri(uri: uri);

iPadでの共有

iPadでの共有には sharePositionOrigin パラメータを指定する必要があります。これを指定しないと、アプリがクラッシュする可能性があります。

Builder(
  builder: (BuildContext context) {
    return ElevatedButton(
      onPressed: () => _onShare(context),
      child: const Text('Share'),
    );
  },
),

// _onShareメソッド:
final box = context.findRenderObject() as RenderBox?;

await Share.share(
  'check out my website https://example.com',
  subject: 'Look what I made!',
  sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
);
タイトルとURLをコピーしました