Code Monkey home page Code Monkey logo

flutter_social_textfield's Introduction

flutter_social_textfield

Overview

A Flutter plugin that helps detection of common social media contents. The current position of the cursor is used for detecting content. Current detection types are:

  • HashTag (#): Texts start with #
  • Mention (@): Text start with @ sign
  • Web Links: Can detect web links
  • Emojis: can detect emojis [thanks @moazh]

You can see the supported detection types from DetectedType enum

enum DetectedType{
    mention, //By Default texts starting with @sign
    hashtag,  //By default, texts starting with #hashtag
    url, //By default texts starting with http://xxx.yyy or https://aaa.bbb 
    plain_text //Any other type falls into this.
}

You can change the relevant regex for that type if you want, which has been mentioned in Configuring the Text Editing Controller part.

Getting Started

SocialTextField is basically an improved Text Editing Controller, so you can just assign text controller to any text editing widget that you want.

_textEditingController = SocialTextEditingController();

TextField(
    controller: _textEditingController,
    expands: true,
    maxLines: null,
    minLines: null,
    decoration: InputDecoration(
    hintText: "Please Enter a Text"
    ),
)

Configuring the Text Editing Controller

SocialTextEditingController can be configured by calling configuration methods.

There are 2 setter methods for this.

  • setTextStyle: Change how detected content shown.

    setTextStyle(DetectedType.mention, TextStyle(...)

  • setRegexp: Override the detection regexp

    setRegexp(DetectedType.url, RegExp("your_own_regex_fr_this_type"))

It is recommended to set these values on initialization;

_textEditingController = SocialTextEditingController()
    ..setTextStyle(DetectedType.mention, TextStyle(color: Colors.purple,backgroundColor: Colors.purple.withAlpha(50)))
    ..setRegexp(DetectedType.url, RegExp("your_own_regex_fr_this_type"));

Listening Content Detection Events

One of the main reasons that i want to create this plugin was that i needed to listen detection events from different widgets. So flutter_social_textfield provides a stream for detections so you can subscribe and listen events when needed.

StreamSubscription<SocialContentDetection> _streamSubscription;

@override
void initState() {
super.initState();
_textEditingController = SocialTextEditingController()
..text = "Lorem ipsum amet."
..setTextStyle(DetectedType.mention, TextStyle(color: Colors.purple,backgroundColor: Colors.purple.withAlpha(50)))
..setTextStyle(DetectedType.url, TextStyle(color: Colors.blue, decoration: TextDecoration.underline))
..setTextStyle(DetectedType.hashtag, TextStyle(color: Colors.blue, fontWeight: FontWeight.w600));

//Subscribe to events
 _streamSubscription = _textEditingController.subscribeToDetection(onDetectContent);
}

void onDetectContent(SocialContentDetection detection){
 print("Detected Contet: $detection");
}

don't forget to unsubscribe on dispose.

@override
void dispose() {
    _streamSubscription.cancel();
    super.dispose();
}

The Stream Subscriptoin returns SocialContentDetection class:

class SocialContentDetection{
    final DetectedType type; //mention, url, hashtag, plan_text
    final TextRange range; //range of detected content
    final String text; //detected content
    SocialContentDetection(this.type, this.range, this.text);
    
    @override
    String toString() {
        return 'SocialContentDetection{type: $type, range: $range, text: $text}';
    }
}

DefaultSocialTextFieldController

I've implemented an experimental widget for ease of use. DefaultSocialTextFieldController puts the main content inside a Stack and allows users to show relevant content when curser comes over a detected content

As with version 0.0.9 There are 2 different types of presentation of detection content:

Detection Presentation Mode

DetectionPresentationMode.split_screen : Shows detection view but moving from bottom to determined widget size. useful for full screen textfields like text editors

DetectionPresentationMode.above_text_field : Useful for showing detection view from above the textfield. The location of textfield is detected from the FocusNode automatically.

Please notice the 'scrollPhysics' property of the TextField;

DefaultSocialTextFieldController(
        detectionPresentationMode: DetectionPresentationMode.above_text_field,
        focusNode: _focusNode,
        scrollController: _scrollController,
        textEditingController: _textEditingController,
        child: Container(
          padding: EdgeInsets.all(8),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Expanded(
                child: TextField(
                  scrollPhysics: ClampingScrollPhysics(), //Use this for unnecessary scroll bounces
                  scrollController: _scrollController,
                  focusNode: _focusNode,
                  controller: _textEditingController,
                  expands: true,
                  maxLines: null,
                  minLines: null,
                  decoration: InputDecoration(
                      hintText: "Please Enter a Text"
                  ),
                ),
              ),
            ],
          ),
        ),
        detectionBuilders: {
          DetectedType.mention:(context)=>mentionContent(height),
          DetectedType.hashtag:(context)=>hashtagContent(height),
          DetectedType.url:(context)=>urlContent(height)
        },
      )

DefaultSocialTextFieldController in action

Using DefaultSocialTextFieldController

An example usage of the DefaultSocialTextFieldController can be seen in the example project. The main goal is to provide an easy way to implement features that have been seen in most of the social media applications.

SocialTextSpanBuilder

flutter_social_textfield uses SocialTextSpanBuilder for rendering the text inside the attached textfield. So if you just want to show formatted text content instead of using inside an editor you can use it like this:

With version 0.0.3 you can also implement onTapDetection function for adding tap gestures to detected strings.

void initBuilder(){
    final Map<DetectedType, RegExp> _regularExpressions = {
        DetectedType.mention:atSignRegExp,
        DetectedType.hashtag:hashTagRegExp,
        DetectedType.url:urlRegex
    };

    final Map<DetectedType, TextStyle> _detectionTextStyles = {
        DetectedType.mention:TextStyle(...)
    };

    final TextStyle _defaultTextStyle = TextStyle();

    final _textSpanBuilder = SocialTextSpanBuilder(regularExpressions: _regularExpressions,defaultTextStyle:_defaultTextStyle,detectionTextStyles: _detectionTextStyles, onTapDetection: (detection){print("Tapped on detection: $detection");});
}

//And you can return RichText content bu calling build(context) method afterwards.
@override
    Widget build(BuildContext context) {
        var height = MediaQuery.of(context).size.height * 0.4;
        return Scaffold(
            appBar: AppBar(
                title: Text(widget.title),
            ),
        body: Container(
           child:RichText(
              text:_textSpanBuilder.build(context)
            )
        )// This trailing comma makes auto-formatting nicer for build methods.
    );  
}   

Acknowledgements

Thanks https://github.com/Bhupesh-V (@Bhupesh-V) for his contribution to url regex 👌

This widget's default regular expressions taken from this wonderful widget:

detectable_text_field

flutter_social_textfield's People

Contributors

berk-byrktr avatar dreampowder avatar pinguluk avatar sippetrosyan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

flutter_social_textfield's Issues

Support for types

Hello!

Thank you for this awesome package.

I was wondering if the support for custom types would be added. Right now I'm using all 3 customizable DetectionTypes and may need more in the future. Is there a way to do that, please?

How to customized some link pattern?

Hi, this lib is useful.

I want customized the link pattern, is there a way to do it?

am using this logic to buld spans:

class WidgetSpanTextEditingController extends TextEditingController {
  WidgetSpanTextEditingController({String? text})
      : super.fromValue(text == null
            ? TextEditingValue.empty
            : TextEditingValue(text: text));

  @override
  TextSpan buildTextSpan(
      {required BuildContext context,
      TextStyle? style,
      required bool withComposing}) {
    TextRange? matchedRange;
    TextRange? matchedRange2;

    if (text.contains('$SPLIT_CMD_START') && text.contains('$SPLIT_CMD_END')) {
      matchedRange = _findMatchedRange(text);
      matchedRange2 = _findMatchedRange(text, trimTag: false);
    }

    if (matchedRange != null) {
      return TextSpan(
        children: [
          TextSpan(text: matchedRange.textBefore(text)),
          WidgetSpan(
              child: GestureDetector(
                  child: Padding(
                    padding: const EdgeInsets.only(
                        right: 5.0, top: 2.0, bottom: 2.0),
                    child: ClipRRect(
                        borderRadius:
                            const BorderRadius.all(Radius.circular(5.0)),
                        child: Container(
                          padding: const EdgeInsets.all(5.0),
                          color: Colors.orange,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              Text(matchedRange2!
                                      .textInside(text)
                                      .trim()
                                      .split(SPLIT_CMD_MID)[0]
                                  //style: textStyle?.copyWith(color: Colors.orange),
                                  ),
                              const SizedBox(
                                width: 5.0,
                              ),
                              InkWell(
                                child: const Icon(
                                  Icons.close,
                                  size: 15.0,
                                ),
                                onTap: () {
                                  // controller!.value = controller!.value
                                  //     .copyWith(
                                  //         text: controller!.text.replaceRange(
                                  //             start!, start! + text.length, ''),
                                  //         selection: TextSelection.fromPosition(
                                  //             TextPosition(offset: start!)));
                                  clearMatchedText(matchedRange, text);
                                },
                              )
                            ],
                          ),
                        )),
                  ),
                  onTap: () {})),
          TextSpan(text: matchedRange.textAfter(text)),
        ],
        style: style,
      );
    }

    return TextSpan(text: text, style: style);
  }

  String clearMatchedText(TextRange? range, String text) {
    if (range != null) if (range.start >= 0 && range.end <= text.length) {
      text = text.replaceRange(range.start, range.end, '');
      return text;
    }
    return text;
  }

  // TextRange _findMatchedRange(String text) {
  //   final RegExp matchPattern = RegExp(RegExp.escape('\uffff'));
  //   late TextRange matchedRange;

  //   for (final Match match in matchPattern.allMatches(text)) {
  //     matchedRange = TextRange(start: match.start, end: match.end);
  //   }

  //   return matchedRange;
  // }

  TextRange _findMatchedRange(String text, {bool trimTag = true}) {
    final RegExp matchPattern = RegExp(r'<cmd>(.*?)<\/cmd>');
    late TextRange matchedRange;

    for (final Match match in matchPattern.allMatches(text)) {
      int start = match.start;
      if (!trimTag) start += '$SPLIT_CMD_START'.length;
      int end = match.end;
      if (!trimTag) end -= '$SPLIT_CMD_END'.length;
      matchedRange = TextRange(start: start, end: end);
    }
    return matchedRange;
  }
}

but question is when deleting, it is not delete whole span, but character by character.

Issue with skin colours emojis

When texting emojis, and you introduce a skin colour emoji, the emoji after it will be the same as first, and sometimes breaks the TextField State.

Any thoughts on this?

the method .subscribeToDetection is not there anymore?

the method .subscribeToDetection is not there here is some of my code-

void initState() { _subCommentController = SocialTextEditingController()..setTextStyle(DetectedType.mention, TextStyle(color: Colors.purple, backgroundColor: Colors.purple.withAlpha(50))); _streamSubscription = _subCommentController.subscribeToDetection(func); super.initState(); }

and also how do i add suggestions when someone types like it comes in the example gif

An issue occurred after upgrading flutter 2.2.0

An issue occurred after upgrading flutter 2.2.0

** BUILD FAILED **

Xcode's output:

4
Invalid depfile: /Users/claude/Documents/projects/Lat/Lat/.dart_tool/flutter_build/de53b0890724bc58734a7ab9fa32f191/kernel_snapshot.d
../../../../.pub-cache/hosted/pub.dartlang.org/flutter_social_textfield-0.0.3/lib/controller/social_text_editing_controller.dart:94:12: Error: The method 'SocialTextEditingController.buildTextSpan' has fewer named arguments than those of overridden method 'TextEditingController.buildTextSpan'.
TextSpan buildTextSpan({TextStyle? style, required bool withComposing}) {
^
../../../../fvm/versions/2.2.0/packages/flutter/lib/src/widgets/editable_text.dart:192:12: Context: This is the overridden method ('buildTextSpan').
TextSpan buildTextSpan({required BuildContext context, TextStyle? style , required bool withComposing}) {
^
../../../../.pub-cache/hosted/pub.dartlang.org/flutter_social_textfield-0.0.3/lib/controller/social_text_editing_controller.dart:94:12: Error: The method 'SocialTextEditingController.buildTextSpan' doesn't have the named parameter 'context' of overridden method 'TextEditingController.buildTextSpan'.
TextSpan buildTextSpan({TextStyle? style, required bool withComposing}) {
^
../../../../fvm/versions/2.2.0/packages/flutter/lib/src/widgets/editable_text.dart:192:12: Context: This is the overridden method ('buildTextSpan').
TextSpan buildTextSpan({required BuildContext context, TextStyle? style , required bool withComposing}) {

Unable to detect URL with "-" in subdomain

Here https://bhupesh-v.github.io/ is a sample URL that is not being detected

Seems like the regexContent is missing "-" as valid char

const urlRegexContent = "((http|https)://)(www.)?" +
    "[a-zA-Z0-9@:%._\\+~#?&//=]" +
    "{2,256}\\.[a-z]" +
    "{2,6}\\b([-a-zA-Z0-9@:%" +
    "._\\+~#?&//=]*)";

Here's a draft solution

void main() {
  const oldurlRegexContent = "((http|https)://)(www.)?" +
    "[a-zA-Z0-9@:%._\\+~#?&//=]" +
    "{2,256}\\.[a-z]" +
    "{2,6}\\b([-a-zA-Z0-9@:%" +
    "._\\+~#?&//=]*)";
 
// notice the extra "-"
  const newurlRegexContent = "((http|https)://)(www.)?" +
    "[a-zA-Z0-9@:%._\\+~#?&//=-]" +
    "{2,256}\\.[a-z]" +
    "{2,6}\\b([-a-zA-Z0-9@:%" +
    "._\\+~#?&//=]*)";
  
  var f1 = RegExp(oldurlRegexContent);
  var f2 = RegExp(newurlRegexContent);
  print(f1.stringMatch("https://bhupesh-v.github.io/"));
  // null
  print(f2.stringMatch("https://bhupesh-v.github.io/"));
  // https://bhupesh-v.github.io/

}

size.isFinite is not true

this is the reply textfield

  replyField({required this.commentID, required this.docName, Key? key})
      : super(key: key);

  final String commentID;
  final String docName;

  @override
  _replyFieldState createState() => _replyFieldState();
}

class _replyFieldState extends State<replyField> {
  late final CollectionReference users =
      FirebaseFirestore.instance.collection('Users');

  late final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

  late final FirebaseStorage storage = FirebaseStorage.instance;

  late final CollectionReference posts =
      FirebaseFirestore.instance.collection('Posts');
  late StreamSubscription<SocialContentDetection> _streamSubscription;
  SocialTextEditingController _subCommentController =
      SocialTextEditingController()
        ..setTextStyle(
            DetectedType.mention,
            TextStyle(
                color: Colors.purple,
                backgroundColor: Colors.purple.withAlpha(50)));
  bool isSuggestion = false;
  FocusNode _focusNode = FocusNode();
  ScrollController _scrollController = ScrollController();
  late SocialContentDetection lastDetection;
  @override
  void initState() {
    super.initState();

    _streamSubscription =
        _subCommentController.subscribeToDetection(testFunction);
  }

  void testFunction(SocialContentDetection detection) {
    lastDetection = detection;
  }

  void dispose() {
    _focusNode.dispose();
    _streamSubscription.cancel();
    _subCommentController.dispose();
    _scrollController.dispose();
    super.dispose();
  }

  PreferredSize mention(double height) {
    return PreferredSize(
      child: FutureBuilder<QuerySnapshot>(
          future: users.get(),
          builder: (context, snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.waiting:
                return SizedBox.shrink();
              default:
                if (snapshot.hasError) {
                  return Text(snapshot.error.toString());
                } else {
                  var suggestions = snapshot.data!.docs
                      .where((element) => element
                          .get('username')
                          .contains(lastDetection.text.replaceAll("@", "")))
                      .toList();
                  return Container(
                    child: ListView.separated(
                      itemCount: suggestions.length,
                      itemBuilder: (context, index) {
                        return ListTile(
                          title: suggestions[index].get("username"),
                        );
                      },
                      separatorBuilder: (context, index) {
                        return Divider(color: Colors.grey[600]);
                      },
                    ),
                  );
                }
            }
          }),
      preferredSize: Size.fromHeight(height),
    );
  }

  @override
  Widget build(BuildContext context) {
    var height = MediaQuery.of(context).size.height * 0.4;
    return DefaultSocialTextFieldController(
      focusNode: _focusNode,
      scrollController: _scrollController,
      textEditingController: _subCommentController,
      child: Container(
        padding: EdgeInsets.all(8),
        child: Column(
          children: <Widget>[
            Expanded(
              child: TextField(
                focusNode: _focusNode,
                expands: true,
                scrollController: _scrollController,
                scrollPhysics: AlwaysScrollableScrollPhysics(),
                controller: _subCommentController,
                decoration: InputDecoration(
                  suffixIcon: IconButton(
                    icon: Icon(Icons.reply),
                    onPressed: () async {
                        doing things with firebase and etc. etc.
                        setState(() {});
                      }
                    },
                  ),
                  filled: true,
                  hintText: "Reply",
                  prefixIcon: Icon(Icons.messenger, color: Colors.grey[600]),
                  border: OutlineInputBorder(
                    borderSide: BorderSide.none,
                  ),
                ),
                textInputAction: TextInputAction.done,
                minLines: null,
                maxLines: null,
              ),
            ),
          ],
        ),
      ),
      detectionBuilders: {DetectedType.mention: (_) => mention(height)},
    );
  }
}`

the reply textfield is being used in a ListTile'schildren: which is inside an ExpansionTile which is inside a streambuilder here is the full code(its big and from the ExpansionTile() ):


return ExpansionTile(
    textColor:
        Colors.black54,
    iconColor:
        Colors.black54,
    leading:
        CircleAvatar(
      backgroundColor:
          Colors.black,
    ),
    title:
        Row(
      mainAxisAlignment:
          MainAxisAlignment.spaceBetween,
      children: [
        Text(
          commentData[commentIndex]['username'],
        ),
        Text(commentData[commentIndex]['Time']),
      ],
    ),
    subtitle:
        Row(
      mainAxisAlignment:
          MainAxisAlignment.spaceBetween,
      children: <Widget>[
        Text.rich(
          TextSpan(
              text: '',
              children: commentData[commentIndex]['comment'].split(' ').map<InlineSpan>((w) {
                return w.startsWith('@') && w.length > 1
                    ? TextSpan(
                        text: ' ' + w,
                        style: TextStyle(color: Colors.blue),
                        recognizer: TapGestureRecognizer()..onTap = () {},
                      )
                    : TextSpan(text: ' ' + w, style: TextStyle(color: Colors.black));
              }).toList()),
        ),
        commentLikeButton(commentData[commentIndex]["Likes"], list[index]['document_name'], commentID[commentIndex], "")
      ],
    ),
    trailing:
        SizedBox.shrink(),

    children: [
      // reply list
      subcommentData.isEmpty
          ? Text('No Comments Yet')
          : ListView.separated(
              itemCount: subcommentData.length,
              scrollDirection: Axis.vertical,
              shrinkWrap: true,
              separatorBuilder: (context, index) {
                return Divider(
                  color: Colors.grey[600],
                );
              },
              itemBuilder: (context, replyIndex) {
                return Container(
                  height: 50,
                  color: Colors.white,
                  child: ListTile(
                    dense: true,
                    leading: CircleAvatar(
                      backgroundColor: Colors.black,
                    ),
                    title: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                      Text(subcommentData[replyIndex]['username']),
                      Text(subcommentData[replyIndex]['Time'])
                    ]),
                    subtitle: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text.rich(
                          TextSpan(
                              text: '',
                              children: commentData[replyIndex]['comment'].split(' ').map<InlineSpan>((w) {
                                return w.startsWith('@') && w.length > 1
                                    ? TextSpan(
                                        text: ' ' + w,
                                        style: TextStyle(color: Colors.blue),
                                        recognizer: TapGestureRecognizer()..onTap = () {},
                                      )
                                    : TextSpan(text: ' ' + w, style: TextStyle(color: Colors.black));
                              }).toList()),
                        ),
                        commentLikeButton(subcommentData[replyIndex]['Likes'], list[index]['document_name'], commentID[commentIndex], subcommentID[replyIndex])
                      ],
                    ),
                  ),
                );
              }),

      // reply text field

      SizedBox(
        height: 10,
      ),
      replyField(
          commentID: commentID[commentIndex],
          docName: list[index]['document_name']),
    ],
    // trailing: Text(date),
  );

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.