Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help with making WhatsApp like formatting / italic / bold / underline #22

Closed
developer-farhan opened this issue Oct 14, 2021 · 15 comments

Comments

@developer-farhan
Copy link

im trying to get bold and italic and underline formatting like WhatsApp and im using the following but im getting no result

patternList: [
///bold font
EasyRichTextPattern(
targetString: '()(.?)()',
matchBuilder:
(BuildContext context, RegExpMatch match) {
print(match[0]);
return TextSpan(
text: match[0].replaceAll('', ''),
style:
TextStyle(fontWeight: FontWeight.bold),
);
},
),

                      ///italic font
                      EasyRichTextPattern(
                        targetString: '(\_)(.*?)(\_)',
                        matchBuilder:
                            (BuildContext context, RegExpMatch match) {
                          print(match[0]);
                          return TextSpan(
                            text: match[0].replaceAll('_', ''),
                            style:
                                TextStyle(fontStyle: FontStyle.italic),
                          );
                        },
                      ),
                    ],

Screenshot 2021-10-13 at 13 37 02

the other text turns black idk why when default color is white could you help me out
@2000calories
Copy link
Owner

Need double \ in dart regular expression

                      EasyRichTextPattern(
                        targetString: '(\\_)(.*?)(\\_)',
                        matchBuilder:
                            (BuildContext context, RegExpMatch match) {
                          print(match[0]);
                          return TextSpan(
                            text: match[0].replaceAll('_', ''),
                            style:
                                TextStyle(fontStyle: FontStyle.italic),
                          );
                        },
                      ),

@developer-farhan
Copy link
Author

developer-farhan commented Oct 18, 2021

@2000calories that was the first thing I did but I keep getting this error

The following FormatException was thrown building EasyRichText(dirty):
Invalid identity escape in Unicode pattern((?<!\w)(_)(.*?)(_)(?!\w))

@2000calories
Copy link
Owner

can you show your flutter doctor result

@developer-farhan
Copy link
Author

developer-farhan commented Oct 19, 2021

@2000calories Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.6, on macOS 11.5.1 20G80 darwin-arm, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] Connected device (2 available)

• No issues found!

@2000calories
Copy link
Owner

OK, I found a bug when using multiple WhatsApp-like EasyRichTextPattern, I will fix it soon.

@developer-farhan
Copy link
Author

@2000calories do keep me posted please prompt fix would be very well appreciated

@2000calories
Copy link
Owner

@officialFlutterDeveloper
Try the latest master branch. I will publish a new version if it works.

  easy_rich_text:
    git:
      url: https://github.com/2000calories/flutter_easy_rich_text.git
      ref: master

@developer-farhan
Copy link
Author

developer-farhan commented Oct 23, 2021

@2000calories did not work for me

@2000calories
Copy link
Owner

try the following example

///WhatsApp like text formatter
EasyRichText(
  "what are *you* doing *and* hi ~hey~. _italic_ ",
  defaultStyle: TextStyle(color: Colors.white),
  patternList: [
    ///bold font
    EasyRichTextPattern(
      targetString: '(\\*)(.*?)(\\*)',
      matchBuilder:
          (BuildContext context, RegExpMatch match) {
        print(match[0]);
        return TextSpan(
          text: match[0].replaceAll('*', ''),
          style: TextStyle(fontWeight: FontWeight.bold),
        );
      },
    ),

    ///italic font
    EasyRichTextPattern(
      targetString: '(\_)(.*?)(\_)',
      matchBuilder:
          (BuildContext context, RegExpMatch match) {
        print(match[0]);
        return TextSpan(
          text: match[0].replaceAll('_', ''),
          style: TextStyle(fontStyle: FontStyle.italic),
        );
      },
    ),

    ///strikethrough
    EasyRichTextPattern(
      targetString: '(\~)(.*?)(\~)',
      matchBuilder:
          (BuildContext context, RegExpMatch match) {
        print(match[0]);
        return TextSpan(
          text: match[0].replaceAll('~', ''),
          style: TextStyle(
              decoration: TextDecoration.lineThrough),
        );
      },
    ),
  ],
),

@developer-farhan
Copy link
Author

@2000calories worked perfectly well done!

@2000calories
Copy link
Owner

I published a new version.

dependencies:
  easy_rich_text: '^1.0.0'

@developer-farhan
Copy link
Author

is it possible I can use the text changing in textfield? just like how WhatsApp does ? so people can play around before they even send it

@2000calories
Copy link
Owner

i think you cannot use RichText widget as input of TextFeld widget. Perhaps you can show the RichText widget as a preview just above the TextFeld widget if special format were detected in the input.

@radheyshyamjat
Copy link

radheyshyamjat commented Mar 14, 2023

With extend this one anyone please help me to implement both at a time like *_Bold Plus Italic_*

@radheyshyamjat
Copy link

radheyshyamjat commented Mar 14, 2023

With extend this one anyone please help me to implement both at a time like Bold Plus Italic

This also resolve it's just a matter or regex it's very good package for use formatting
If anyone required help posting correct answer also

 EasyRichTextPattern(targetString: '([*_~]{3})(.*?)([_*~])',
          matchBuilder: (BuildContext context, RegExpMatch? match) {
            return TextSpan(
              text: gerReplacedTextBoldItalicStrike(match?[0]),
              style: TextStyle(
                  fontStyle: FontStyle.italic,
                  fontWeight: FontWeight.bold,
                  decoration: TextDecoration.lineThrough),
            );
          },
        )


     String gerReplacedTextBoldItalicStrike(String? msg){
       String? msgString = msg;
       if(msgString == null) return "";
       msgString = msgString.replaceAll("*", "");
       msgString = msgString.replaceAll("_", "");
       msgString = msgString.replaceAll("~", "");
       return msgString;
     }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants