39 lines
1.1 KiB
Dart
39 lines
1.1 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
|
|
class DynamicText extends StatelessWidget {
|
|
final String text;
|
|
final TextStyle highlightedStyle;
|
|
final TextStyle normalStyle;
|
|
|
|
DynamicText({required this.text, required this.highlightedStyle, required this.normalStyle});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return RichText(
|
|
text: _buildTextSpan(text, highlightedStyle, normalStyle),
|
|
);
|
|
}
|
|
|
|
TextSpan _buildTextSpan(String text, TextStyle highlightedStyle, TextStyle normalStyle) {
|
|
List<TextSpan> spans = [];
|
|
final RegExp regExp = RegExp(r'\*([^*]+)\*');
|
|
int lastIndex = 0;
|
|
|
|
for (final match in regExp.allMatches(text)) {
|
|
if (match.start > lastIndex) {
|
|
spans.add(TextSpan(text: text.substring(lastIndex, match.start), style: normalStyle));
|
|
}
|
|
spans.add(TextSpan(
|
|
text: '(${match.group(1)})',
|
|
style: highlightedStyle,
|
|
));
|
|
lastIndex = match.end;
|
|
}
|
|
|
|
if (lastIndex < text.length) {
|
|
spans.add(TextSpan(text: text.substring(lastIndex), style: normalStyle));
|
|
}
|
|
|
|
return TextSpan(children: spans);
|
|
}
|
|
} |