Flutter用户反馈插件flutter_feedback的使用
Flutter用户反馈插件flutter_feedback的使用
功能
- ✅ 可以截取当前页面的屏幕截图。
- ✅ 支持Android和iOS平台。
- ❌ 不支持Web。
- ✅ 提供反馈表单模板。
- ✅ 预览截图。
- ✅ 编辑截图。可以在截图上添加文字和线条。
设置
Android
对于Android平台,此插件需要WRITE_EXTERNAL_STORAGE
权限。因此,你需要在AndroidManifest.xml
文件中添加该权限,如下所示:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="id.net.nusa.plugin.flutter_feedback_example">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
iOS
对于iOS平台,此插件需要NSPhotoLibraryUsageDescription
权限。因此,你需要在Info.plist
文件中添加该权限,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
...
<key>NSPhotoLibraryUsageDescription</key>
<string>用于保存截图到相册。</string>
...
</dict>
</plist>
同时,你需要在Podfile
中启用照片权限:
target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
## dart: PermissionGroup.calendar
'PERMISSION_EVENTS=0',
## dart: PermissionGroup.reminders
'PERMISSION_REMINDERS=0',
## dart: PermissionGroup.contacts
'PERMISSION_CONTACTS=0',
## dart: PermissionGroup.camera
'PERMISSION_CAMERA=0',
## dart: PermissionGroup.microphone
'PERMISSION_MICROPHONE=0',
## dart: PermissionGroup.speech
'PERMISSION_SPEECH_RECOGNIZER=0',
## dart: PermissionGroup.photos
'PERMISSION_PHOTOS=1',
## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
'PERMISSION_LOCATION=0',
## dart: PermissionGroup.notification
'PERMISSION_NOTIFICATIONS=0',
## dart: PermissionGroup.mediaLibrary
'PERMISSION_MEDIA_LIBRARY=0',
## dart: PermissionGroup.sensors
'PERMISSION_SENSORS=0',
## dart: PermissionGroup.bluetooth
'PERMISSION_BLUETOOTH=0',
## dart: PermissionGroup.appTrackingTransparency
'PERMISSION_APP_TRACKING_TRANSPARENCY=0',
## dart: PermissionGroup.criticalAlerts
'PERMISSION_CRITICAL_ALERTS=0',
]
end
使用方法
使用此插件非常简单。你只需调用takeScreenshot(context)
函数,并检查返回值以确定是否成功或失败。以下是一个完整的示例:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_feedback/flutter_feedback.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
scaffoldMessengerKey: scaffoldMessengerKey,
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
[@override](/user/override)
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final formFeedbackController = FormFeedbackController();
var locale = 'en';
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter反馈插件'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () async {
final flutterFeedback = FlutterFeedback();
final result = await flutterFeedback.takeScreenshot(context);
switch (result!.status) {
case Status.success:
// 截图成功保存。
// 这里是一个例子,如果想导航到预览截图结果的页面。
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return PreviewImagePage(File(result.path!));
},
),
);
break;
case Status.denied:
_showSnackbBar('权限被拒绝');
break;
case Status.restricted:
_showSnackbBar('权限受限');
break;
case Status.permanentlyDenied:
_showSnackbBar('权限永久拒绝');
break;
case Status.fileNotFound:
_showSnackbBar('截图文件未找到');
break;
case Status.unknown:
_showSnackbBar('未知错误');
break;
}
},
child: Text('截屏'),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Radio(
value: 'en',
groupValue: locale,
onChanged: (String? value) => setState(() => locale = value ?? 'en'),
),
Text('英语'),
SizedBox(width: 16),
Radio(
value: 'id',
groupValue: locale,
onChanged: (String? value) => setState(() => locale = value ?? 'id'),
),
Text('印尼语'),
],
),
],
),
),
);
}
void _showSnackbBar(String message) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message)));
}
}
更多关于Flutter用户反馈插件flutter_feedback的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter用户反馈插件flutter_feedback的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中集成和使用flutter_feedback
插件的详细代码示例。这个插件允许用户通过简单的界面提供反馈,非常适用于收集用户意见和建议。
步骤 1: 添加依赖
首先,在你的Flutter项目的pubspec.yaml
文件中添加flutter_feedback
依赖。
dependencies:
flutter:
sdk: flutter
flutter_feedback: ^x.y.z # 请替换为最新版本号
步骤 2: 安装依赖
保存pubspec.yaml
文件后,在命令行中运行以下命令以安装依赖:
flutter pub get
步骤 3: 配置插件
flutter_feedback
插件需要在iOS和Android平台上进行一些配置,以便正确显示反馈界面。
iOS配置
- 打开
ios/Runner/Info.plist
文件,并添加以下权限请求,以允许应用访问网络(用于发送反馈数据)。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
- 如果你希望将反馈发送到你的服务器,你需要在
AppDelegate.swift
中配置API端点。
import UIKit
import Flutter
import flutter_feedback
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
// 配置flutter_feedback插件
let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
let feedback = FlutterFeedback(flutterEngine: flutterEngine, apiEndpoint: "https://your-api-endpoint.com/submit-feedback")
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Android配置
在android/app/src/main/AndroidManifest.xml
中,确保你有网络访问权限:
<uses-permission android:name="android.permission.INTERNET"/>
步骤 4: 在Flutter代码中使用插件
现在,你可以在你的Flutter代码中导入并使用flutter_feedback
插件了。
import 'package:flutter/material.dart';
import 'package:flutter_feedback/flutter_feedback.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Feedback Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Feedback Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You can send feedback by tapping the button below.',
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
// 打开反馈界面
await FlutterFeedback.instance.showFeedbackForm(
title: 'Provide Feedback',
description: 'Please share your thoughts on our app.',
placeholder: 'Your feedback here...',
categoryLabel: 'Category',
categoryOptions: ['Bug', 'Suggestion', 'Praise'],
contactInfoLabel: 'Your Contact Info (optional)',
userEmailLabel: 'Your Email',
attachmentLabel: 'Attach a screenshot or file',
sendButtonText: 'Send Feedback',
cancelButtonText: 'Cancel',
isEmailRequired: false,
isAttachmentRequired: false,
// 如果你要将反馈发送到自定义服务器,请提供API端点
// apiEndpoint: 'https://your-api-endpoint.com/submit-feedback',
onSuccess: (data) {
// 反馈发送成功后的回调
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Feedback submitted successfully!')),
);
},
onError: (error) {
// 反馈发送失败后的回调
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to submit feedback: $error')),
);
},
);
},
child: Text('Send Feedback'),
),
],
),
),
);
}
}
以上代码展示了如何在Flutter应用中集成flutter_feedback
插件,并允许用户通过点击按钮发送反馈。记得将apiEndpoint
替换为你自己的API端点,如果你希望将反馈数据发送到自定义服务器。