flutter - 如何在 Flutter 中实现接受文本和 URL 的 Share Extension?
问题描述
我已经使用 Flutter 构建了我的应用程序,但我不确定如何实现通过 Safari 浏览器将 Flutter 中的文本和 URL 接受到 Flutter 应用程序的共享扩展。任何帮助将不胜感激!我在这里先向您的帮助表示感谢:)
例如,当我进入 Safari 浏览器并共享一些 URL 或文本时,我的应用程序应该显示在共享列表中,当我单击应用程序时,它应该在 TextEditField 中打开带有文本或 URL 的应用程序。
解决方案
这个包可能会帮助你:receive_sharing_intent。
此代码来自包的“入门”部分中的示例:
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
StreamSubscription _intentDataStreamSubscription;
List<SharedMediaFile> _sharedFiles;
String _sharedText;
@override
void initState() {
super.initState();
// For sharing images coming from outside the app while the app is in the memory
_intentDataStreamSubscription =
ReceiveSharingIntent.getMediaStream().listen((List<SharedMediaFile> value) {
setState(() {
print("Shared:" + (_sharedFiles?.map((f)=> f.path)?.join(",") ?? ""));
_sharedFiles = value;
});
}, onError: (err) {
print("getIntentDataStream error: $err");
});
// For sharing images coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialMedia().then((List<SharedMediaFile> value) {
setState(() {
_sharedFiles = value;
});
});
// For sharing or opening urls/text coming from outside the app while the app is in the memory
_intentDataStreamSubscription =
ReceiveSharingIntent.getTextStream().listen((String value) {
setState(() {
_sharedText = value;
});
}, onError: (err) {
print("getLinkStream error: $err");
});
// For sharing or opening urls/text coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialText().then((String value) {
setState(() {
_sharedText = value;
});
});
}
@override
void dispose() {
_intentDataStreamSubscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
const textStyleBold = const TextStyle(fontWeight: FontWeight.bold);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: <Widget>[
Text("Shared files:", style: textStyleBold),
Text(_sharedFiles?.map((f)=> f.path)?.join(",") ?? ""),
SizedBox(height: 100),
Text("Shared urls/text:", style: textStyleBold),
Text(_sharedText ?? "")
],
),
),
),
);
}
}
推荐阅读
- css - How to remove padding for mobile devices on WordPress
- javascript - 已发布的 React 应用程序中函数的可变性
- nullpointerexception - 写入 Excel 文件时出现空指针异常
- arrays - Juggling 算法的时间复杂度应该是 O(n) 吗?
- cors - WSO2 EI 在从 Angular 客户端请求时给出预检请求 CORS 错误
- javascript - 如何为使用方括号的 Google AMP 的 JSX 属性编写类型声明?
- php - 使用 PHP mail() 没有收到任何电子邮件。?
- python - 覆盖 [] 以构建 Python 列表
- android - 如何在 Coordinator Layout 中将 bottomNavigationView 固定到底部?
- c++ - 如何在提升日期时间中创建日期范围?