Quantcast
Channel: How to delete/detect any emoji as a whole from a string in Flutter? - Stack Overflow
Viewing all articles
Browse latest Browse all 10

Answer by Kenneth Li for How to delete/detect any emoji as a whole from a string in Flutter?

$
0
0

This answer still has problem

Since dart does not provide the data type 'Grapheme Cluster', I try to use method channel to do this using kotlin:

Step 1: Create a new 'Flutter Plugin' project, name the project 'gmc01', 2 files will be created automatically, namely 'gmc01.dart' and 'main.dart'.

Step 2: replace the codes in gmc01.dart with the following:

import 'dart:async';import 'package:flutter/services.dart';class Gmc01 {  static const MethodChannel _channel =      const MethodChannel('gmc01');  static Future<String> removeLastGMC(String strOriginal) async {    final String version = await _channel.invokeMethod('removeLastGMC', strOriginal);    return version;  }}

Step 3: Replace the codes in main.dart with the following:

import 'package:gmc01/gmc01.dart';void main() async {  String strTemp = '12345678我们5🇵🇬你😀👨‍👩‍👦';  strTemp = await Gmc01.removeLastGMC(strTemp);  print(strTemp);  strTemp = await Gmc01.removeLastGMC(strTemp);  print(strTemp);  strTemp = await Gmc01.removeLastGMC(strTemp);  print(strTemp);  strTemp = await Gmc01.removeLastGMC(strTemp);  print(strTemp);  strTemp = await Gmc01.removeLastGMC(strTemp);  print(strTemp);  strTemp = await Gmc01.removeLastGMC(strTemp);  print(strTemp);  strTemp = await Gmc01.removeLastGMC(strTemp);  print(strTemp);}

Step 4: Inside android/build.gradle, change the minSdkVersion from 16 to 24.

Step 5: Inside example/android/app/build.gradle, change the minSdkVersion from 16 to 24.

Step 6: Click File->Open, select gmc01->android, then click 'OK', the kotlin part of the plugin will be opened (In another Window).

Step 7: Replace the codes in Gmc01Plugin.kt with the following: (Replace the first line with your own package name)

package com.example.gmc01   // replace the left with your own package nameimport io.flutter.plugin.common.MethodCallimport io.flutter.plugin.common.MethodChannelimport io.flutter.plugin.common.MethodChannel.MethodCallHandlerimport io.flutter.plugin.common.MethodChannel.Resultimport io.flutter.plugin.common.PluginRegistry.Registrarimport android.icu.text.BreakIteratorclass Gmc01Plugin: MethodCallHandler {  companion object {    @JvmStatic    fun registerWith(registrar: Registrar) {      val channel = MethodChannel(registrar.messenger(), gmc01)      channel.setMethodCallHandler(Gmc01Plugin())    }  }  override fun onMethodCall(call: MethodCall, result: Result) {    var strArg: String    strArg = call.arguments.toString()    var boundary = BreakIterator.getWordInstance()    boundary.setText(strArg);    when (call.method) {      removeLastGMC -> {        result.success(removeLastGMC(boundary, strArg))      }      else -> {        result.notImplemented()      }    }  }  fun removeLastGMC(boundary: BreakIterator, source: String):String {    val end = boundary.last()    val start = boundary.previous()    return source.substring(0, start)  }}

Step 8: Go back to the window of the plugin, and click 'Run'.

Here are the output in the console:

I/flutter (22855): 12345678我们5🇵🇬你😀I/flutter (22855): 12345678我们5🇵🇬你I/flutter (22855): 12345678我们5🇵🇬I/flutter (22855): 12345678我们5I/flutter (22855): 12345678我们I/flutter (22855): 12345678I/flutter (22855): 

As you can see, the 'Family Emoji', 'Face Emoji' and 'Country Flag' emoji are removed correctly, but the Chinese 2 chars '我们' and the digits '12345678' are removed by using a single removeLastGMC, so still need to figure out how to distinguish Chinese Double Bytes characters / English Chars / Emojis.

BTW, I don't know how to do the Swift part, can someone help?


Viewing all articles
Browse latest Browse all 10

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>