fl_shared_link 0.3.2 copy "fl_shared_link: ^0.3.2" to clipboard
fl_shared_link: ^0.3.2 copied to clipboard

Gets parameters when the app is opened, and listens to be opened

example/lib/main.dart

import 'package:fl_shared_link/fl_shared_link.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

bool get _isAndroid => defaultTargetPlatform == TargetPlatform.android;

bool get _isIOS => defaultTargetPlatform == TargetPlatform.iOS;

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
          appBar: AppBar(title: const Text('FlSharedLink Plugin')),
          body: Container(
              alignment: Alignment.center,
              padding: const EdgeInsets.all(20),
              child: const SingleChildScrollView(child: HomePage())))));
}

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

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

class _HomePageState extends State<HomePage> {
  IOSUniversalLinkModel? universalLink;
  IOSOpenUrlModel? openUrl;
  Map? launchingOptionsWithIOS;
  AndroidIntentModel? intent;
  String? realPath;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      if (_isIOS) {
        universalLink = await FlSharedLink().universalLinkWithIOS;
        openUrl = await FlSharedLink().openUrlWithIOS;
        launchingOptionsWithIOS = await FlSharedLink().launchingOptionsWithIOS;
      }
      if (_isAndroid) intent = await FlSharedLink().intentWithAndroid;
      setState(() {});
      FlSharedLink().receiveHandler(
          onUniversalLink: (IOSUniversalLinkModel? data) {
        universalLink = data;
        setState(() {});
      }, onOpenUrl: (IOSOpenUrlModel? data) {
        openUrl = data;
        setState(() {});
      }, onIntent: (AndroidIntentModel? data) {
        intent = data;
        setState(() {});
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> children = [];
    if (_isAndroid) children = androidChildren;
    if (_isIOS) children = iosChildren;
    children.add(ElevatedButton(
        onPressed: () {
          FlSharedLink().clearCache();
        },
        child: const Text('清除缓存')));
    return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: children);
  }

  List<Widget> get androidChildren => [
        const Text('Android Intent'),
        const SizedBox(height: 10),
        Container(
            width: double.infinity,
            padding: const EdgeInsets.all(12),
            margin: const EdgeInsets.all(12),
            alignment: Alignment.center,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8),
                color: Colors.grey.withValues(alpha: 0.3)),
            child: Text('${intent?.toMap()}')),
        const SizedBox(height: 10),
        ElevatedButton(
            onPressed: getRealFilePathWithAndroid,
            child: const Text('Android uri转真实文件地址 兼容微信QQ')),
        const SizedBox(height: 10),
        Container(
            width: double.infinity,
            padding: const EdgeInsets.all(12),
            margin: const EdgeInsets.all(12),
            alignment: Alignment.center,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8),
                color: Colors.grey.withValues(alpha: 0.3)),
            child: Text(realPath.toString())),
        const SizedBox(height: 30),
      ];

  void getRealFilePathWithAndroid() async {
    final id = intent?.id;
    if (id == null) return;
    realPath = await FlSharedLink().getRealFilePathWithAndroid(id);
    setState(() {});
  }

  List<Widget> get iosChildren => [
        const Text('IOS Launching Options'),
        const SizedBox(height: 10),
        Container(
            width: double.infinity,
            padding: const EdgeInsets.all(12),
            margin: const EdgeInsets.all(12),
            alignment: Alignment.center,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8),
                color: Colors.grey.withValues(alpha: 0.3)),
            child: Text('$launchingOptionsWithIOS')),
        const SizedBox(height: 10),
        const Text('IOS UniversalLink'),
        const SizedBox(height: 10),
        Container(
            width: double.infinity,
            padding: const EdgeInsets.all(12),
            margin: const EdgeInsets.all(12),
            alignment: Alignment.center,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8),
                color: Colors.grey.withValues(alpha: 0.3)),
            child: Text('${universalLink?.toMap()}')),
        const SizedBox(height: 10),
        const Text('IOS openUrl'),
        const SizedBox(height: 10),
        Container(
            width: double.infinity,
            padding: const EdgeInsets.all(12),
            margin: const EdgeInsets.all(12),
            alignment: Alignment.center,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8),
                color: Colors.grey.withValues(alpha: 0.3)),
            child: Text('${openUrl?.toMap()}')),
        const SizedBox(height: 30),
      ];
}
4
likes
160
points
27
downloads

Publisher

unverified uploader

Weekly Downloads

Gets parameters when the app is opened, and listens to be opened

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on fl_shared_link