Flutter不良词汇检测插件ai_profanity_textfield的使用

发布于 1周前 作者 sinazl 来自 Flutter

Flutter不良词汇检测插件ai_profanity_textfield的使用

AI Profanity TextField 🛡️

一个智能的、基于AI的Flutter文本字段小部件,使用Gemini API实时过滤不良词汇!在任何语言中保持应用内容的清洁和友好!✨

ai_profanity

特性 🌟

  • 🌍 支持所有语言的多语言不良词汇检测
  • 🤖 使用Gemini的AI支持不良词汇检测
  • ✅ 支持自定义验证器
  • ⚡ 实时验证(边打字边验证)
  • 🎨 完全可定制的外观
  • 🔄 加载指示器和验证状态
  • ❌ 自定义错误消息
  • ✅ 成功指示器
  • 🕒 支持防抖动以优化性能

语言支持 🌐

多亏了Gemini强大的AI能力,我们的小部件可以检测以下语言中的不良词汇:

  • 英语 🇺🇸
  • 西班牙语 🇪🇸
  • 法语 🇫🇷
  • 德语 🇩🇪
  • 土耳其语 🇹🇷
  • 中文 🇨🇳
  • 日语 🇯🇵
  • 韩语 🇰🇷
  • 阿拉伯语 🇸🇦
  • 俄语 🇷🇺
  • …还有很多!

AI模型理解每种语言的上下文和文化细微差别,确保全球范围内准确的不良词汇检测! 🌎

开始使用 🚀

安装

pubspec.yaml文件中添加以下内容:

dependencies:
  ai_profanity_textfield: ^1.0.0

获取您的API密钥

  1. 访问 Google AI Studio
  2. 使用您的Google帐户登录
  3. 点击“创建API密钥”
  4. 复制您的API密钥

快速开始 🏃

ProfanityTextFormField(
  geminiService: geminiService, // 您的Gemini服务实例
  decoration: InputDecoration(
    hintText: 'Enter text here...',
  ),
  onProfanityDetected: (text) {
    print('Profanity detected in: $text');
  },
)

自定义验证器 ✨

您可以添加自己的验证规则,与AI不良词汇检测一起使用!

ProfanityTextFormField(
  geminiService: geminiService,
  checkWhileTyping: true,
  debounceDuration: const Duration(milliseconds: 500),
  validators: [
    (value) => value.isEmpty ? 'Required field' : null,
    (value) => value.length < 3 ? 'Too short' : null,
    (value) => !value.contains('@') ? 'Must contain @' : null,
  ],
  onProfanityDetected: (text) => print('Profanity detected!'),
  onCleanText: (text) => print('Text is clean and valid'),
  decoration: InputDecoration(
    hintText: 'Type something...',
    fillColor: Colors.white,
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(18),
    ),
  ),
  showValidIcon: true,
  profanityMessage: 'Oops! Let\'s keep it friendly 😊',
  validationMessageDuration: const Duration(seconds: 3),
)

验证过程 🔄

  1. 自定义验证器首先运行,按它们定义的顺序。
  2. 如果所有自定义验证通过,将运行AI不良词汇检查。
  3. 文本仅当自定义验证和不良词汇检查都通过时才被视为有效。

多语言示例 🌐

ProfanityTextFormField(
  geminiService: geminiService,
  decoration: InputDecoration(
    hintText: '여기에 텍스트를 입력하세요...', // 韩语
    // 或者 '在此输入文字...' // 中文
    // 或者 'Écrivez ici...' // 法语
    // 等等
  ),
  profanityMessage: 'Lütfen nazik olalım! 😊', // 土耳其语
  validators: [
    // 自定义验证器适用于任何语言!
    (value) {
      if (value.length < 5) {
        return '最小5文字必要です'; // 日语错误信息
      }
      return null;
    },
  ],
)

高级功能 🔧

实时验证 ⚡

小部件在用户输入时验证文本,并带有可自定义的防抖持续时间:

ProfanityTextFormField(
  geminiService: geminiService,
  checkWhileTyping: true,
  debounceDuration: Duration(milliseconds: 500),
  validators: [
    // 自定义验证器也实时运行!
    (value) => value.length < 3 ? 'Too short!' : null,
  ],
  onCleanText: (text) {
    print('Text is clean and valid: $text');
  },
)

自定义样式 🎨

让其匹配您的应用程序主题:

ProfanityTextFormField(
  geminiService: geminiService,
  successDecoration: InputDecoration(
    fillColor: Colors.green,
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(18),
    ),
  ),
  profanityDecoration: InputDecoration(
    fillColor: Colors.red,
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(18),
    ),
  ),
  progressIndicatorColor: Colors.blue,
  progressIndicatorSize: 20,
)

验证状态 🚦

小部件提供了不同的状态,具有可自定义的外观:

  • ⏳ 加载中
  • ✅ 有效(所有自定义验证和不良词汇检查均通过)
  • ❌ 无效
  • 🆕 初始状态

错误处理 🚫

任何语言的自定义错误消息和处理程序:

ProfanityTextFormField(
  geminiService: geminiService,
  profanityMessage: 'Please keep it friendly! 😊',
  validators: [
    (value) => value.isEmpty ? 'Field cannot be empty!' : null,
  ],
  onError: (error) {
    print('Error occurred: $error');
  },
  clearOnProfanity: true,
)

属性 📋

属性 类型 描述
geminiService GeminiService 必需。您的Gemini API服务实例
validators List<FormFieldValidator<String>?>? 自定义验证规则
checkWhileTyping bool 启用/禁用实时验证
debounceDuration Duration 验证触发前的延迟
onProfanityDetected Function(String)? 当发现不良词汇时的回调
onCleanText Function(String)? 当文本干净且有效时的回调
clearOnProfanity bool 发现不良词汇时是否清空字段
showValidIcon bool 是否显示验证图标
profanityMessage String 自定义错误消息(支持所有语言)

示例 📝

用户名字段

ProfanityTextFormField(
  geminiService: geminiService,
  decoration: InputDecoration(
    labelText: 'Username',
    prefixIcon: Icon(Icons.person),
  ),
  validators: [
    (value) => value.length < 3 ? 'Username too short' : null,
    (value) => value.contains(' ') ? 'No spaces allowed' : null,
  ],
)

评论字段

ProfanityTextFormField(
  geminiService: geminiService,
  decoration: InputDecoration(
    labelText: 'Comment',
    hintText: 'Share your thoughts...',
  ),
  maxLines: 3,
  clearOnProfanity: true,
  profanityMessage: 'Please keep comments friendly!',
)

搜索字段

ProfanityTextFormField(
  geminiService: geminiService,
  decoration: InputDecoration(
    labelText: 'Search',
    prefixIcon: Icon(Icons.search),
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(30),
    ),
  ),
  debounceDuration: Duration(milliseconds: 300),
)

更多关于Flutter不良词汇检测插件ai_profanity_textfield的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter不良词汇检测插件ai_profanity_textfield的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中集成和使用ai_profanity_textfield插件的一个示例。这个插件可以帮助你在用户输入时检测不良词汇。

1. 添加依赖

首先,在你的pubspec.yaml文件中添加ai_profanity_textfield依赖:

dependencies:
  flutter:
    sdk: flutter
  ai_profanity_textfield: ^最新版本号 # 请替换为实际发布的最新版本号

然后运行flutter pub get来安装依赖。

2. 导入插件

在你的Dart文件中导入插件:

import 'package:flutter/material.dart';
import 'package:ai_profanity_textfield/ai_profanity_textfield.dart';

3. 使用插件

下面是一个完整的示例,展示了如何在Flutter应用中使用ai_profanity_textfield

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('不良词汇检测示例'),
        ),
        body: Center(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: ProfanityTextField(
              controller: TextEditingController(),
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                labelText: '输入文本',
              ),
              onProfanityDetected: (String profanity) {
                // 当检测到不良词汇时调用的回调
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(
                    content: Text('检测到不良词汇: $profanity'),
                    backgroundColor: Colors.red,
                  ),
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}

解释

  1. 添加依赖:在pubspec.yaml中添加依赖项。
  2. 导入插件:在你的Dart文件中导入ai_profanity_textfield
  3. 使用插件
    • 创建一个Scaffold作为主布局。
    • 使用ProfanityTextField代替标准的TextField
    • 设置controller来管理文本输入。
    • 使用decoration来自定义输入框的外观。
    • 当检测到不良词汇时,通过onProfanityDetected回调显示一个Snackbar通知用户。

这个示例展示了基本的用法,你可以根据需求进一步自定义和扩展,比如添加更多的文本格式化、验证规则等。

回到顶部