0
votes

Changer de couleur de texte si le texte contient hashtag "#"

Fondamentalement, je veux atteindre une fonctionnalité "hashtag". Je fais du mal à faire une logique dans laquelle si le texte contient un "#" (par exemple, j'adore #flutter), le texte #flutter changera sa couleur en bleu.

Pouvez-vous me donner un indice sur quel widget utiliser ou y a-t-il un paquet pour cela?

Je n'arrive pas à trouver un problème similaire.


1 commentaires

J'ai essayé et ça ne change pas de couleur. Pouvez-vous, s'il vous plaît, montrer une capture d'écran?


3 Réponses :


2
votes

J'espère que cela fonctionne!

Widget _getColoredHashtagText(String text) {
  if (text.contains('#')) {
    var preHashtag = text.substring(0, text.indexOf('#'));
    var postHashtag = text.substring(text.indexOf('#'));
    var hashTag = postHashtag;
    var other;
    if (postHashtag.contains(' ')) {
      hashTag = postHashtag.substring(0, postHashtag.indexOf(' '));
      other = postHashtag.substring(postHashtag.indexOf(' '));
    }
    return RichText(
      text: TextSpan(
        style: DefaultTextStyle.of(context).style,
        children: <TextSpan>[
          TextSpan(text: preHashtag),
          TextSpan(text: hashTag, style: TextStyle(color: Colors.blue)),
          TextSpan(text: other != null ? other : ""),
        ],
      ),
    );
  } else {
    return Text(text);
  }
}


2 commentaires

J'ai le gist de votre code bro. Mais malheureusement, cela ne fonctionne que sur le premier # .


exemple que j'ai "#flutter est le #Best" #flutter est le seul bleu coloré



1
votes

avec richtext code> Vous pouvez créer un texte avec différents styles, où chaque texte est un textspan code>, comme celui-ci:

RichText _convertHashtag(String text) {
  List<String> split = text.split(RegExp("#"));
  List<String> hashtags = split.getRange(1, split.length).fold([], (t, e) {
    var texts = e.split(" ");
    if (texts.length > 1) {
      return List.from(t)
        ..addAll(["#${texts.first}", "${e.substring(texts.first.length)}"]);
    }
    return List.from(t)..add("#${texts.first}");
  });
  return RichText(
    text: TextSpan(
      children: [TextSpan(text: split.first)]..addAll(hashtags
          .map((text) => text.contains("#")
              ? TextSpan(text: text, style: TextStyle(color: Colors.blue))
              : TextSpan(text: text))
          .toList()),
    ),
  );
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    backgroundColor: Colors.indigo,
    appBar: AppBar(title: Text('#hashtag')),
    body: Center(
      child: _convertHashtag("I love #flutter and #android very much"),
    ),
  );
}


2 commentaires

Merci mec! Une dernière chose. Je n'arrive pas à trouver comment Overflow en utilisant Textspan. C'est ma première fois avec ce widget.


Ceci est une solution rapide, vous pourriez l'améliorer pour faciliter l'application de tout style souhaité. À propos du débordement, Richtext a la propriété



0
votes
HashTagText(text: "I will found and #highlight all #tag and #make it #clickable", onHashTagClick: (tag){
              print("You clicked on $tag");
            },)

0 commentaires