Skip to content

Commit

Permalink
feat: boldstyle added to customize bold style
Browse files Browse the repository at this point in the history
few refactors
  • Loading branch information
rohanjsh committed Dec 24, 2023
1 parent cb5624e commit 2109071
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
43 changes: 34 additions & 9 deletions lib/src/core/typeset_parser.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:typeset/src/core/typeset_controller.dart';
import 'package:typeset/src/models/style_type_enum.dart';
import 'package:typeset/typeset.dart';
Expand Down Expand Up @@ -223,6 +222,7 @@ class TypesetParser {
TextStyle? linkStyle,
GestureRecognizer? recognizer,
TextStyle? monospaceStyle,
TextStyle? boldStyle,
}) {
final controller = TypesetController(
input: inputText,
Expand All @@ -238,7 +238,7 @@ class TypesetParser {

switch (text.styleType) {
case StyleTypeEnum.link:
final linkData = text.value.split('|');
final linkData = text.value.split(TypesetReserved.linkSplitChar);
final linkText = linkData.isNotEmpty ? linkData[0] : '';
final url = linkData.length == 2 ? linkData[1] : '';
spans.add(
Expand All @@ -254,7 +254,10 @@ class TypesetParser {
decorationColor: Colors.blue,
),
recognizer: recognizer ??
(TapGestureRecognizer()..onTap = () => _launchUrl(url)),
(TapGestureRecognizer()
..onTap = () => _launchUrl(
url,
)),
),
);
break;
Expand All @@ -264,7 +267,24 @@ class TypesetParser {
text: justText,
style: monospaceStyle ??
GoogleFonts.sourceCodePro(
textStyle: TextStyle(fontSize: fontSize),
textStyle: TextStyle(
fontSize: fontSize,
),
),
),
);
break;

case StyleTypeEnum.bold:
spans.add(
TextSpan(
text: justText,
style: boldStyle?.copyWith(
fontSize: fontSize,
) ??
TextStyle(
fontWeight: FontWeight.bold,
fontSize: fontSize,
),
),
);
Expand All @@ -275,9 +295,6 @@ class TypesetParser {
TextSpan(
text: justText,
style: TextStyle(
fontWeight: text.styleType == StyleTypeEnum.bold
? FontWeight.bold
: null,
fontStyle: text.styleType == StyleTypeEnum.italic
? FontStyle.italic
: null,
Expand All @@ -296,8 +313,16 @@ class TypesetParser {
}

static Future<void> _launchUrl(String url) async {
if (await canLaunchUrl(Uri.parse(url))) {
await launchUrl(Uri.parse(url));
if (await canLaunchUrl(
Uri.parse(
url,
),
)) {
await launchUrl(
Uri.parse(
url,
),
);
}
}
}
3 changes: 3 additions & 0 deletions lib/src/core/typeset_reserved.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ final class TypesetReserved {
/// The literal for creating links.
static const linkChar = '§';

/// The literal for splitting links.
static const linkSplitChar = '|';

/// Regex to identify links
static const fontSizeRegex = r'(.+?)<(\d+)>';
}
5 changes: 5 additions & 0 deletions lib/src/typeset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class TypeSet extends StatelessWidget {
this.recognizer,
this.linkStyle,
this.monospaceStyle,
this.boldStyle,
});

///[style] is the style of the text
Expand Down Expand Up @@ -113,6 +114,9 @@ class TypeSet extends StatelessWidget {
///[monospaceStyle] is the style of the monospace text
final TextStyle? monospaceStyle;

///[boldStyle] is the style of the bold text
final TextStyle? boldStyle;

@override
Widget build(BuildContext context) {
// Use the `RichText` widget to display the text with the correct styles
Expand All @@ -123,6 +127,7 @@ class TypeSet extends StatelessWidget {
recognizer: recognizer,
linkStyle: linkStyle,
monospaceStyle: monospaceStyle,
boldStyle: boldStyle,
),
),
textAlign: textAlign,
Expand Down
1 change: 1 addition & 0 deletions lib/typeset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/// with different formatters
library typeset;

export 'package:google_fonts/google_fonts.dart';
export 'src/core/typeset_reserved.dart';
export 'src/typeset.dart';
export 'src/typeset_ext.dart';

0 comments on commit 2109071

Please sign in to comment.