Flutter原生表情键盘插件native_emoji_keyboard的使用

Flutter原生表情键盘插件native_emoji_keyboard的使用

native_emoji_keyboard 是一个用于在 Flutter 应用中集成原生 iOS 表情键盘的插件。本文将详细介绍如何安装和使用该插件。

安装

要使用 native_emoji_keyboard 插件,需要满足以下条件:

  • Xcode 11.4+
  • Flutter SDK 2.0.0+

步骤 1: 在 Info.plist 中添加配置

首先,在项目的 Info.plist 文件中添加以下键值对:

<key>io.flutter.embedded_views_preview</key>
<true/>

步骤 2: 添加 UiKitView

在 Flutter 代码中添加 UiKitView 组件以展示表情键盘。例如:

SizedBox(
  width: 350,
  height: 250,
  child: UiKitView(
    viewType: "EmojiTextField",
  ),
)

使用示例

以下是一个完整的示例代码,展示了如何在 Flutter 应用中使用 native_emoji_keyboard 插件。

示例代码

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:native_emoji_keyboard/emoji_keyboard.dart';

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

class MyApp extends StatefulWidget {
  [@override](/user/override)
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // 创建一个 ValueNotifier 用于监听文本变化
  ValueNotifier<String> onTextChange = ValueNotifier<String>("👍");

  // 当前选中的表情
  String emoji = "👍";

  [@override](/user/override)
  void initState() {
    super.initState();

    // 添加监听器,当文本发生变化时更新状态
    onTextChange.addListener(() {
      emoji = onTextChange.value;

      print('emoji: $emoji');
      setState(() {});
    });
  }

  [@override](/user/override)
  void dispose() {
    super.dispose();

    // 释放资源
    onTextChange.dispose();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: SizedBox(
            key: Key(emoji),
            width: 350,
            height: 250,
            child: EmojiKeyboardText(
              text: emoji,
              fontSize: 40,
              onTextChange: onTextChange,
              onTap: () {
                print('Tapped');
              },
            ),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter原生表情键盘插件native_emoji_keyboard的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter原生表情键盘插件native_emoji_keyboard的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


native_emoji_keyboard 是一个 Flutter 插件,用于在应用中集成原生的表情符号键盘。这个插件可以帮助你轻松地在应用中显示原生表情符号键盘,而不需要自己从头开发。

安装插件

首先,你需要在 pubspec.yaml 文件中添加 native_emoji_keyboard 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  native_emoji_keyboard: ^1.0.0  # 请使用最新的版本号

然后运行 flutter pub get 来安装插件。

基本使用

  1. 导入插件

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

    import 'package:native_emoji_keyboard/native_emoji_keyboard.dart';
    
  2. 显示表情符号键盘

    你可以使用 NativeEmojiKeyboard 小部件来显示表情符号键盘。以下是一个简单的示例:

    import 'package:flutter/material.dart';
    import 'package:native_emoji_keyboard/native_emoji_keyboard.dart';
    
    class EmojiKeyboardExample extends StatefulWidget {
      @override
      _EmojiKeyboardExampleState createState() => _EmojiKeyboardExampleState();
    }
    
    class _EmojiKeyboardExampleState extends State<EmojiKeyboardExample> {
      TextEditingController _controller = TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Native Emoji Keyboard Example'),
          ),
          body: Column(
            children: [
              TextField(
                controller: _controller,
                decoration: InputDecoration(
                  labelText: '输入消息',
                ),
              ),
              Expanded(
                child: NativeEmojiKeyboard(
                  onEmojiSelected: (emoji) {
                    setState(() {
                      _controller.text += emoji;
                    });
                  },
                ),
              ),
            ],
          ),
        );
      }
    }
    

    在这个示例中,我们创建了一个包含 TextFieldNativeEmojiKeyboard 的页面。当用户选择一个表情符号时,onEmojiSelected 回调会被触发,并将选中的表情符号添加到 TextField 中。

  3. 自定义表情符号键盘

    NativeEmojiKeyboard 提供了一些可选的参数来自定义键盘的外观和行为。例如:

    NativeEmojiKeyboard(
      onEmojiSelected: (emoji) {
        setState(() {
          _controller.text += emoji;
        });
      },
      backgroundColor: Colors.grey[200],
      emojiSize: 24.0,
      showBackspace: true,
    );
回到顶部