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();
  }
}


Solution 1: esentis

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.


Solution 2: Ravindra S. Patil

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,
            ),
          )
        ],
      ),
    );
  }
}

Your result screen-> image