social_share_kit_facebook 0.1.0 copy "social_share_kit_facebook: ^0.1.0" to clipboard
social_share_kit_facebook: ^0.1.0 copied to clipboard

Facebook SDK add-on for social_share_kit. Upgrades ShareTarget.facebook to the native Share Dialog on both platforms, with real success, cancel and error reporting.

example/lib/main.dart

import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:social_share_kit/social_share_kit.dart';
import 'package:social_share_kit_facebook/social_share_kit_facebook.dart';

void main() {
  // The one line of wiring the add-on needs. Everything after this goes
  // through the ordinary social_share_kit API.
  SocialShareKitFacebook.register();
  runApp(const ExampleApp());
}

class ExampleApp extends StatelessWidget {
  const ExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'social_share_kit_facebook',
      theme: ThemeData(colorSchemeSeed: Colors.blue, useMaterial3: true),
      darkTheme: ThemeData(
        colorSchemeSeed: Colors.blue,
        brightness: Brightness.dark,
        useMaterial3: true,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool _registered = SocialShareKitFacebook.isRegistered;
  bool? _canShare;
  String? _imagePath;

  @override
  void initState() {
    super.initState();
    _refresh();
  }

  Future<void> _refresh() async {
    final bool installed =
        await SocialShareKit.isInstalled(ShareTarget.facebook);
    if (mounted) {
      setState(() {
        _canShare = installed;
        _registered = SocialShareKitFacebook.isRegistered;
      });
    }
  }

  Future<String> _sampleImage() async {
    final String? existing = _imagePath;
    if (existing != null && File(existing).existsSync()) return existing;

    final ui.PictureRecorder recorder = ui.PictureRecorder();
    final Canvas canvas = Canvas(recorder);
    const Size size = Size(600, 600);
    canvas.drawRect(
      Offset.zero & size,
      Paint()..color = const Color(0xFF1877F2),
    );
    canvas.drawCircle(
      size.center(Offset.zero),
      170,
      Paint()..color = Colors.white,
    );

    final ui.Image image = await recorder
        .endRecording()
        .toImage(size.width.toInt(), size.height.toInt());
    final ByteData? data =
        await image.toByteData(format: ui.ImageByteFormat.png);

    final File file = File('${Directory.systemTemp.path}/fb_sample.png');
    await file.writeAsBytes(data!.buffer.asUint8List(), flush: true);
    _imagePath = file.path;
    return file.path;
  }

  Future<void> _share(String label, Future<ShareContent> content) async {
    final ShareResult result = await SocialShareKit.shareTo(
      ShareTarget.facebook,
      await content,
    );

    if (!mounted) return;
    ScaffoldMessenger.of(context)
      ..clearSnackBars()
      ..showSnackBar(
        SnackBar(
          content: Text(
            result.message == null
                ? '$label → ${result.status.name}'
                : '$label → ${result.status.name}\n${result.message}',
          ),
          backgroundColor: result.isSuccess ? null : Colors.red.shade700,
          duration: Duration(seconds: result.isSuccess ? 2 : 6),
        ),
      );
  }

  @override
  Widget build(BuildContext context) {
    final ShareCapabilities capability =
        SocialShareKit.capabilities(ShareTarget.facebook);

    return Scaffold(
      appBar: AppBar(
        title: const Text('Facebook add-on'),
        actions: <Widget>[
          IconButton(onPressed: _refresh, icon: const Icon(Icons.refresh)),
        ],
      ),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: <Widget>[
          Card(
            child: Padding(
              padding: const EdgeInsets.all(16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    'Handler registered: $_registered',
                    style: Theme.of(context).textTheme.titleMedium,
                  ),
                  const SizedBox(height: 4),
                  Text('SDK can share: ${_canShare ?? 'checking…'}'),
                  Text('Supported here: ${capability.isSupported}'),
                  Text('Reports cancellation: ${capability.reportsCancellation}'),
                  Text('Max files: ${capability.maxFiles}'),
                  if (capability.note != null) ...<Widget>[
                    const SizedBox(height: 8),
                    Text(
                      capability.note!,
                      style: Theme.of(context).textTheme.bodySmall,
                    ),
                  ],
                ],
              ),
            ),
          ),
          const SizedBox(height: 16),
          FilledButton(
            onPressed: () => _share(
              'link',
              Future<ShareContent>.value(
                const ShareContent.link('https://pub.flutter-io.cn/packages/social_share_kit'),
              ),
            ),
            child: const Text('Share a link'),
          ),
          const SizedBox(height: 8),
          FilledButton(
            onPressed: () async => _share(
              'photo',
              _sampleImage().then(
                (String path) => ShareContent.files(<String>[path]),
              ),
            ),
            child: const Text('Share a photo'),
          ),
          const SizedBox(height: 8),
          FilledButton(
            onPressed: () => _share(
              'hashtag + link',
              Future<ShareContent>.value(
                const ShareContent.text(
                  '#flutter',
                  url: 'https://pub.flutter-io.cn/packages/social_share_kit',
                ),
              ),
            ),
            child: const Text('Share with a hashtag'),
          ),
          const SizedBox(height: 8),
          OutlinedButton(
            onPressed: () async => _share(
              'photo + video (unsupported)',
              _sampleImage().then(
                (String path) => ShareContent.files(<String>[path, '/tmp/a.mp4']),
              ),
            ),
            child: const Text('Mix photo and video (expect a refusal)'),
          ),
          const SizedBox(height: 16),
          OutlinedButton(
            onPressed: () {
              _registered
                  ? SocialShareKitFacebook.unregister()
                  : SocialShareKitFacebook.register();
              _refresh();
            },
            child: Text(_registered ? 'Unregister add-on' : 'Register add-on'),
          ),
          const Padding(
            padding: EdgeInsets.only(top: 8),
            child: Text(
              'Unregistering hands Facebook back to social_share_kit: intent '
              'sharing on Android, unsupported on iOS.',
              style: TextStyle(fontSize: 12),
            ),
          ),
        ],
      ),
    );
  }
}
0
likes
160
points
56
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Facebook SDK add-on for social_share_kit. Upgrades ShareTarget.facebook to the native Share Dialog on both platforms, with real success, cancel and error reporting.

Repository (GitHub)
View/report issues

Topics

#share #social #facebook #sharing

License

MIT (license)

Dependencies

flutter, social_share_kit

More

Packages that depend on social_share_kit_facebook

Packages that implement social_share_kit_facebook