user3808307
user3808307

Reputation: 1471

Pass params to stateless widget in Flutter

I am trying to do a stateless widget for inline text links.

I am using this answer here https://stackoverflow.com/a/55607224/3808307 to create the links

RichText(
  text: TextSpan(
    children: [
      TextSpan(text: 'This is a going to be a Text which has '),
      TextSpan(
        text: 'single tap',
        style: style,
        recognizer: TapGestureRecognizer()
          ..onTap = () {
            // single tapped
          },
      ),
      TextSpan(text: ' along with '),
      TextSpan(
        text: 'double tap',
        style: style,
        recognizer: DoubleTapGestureRecognizer()
          ..onDoubleTap = () {
            // double tapped
          },
      ),
      TextSpan(text: ' and '),
      TextSpan(
        text: 'long press',
        style: style,
        recognizer: LongPressGestureRecognizer()
          ..onLongPress = () {
            // long pressed
          },
      ),
    ],
  ),
)

, but I would like to have a TextSpan that I can import, and already has the styling applied, passing it a text that would act as label and a function.

Do I need a stateful widget for this, or can I just use a stateless widget?

Upvotes: 0

Views: 118

Answers (1)

You can use a stateless widget without a problem.

In the case that you need to change the style of the text when you press the links you will need to use a stateful widget.

Upvotes: 1

Related Questions