Reputation:
I need to create a custom SpanText, but i found this issue. A value of type 'TextSpan' can't be returned from the method 'build' because it has a return type of 'Widget'.
The Code Is:
class StareWidget extends StatelessWidget {
const StareWidget({Key? key, required this.text}) : super(key: key);
final String text;
@override
Widget build(BuildContext context) {
return TextSpan();
}
}
Upvotes: 1
Views: 1517
Reputation: 818
TextSpan buildTextSpan({required BuildContext context, TextStyle?style}) {
return TextSpan(
style: style,
text: 'your text one',
children: const <TextSpan>[
TextSpan(text: 'your text two'),
],);}
Upvotes: 0
Reputation: 14775
Try below code hope its help to you.
Refer TextSpan here
Refer RichText here
class StareWidget extends StatelessWidget {
const StareWidget({
Key? key,
required this.text,
}) : super(key: key);
final String text;
@override
Widget build(BuildContext context) {
return Text.rich(
TextSpan(
text: 'This is textspan ',
children: <InlineSpan>[
TextSpan(
text: 'Widget in flutter',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
)
],
),
);
}
}
Upvotes: 0
Reputation: 4666
According to documentation
To paint a TextSpan on a Canvas, use a TextPainter. To display a text span in a widget, use a RichText. For text with a single style, consider using the Text widget.
So your TextSpan
needs to have a RichText
as parent.
Upvotes: 1