📙
【Flutter】テキストのシェア機能を実装する
Sample
ライブラリは share_plus を使用します。
(share を使用していましたが、サポートされなくなったため、share_plus
に切り替えています)
Environment
Flutter 3.7.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 12cb4eb7a0 (5 days ago) • 2023-03-01 10:29:26 -0800
Engine • revision ada363ee93
Tools • Dart 2.19.3 • DevTools 2.20.1
Introduce
$ flutter pub add share_plus
environment:
sdk: '>=2.19.3 <3.0.0'
dependencies:
flutter:
sdk: flutter
// This
share_plus: ^6.3.1
Implementation
import 'package:share_plus/share_plus.dart';
class MyApp extends StatelessWidget {
const MyApp({super.key});
final _title = 'Share App';
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: _title),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Share Sample'),
actions: [
IconButton(
icon: const Icon(Icons.share),
onPressed: () => _share('Sample Text'),
),
],
),
);
}
void _share(String text) => Share.share(text);
}
Discussion
pub.dev に記載されているとおり、 share は後継パッケージ share_plus へ刷新されました。2022年以降は、メンテナンスされないとアナウンスされています。今後は share_plus をご利用するように、とのことです。
おそらくご存知かと思いますが、気になったのでコメント致しました。
share_plus | Flutter Package
コメントして頂き、ありがとうございます!!
記事アップデートしておきますね!