Flutter社交分享插件vocsy_esys_flutter_share的使用

发布于 1周前 作者 sinazl 来自 Flutter

Flutter社交分享插件vocsy_esys_flutter_share的使用

vocsy_esys_flutter_share 是一个用于在Flutter应用中与其他应用程序共享文件和文本的插件。该插件是从 esys_flutter_share 分叉而来,并支持空安全(null-safety)。

重要注意事项

iOS项目配置

如果你正在创建一个新的Flutter应用,请确保使用以下命令创建项目:

flutter create -i swift

否则,你可能会遇到如下错误信息:

The "Swift Language Version" (SWIFT_VERSION) build setting must be set to a supported value for targets which use Swift. Supported values are: 3.0, 4.0, 4.2.

如果遇到上述问题,可以尝试修改iOS项目的 Podfile 文件:

target 'Runner' do
  use_frameworks!  # required by simple_permission
  ...
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_VERSION'] = '4.0'  # required by simple_permission
      config.build_settings['ENABLE_BITCODE'] = 'NO'
    end
  end
end

功能特性

名称 Android iOS
共享文本
共享图片与文本 [资产/网络]
共享多个图片
共享多个图片及CSV文件
共享CSV文件
共享来自URL的图片

示例截图

Screenshot 1 Screenshot 2

使用方法

导入包

import 'package:vocsy_esys_flutter_share/vocsy_esys_flutter_share.dart';

示例代码

下面是一个完整的示例demo,展示了如何使用 vocsy_esys_flutter_share 插件进行各种类型的分享:

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:vocsy_esys_flutter_share/vocsy_esys_flutter_share.dart';

void main() => runApp(
  MaterialApp(
    debugShowCheckedModeBanner: false,
    home: MyHomePage(),
  ),
);

class MyHomePage extends StatefulWidget {
  MyHomePage();

  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Vocsy Esys Share Plugin Sample'),
      ),
      body: Center(
        child: IntrinsicWidth(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                child: Text('Share text'),
                onPressed: () async => await _shareText(),
              ),
              ElevatedButton(
                child: Text('Share image'),
                onPressed: () async => await _shareImage(),
              ),
              ElevatedButton(
                child: Text('Share images'),
                onPressed: () async => await _shareImages(),
              ),
              ElevatedButton(
                child: Text('Share CSV'),
                onPressed: () async => await _shareCSV(),
              ),
              ElevatedButton(
                child: Text('Share mixed'),
                onPressed: () async => await _shareMixed(),
              ),
              ElevatedButton(
                child: Text('Share image from url'),
                onPressed: () async => await _shareImageFromUrl(),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> _shareText() async {
    try {
      VocsyShare.text('my text title', 'This is my text to share with other applications.', 'text/plain');
    } catch (e) {
      print('error: $e');
    }
  }

  Future<void> _shareImage() async {
    try {
      final ByteData bytes = await rootBundle.load('assets/image1.png');
      await VocsyShare.file('esys image', 'esys.png', bytes.buffer.asUint8List(), 'image/png', text: 'My optional text.');
    } catch (e) {
      print('error: $e');
    }
  }

  Future<void> _shareImages() async {
    try {
      final ByteData bytes1 = await rootBundle.load('assets/image1.png');
      final ByteData bytes2 = await rootBundle.load('assets/image2.png');

      await VocsyShare.files(
        'esys images',
        {
          'esys.png': bytes1.buffer.asUint8List(),
          'bluedan.png': bytes2.buffer.asUint8List(),
        },
        'image/png',
      );
    } catch (e) {
      print('error: $e');
    }
  }

  Future<void> _shareCSV() async {
    try {
      final ByteData bytes = await rootBundle.load('assets/addresses.csv');
      await VocsyShare.file('addresses', 'addresses.csv', bytes.buffer.asUint8List(), 'text/csv');
    } catch (e) {
      print('error: $e');
    }
  }

  Future<void> _shareMixed() async {
    try {
      final ByteData bytes1 = await rootBundle.load('assets/image1.png');
      final ByteData bytes2 = await rootBundle.load('assets/image2.png');
      final ByteData bytes3 = await rootBundle.load('assets/addresses.csv');

      await VocsyShare.files(
        'esys images',
        {
          'esys.png': bytes1.buffer.asUint8List(),
          'bluedan.png': bytes2.buffer.asUint8List(),
          'addresses.csv': bytes3.buffer.asUint8List(),
        },
        '*/*',
        text: 'My optional text.',
      );
    } catch (e) {
      print('error: $e');
    }
  }

  Future<void> _shareImageFromUrl() async {
    try {
      var request = await HttpClient().getUrl(Uri.parse('https://cdn.shopware.store/r/4/9/Q2AEt/thumbnail/5d/d6/aa/1612969110/leiterplatte_800x800.png'));
      var response = await request.close();
      Uint8List bytes = await consolidateHttpClientResponseBytes(response);
      await VocsyShare.file('ESYS AMLOG', 'amlog.jpg', bytes, 'image/jpg');
    } catch (e) {
      print('error: $e');
    }
  }
}

更多关于Flutter社交分享插件vocsy_esys_flutter_share的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter社交分享插件vocsy_esys_flutter_share的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用vocsy_esys_flutter_share插件来实现社交分享的示例代码。这个插件允许你将内容分享到多个社交平台,如Facebook、Twitter、LinkedIn等。

首先,你需要在你的pubspec.yaml文件中添加vocsy_esys_flutter_share依赖:

dependencies:
  flutter:
    sdk: flutter
  vocsy_esys_flutter_share: ^x.y.z  # 请将x.y.z替换为最新的版本号

然后运行flutter pub get来安装依赖。

接下来,你需要在你的Dart文件中导入插件并配置分享功能。以下是一个简单的示例:

import 'package:flutter/material.dart';
import 'package:vocsy_esys_flutter_share/vocsy_esys_flutter_share.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Social Share Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ShareScreen(),
    );
  }
}

class ShareScreen extends StatelessWidget {
  final Share share = Share.instance;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Social Share Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Click the button to share!',
              style: TextStyle(fontSize: 24),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () => _shareContent(),
              child: Text('Share'),
            ),
          ],
        ),
      ),
    );
  }

  void _shareContent() async {
    try {
      final ShareResult result = await share.share(
        title: 'Flutter Social Share',
        text: 'Hello, I am sharing this content using Flutter!',
        imageUrl: 'https://example.com/image.jpg', // 替换为你要分享的图片URL
        link: 'https://flutter.dev', // 替换为你要分享的链接
      );

      if (result.wasPosted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Content shared successfully!')),
        );
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Failed to share content.')),
        );
      }
    } catch (error) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error: $error')),
      );
    }
  }
}

在这个示例中:

  1. 我们创建了一个简单的Flutter应用,其中包含一个按钮。
  2. 当用户点击按钮时,将调用_shareContent方法。
  3. share.share方法接受多个参数,如titletextimageUrllink,这些参数将用于分享的内容。
  4. 分享结果将通过ShareResult对象返回,你可以根据wasPosted属性来判断分享是否成功。

注意:

  • 你需要确保设备已安装并配置了相关的社交应用(如Facebook、Twitter等),否则分享功能可能无法正常工作。
  • 某些社交平台可能要求你进行额外的配置或认证,以便成功分享内容。

希望这个示例能帮助你快速上手vocsy_esys_flutter_share插件!

回到顶部