Skip to content

Instantly share code, notes, and snippets.

@miriamMartinezUB
Last active March 1, 2021 22:09
Show Gist options
  • Save miriamMartinezUB/b3580b9474d90673faadd866b22f0e7a to your computer and use it in GitHub Desktop.
Save miriamMartinezUB/b3580b9474d90673faadd866b22f0e7a to your computer and use it in GitHub Desktop.
How custom the text in flutter:
import 'package:flutter/widgets.dart';
final regExpPattern = r'\[([^\]]+)\]\(([^\s\)]+)\)';
final regExp = RegExp(regExpPattern);
const String BOLD = 'B';
const String ITALIC = 'I';
const String COLORED = 'C';
const String BOLD_AND_COLORED = 'BC';
const String ITALIC_AND_COLORED = 'IC';
const String ITALIC_AND_BOLD = 'IB';
class StylizedText extends StatelessWidget {
final String text;
final Color color;
final Color stylizedTextColor;
final double fontSize;
final FontWeight fontWeight;
final TextAlign textAlign;
const StylizedText({
@required this.text,
@required this.color,
this.stylizedTextColor,
@required this.fontSize,
@required this.fontWeight,
this.textAlign = TextAlign.start,
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return RichText(
text: TextSpan(
children: _createTextSpans(text),
),
textAlign: textAlign,
);
}
List<TextSpan> _createTextSpans(String content) {
List<TextSpan> result = [];
List<String> splitContentList = _generateElements(content);
for (String text in splitContentList) {
result.add(getStylizedText(text));
}
return result;
}
TextSpan getStylizedText(String text) {
final match = regExp.firstMatch(text);
TextStyle textStyle = TextStyle(
color: color,
fontSize: fontSize,
fontWeight: fontWeight,
);
if (match?.groupCount == 2) {
text = match.group(1);
String style = _getStyleName(match.group(2));
switch (style) {
case BOLD:
textStyle = TextStyle(
color: color,
fontSize: fontSize,
fontWeight: FontWeight.bold,
);
break;
case ITALIC:
textStyle = TextStyle(
color: color,
fontSize: fontSize,
fontWeight: fontWeight,
fontStyle: FontStyle.italic,
);
break;
case COLORED:
textStyle = TextStyle(
color: _stylizedTextColor(),
fontSize: fontSize,
fontWeight: fontWeight,
);
break;
case BOLD_AND_COLORED:
textStyle = TextStyle(
color: _stylizedTextColor(),
fontSize: fontSize,
fontWeight: FontWeight.bold,
);
break;
case ITALIC_AND_COLORED:
textStyle = TextStyle(
color: _stylizedTextColor(),
fontSize: fontSize,
fontWeight: fontWeight,
fontStyle: FontStyle.italic,
);
break;
case ITALIC_AND_BOLD:
textStyle = TextStyle(
color: color,
fontSize: fontSize,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic,
);
break;
}
}
return TextSpan(text: text, style: textStyle);
}
Color _stylizedTextColor() => stylizedTextColor != null ? stylizedTextColor : color;
}
_getStyleName(String styleName) {
if (styleName == 'CB') {
return styleName = BOLD_AND_COLORED;
} else if (styleName == 'CI') {
return styleName = ITALIC_AND_COLORED;
} else if (styleName == 'BI') {
return styleName = ITALIC_AND_BOLD;
} else {
return styleName;
}
}
List<String> _generateElements(String text) {
if (text.isEmpty) return [];
List<String> elements = List();
final matches = regExp.allMatches(text);
if (matches.isEmpty) {
elements.add(
text,
);
} else {
var index = 0;
matches.forEach((match) {
if (match.start != 0) {
elements.add(
text.substring(index, match.start),
);
}
elements.add(
match.group(0),
);
index = match.end;
});
if (index < text.length) {
elements.add(
text.substring(index),
);
}
}
return elements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment