Skip to content

SocialText provides a lightweight widget and parser for Flutter to find and render social tokens like @mentions and #hashtags. Customize triggers and per-prefix styles, and handle tap/long‑press events. Includes a simple parser for extracting tokens without UI.

License

Notifications You must be signed in to change notification settings

Chndr-3/social_text

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

social_text

pub package

Detect, style and interact with social tokens like @mentions, #hashtags, and custom prefixes in Flutter.

Features

  • Parse tokens for any prefix: @, #, $, %, or custom
  • Style per-trigger via styleMap
  • Tappable tokens with onTap(SocialToken)
  • Optional onLongPress(SocialToken)
  • Preserves plain text between tokens and line wrapping
  • Simple parser helper: SocialParser.parse()

Quick Start

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

class Demo extends StatelessWidget {
  const Demo({super.key});
  @override
  Widget build(BuildContext context) {
    return SocialText(
      text: 'Hello @flutterdev! Love #opensource projects.',
      triggers: ['@', '#'],
      style: const TextStyle(fontSize: 16),
      styleMap: const {
        '@': TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
        '#': TextStyle(color: Colors.green),
      },
      onTap: (token) {
        ScaffoldMessenger.of(context)
            .showSnackBar(SnackBar(content: Text('Tapped ${token.full}')));
      },
    );
  }
}

API Overview

class SocialToken {
  final String prefix; // e.g. '@'
  final String value;  // e.g. 'flutterdev'
  final int start;     // token start index in text
  final int end;       // token end index in text (exclusive)
  const SocialToken(this.prefix, this.value, this.start, this.end);
}

// Parse only
final tokens = SocialParser.parse('Hey @flutterdev check out #flutter!',
  triggers: ['@', '#'],
);

// Build interactive text
SocialText(
  text: 'Hey @flutterdev check out #flutter and \$dart!',
  triggers: ['@', '#', r'$'],
  style: const TextStyle(color: Colors.black87),
  styleMap: const {
    '@': TextStyle(color: Colors.blue),
    '#': TextStyle(color: Colors.purple),
    r'$': TextStyle(color: Colors.teal),
  },
  onTap: (token) => debugPrint('Tapped ${token.full}'),
  onLongPress: (token) => debugPrint('Long-pressed ${token.full}'),
)

Notes

  • Uses RichText + TextSpan under the hood; when both onTap and onLongPress are provided, tokens use WidgetSpan + GestureDetector to support both.
  • Default token characters are [A-Za-z0-9_]+. Override by using your own RegExp via SocialParser.tokenRegExp(triggers: ..., allowedCharsPattern: ...) if needed.
  • Emojis and multi-byte characters are preserved; token indices are Dart string code-unit indices.

Example App

See example/lib/main.dart for a runnable demo that shows:

  • Multiple triggers
  • Per-trigger styles
  • Tap callbacks showing a SnackBar

Run it with:

cd example
flutter run

About

SocialText provides a lightweight widget and parser for Flutter to find and render social tokens like @mentions and #hashtags. Customize triggers and per-prefix styles, and handle tap/long‑press events. Includes a simple parser for extracting tokens without UI.

Resources

License

Stars

Watchers

Forks

Packages

No packages published