I want to create a widget that will underline a particular word in a sentence but have this issue: error: The element type 'List' can't be assigned to the list type 'InlineSpan'.
From screen:
UnderlineMe(sentence: 'I Love Stackoverflow', underline: 'Love'),
From Stateless Widget
class UnderlineMe extends StatelessWidget {
final String sentence;
final String underline;
const UnderlineMe(
{super.key, required this.sentence, required this.underline});
@override
Widget build(BuildContext context) {
var splitSentence = sentence.split(' ');
List<InlineSpan> childrenTextSpan = [];
for (var i = 0; i < splitSentence.length; i++) {
if (splitSentence[i] == underline) {
childrenTextSpan.add(TextSpan(
text: splitSentence[i],
style: const TextStyle(
decoration: TextDecoration.underline,
)));
} else {
TextSpan(
text: splitSentence[i],
);
}
}
return Text.rich(
TextSpan(
children: [
childrenTextSpan,
],
),
);
}
}
Please help. Thanks
You may give it a try to this code! It works fine!
Thanks!